CMakeLists.txt: Change required cmake version to 2.8
[rrq/fuse_xattrs.git] / utils.c
1 /*
2   fuse_xattrs - Add xattrs support using sidecar files
3
4   Copyright (C) 2016  Felipe Barriga Richards <felipe {at} felipebarriga.cl>
5
6   This program can be distributed under the terms of the GNU GPL.
7   See the file COPYING.
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "utils.h"
15 #include "const.h"
16
17
18 char *get_sidecar_path(const char *path)
19 {
20     const size_t path_len = strlen(path);
21     const size_t sidecar_ext_len = strlen(SIDECAR_EXT); // this can be optimized
22     const size_t sidecar_path_len = path_len + sidecar_ext_len + 1;
23     char *sidecar_path = (char *) malloc(sidecar_path_len);
24     memset(sidecar_path, '\0', sidecar_path_len);
25     memcpy(sidecar_path, path, path_len);
26     memcpy(sidecar_path + path_len, SIDECAR_EXT, sidecar_ext_len);
27
28     return sidecar_path;
29 }
30
31 // TODO: make it work for binary data
32 char *sanitize_value(const char *value, size_t value_size)
33 {
34     char *sanitized = malloc(value_size + 1);
35     memcpy(sanitized, value, value_size);
36     sanitized[value_size] = '\0';
37     return sanitized;
38 }
39
40 enum namespace get_namespace(const char *name) {
41     if (strncmp(name, "user.", strlen("user.")) == 0) {
42         return USER;
43     }
44
45     if (strncmp(name, "system.", strlen("system.")) == 0) {
46         return SYSTEM;
47     }
48
49     if (strncmp(name, "security.", strlen("security.")) == 0) {
50         return SECURITY;
51     }
52
53     if (strncmp(name, "trusted.", strlen("trusted.")) == 0) {
54         return TRUSTED;
55     }
56
57     error_print("invalid namespace for key: %s\n", name);
58     return ERROR;
59 }