-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
150 lines (119 loc) · 2.48 KB
/
common.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "common.h"
int safe_creat(const char *pathname, mode_t mode)
{
int fd = creat(pathname, mode);
assert(fd != -1);
return fd;
}
int safe_open(const char *pathname, int flags)
{
int fd = open(pathname, flags);
assert(fd != -1);
return fd;
}
void safe_mkdirs(const char *dir, mode_t mode)
{
size_t len = strlen(dir);
char *tmp = safe_malloc(len + 1);
strcpy(tmp, dir);
assert(tmp[len] == '\0');
for (size_t i = 1; i <= len; i++) {
if (tmp[i] == '/') {
tmp[i] = 0;
mkdir(tmp, mode) == 0;
tmp[i] = '/';
}
}
mkdir(tmp, mode);
safe_free(tmp);
}
int safe_creat_from_filepath(const char *pathname, mode_t mode)
{
char *dir = safe_malloc(strlen(pathname) + 1);
strcpy(dir, pathname);
dirname(dir);
safe_mkdirs(dir, mode);
safe_free(dir);
return safe_creat(pathname, mode);
}
size_t safe_find_files_in_dir(
const char *rootpath,
const char *name,
int outbin_fd,
file_func_ptr func)
{
DIR *dir;
struct dirent *entry;
size_t filecount = 0;
char *path;
dir = opendir(name);
if (!dir)
return filecount;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".") == 0)
continue;
if (strcmp(entry->d_name, "..") == 0)
continue;
path = safe_join_str(name, entry->d_name);
filecount += safe_find_files_in_dir(
rootpath,
path,
outbin_fd,
func);
safe_free(path);
} else {
path = safe_join_str(name, entry->d_name);
func(rootpath, path, outbin_fd);
safe_free(path);
filecount++;
}
}
closedir(dir);
return filecount;
}
void safe_read(int fd, void *buf, size_t count)
{
assert(read(fd, buf, count) == count);
}
void safe_lseek(int fd, off_t offset, int whence)
{
assert(lseek(fd, offset, whence) != (off_t)-1);
}
void *safe_malloc(size_t size)
{
void *buf = malloc(size);
memset(buf, 0, size);
assert(buf != NULL);
return buf;
}
void safe_free(void *buf)
{
assert(buf != NULL);
free(buf);
}
void safe_write(int fd, const void *buf, size_t count)
{
assert(write(fd, buf, count) == count);
}
void safe_close(int fd)
{
assert(close(fd) == 0);
}
size_t safe_get_file_size(int fd)
{
struct stat st;
assert(fstat(fd, &st) == 0);
return st.st_size;
}
char *safe_join_str(const char *s1, const char *s2)
{
size_t len = strlen(s1) + strlen(s2) + 1;
char *path = safe_malloc(len + 1);
assert(sprintf(path, "%s/%s", s1, s2) == len);
return path;
}
const char *safe_clear_path(const char *rootpath, const char *fullpath)
{
return fullpath + strlen(rootpath) + 1;
}