-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.c
47 lines (37 loc) · 1.09 KB
/
message.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "message.h"
#include <string.h>
#include <stdlib.h>
MESSAGE init_message(char* operation, uid_t uid, pid_t pid, char* file_path,
char* chunk, int chunk_size, int status) {
MESSAGE m = malloc(sizeof(*m));
change_message(m, operation, uid, pid, file_path, chunk, chunk_size, status);
return m;
}
MESSAGE empty_message() {
MESSAGE m = malloc(sizeof(*m));
return m;
}
void change_message(MESSAGE m, char* operation, uid_t uid, pid_t pid, char* file_path,
char* chunk, int chunk_size, int status) {
if (!strcmp(operation, "backup"))
m->operation = BACKUP;
else if (!strcmp(operation, "restore"))
m->operation = RESTORE;
else if (!strcmp(operation, "delete"))
m->operation = DELETE;
else if (!strcmp(operation, "gc"))
m->operation = CLEAN;
m->status = status;
m->chunk_size = chunk_size;
m->pid = pid;
m->uid = uid;
strncpy(m->file_path, file_path, PATH_SIZE);
memcpy(m->chunk, chunk, m->chunk_size);
}
void freeMessage(MESSAGE m) {
free(m);
}
char* get_file_name(char *file_path) {
char* base = strrchr(file_path,'/');
return base ? base+1 : file_path;
}