-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.c
93 lines (76 loc) · 2.08 KB
/
utils.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
SafeFS
(c) 2016 2016 INESC TEC. Written by J. Paulo and R. Pontes
*/
#include <errno.h>
#include <linux/limits.h>
#include <openssl/rand.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "utils.h"
int file_exists(char *path) {
struct stat s;
int err = stat(path, &s);
if (err != 0) {
return 0;
}
/* Check if path points to a regular path */
if (!S_ISREG(s.st_mode)) {
return 0;
}
return 1;
}
/*
void generate_random_block(unsigned char* str, int size){
static unsigned char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
int i;
for(i=0;i<size;i++){
int charset_pos=rand() % (int)(sizeof(charset) -1);
str[i]=charset[charset_pos];
}
}*/
void generate_random_block(unsigned char *str, int size) {
// Sometimes this function can return an error if not enough sources of randomness was found.
RAND_pseudo_bytes(str, size);
}
// Replace the path of the file intercepted with fuse with the one of the meta file
// TODO this ROOTPATH is hardcoded for now
int replace_path(char const *path, const char *newpath) {
strcat((char *)newpath, path);
return 0;
}
int mkdir_p(const char *path) {
/* Copied from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950 */
const size_t len = strlen(path);
char _path[PATH_MAX];
char *p;
errno = 0;
/* Copy string so its mutable */
if (len > sizeof(_path) - 1) {
errno = ENAMETOOLONG;
return -1;
}
strcpy(_path, path);
/* Iterate the string */
for (p = _path + 1; *p; p++) {
if (*p == '/') {
/* Temporarily truncate */
*p = '\0';
if (mkdir(_path, S_IRWXU) != 0) {
if (errno != EEXIST) {
return -1;
}
}
*p = '/';
}
}
if (mkdir(_path, S_IRWXU) != 0) {
if (errno != EEXIST) {
return -1;
}
}
return 0;
}