Line | Count | Source (jump to first uncovered line) |
1 | | |
2 | | /** |
3 | | * fs.c |
4 | | * |
5 | | * copyright 2013 - joseph werle <joseph.werle@gmail.com> |
6 | | */ |
7 | | |
8 | | #define _POSIX_C_SOURCE 200809L |
9 | | #include <stdlib.h> |
10 | | #include <stdio.h> |
11 | | #include <dirent.h> |
12 | | #include <string.h> |
13 | | #include <errno.h> |
14 | | #include <sys/types.h> |
15 | | #include <sys/stat.h> |
16 | | #include <unistd.h> |
17 | | #include <fcntl.h> |
18 | | #include "fs.h" |
19 | | |
20 | | void |
21 | 0 | fs_error (const char *prefix) { |
22 | 0 | char fmt[256]; |
23 | 0 | sprintf(fmt, "fs: %s: error", prefix); |
24 | 0 | perror(fmt); |
25 | 0 | } |
26 | | |
27 | | |
28 | | FILE * |
29 | 2.05k | fs_open (const char *path, const char *flags) { |
30 | 2.05k | return fopen(path, flags); |
31 | 2.05k | } |
32 | | |
33 | | |
34 | | int |
35 | 0 | fs_close (FILE *file) { |
36 | 0 | return fclose(file); |
37 | 0 | } |
38 | | |
39 | | |
40 | | int |
41 | 0 | fs_rename (const char *from, const char *to) { |
42 | 0 | return rename(from, to); |
43 | 0 | } |
44 | | |
45 | | |
46 | | fs_stats * |
47 | 0 | fs_stat (const char *path) { |
48 | 0 | fs_stats *stats = (fs_stats*) malloc(sizeof(fs_stats)); |
49 | 0 | int e = stat(path, stats); |
50 | 0 | if (-1 == e) { |
51 | 0 | free(stats); |
52 | 0 | return NULL; |
53 | 0 | } |
54 | 0 | return stats; |
55 | 0 | } |
56 | | |
57 | | |
58 | | fs_stats * |
59 | 0 | fs_fstat (FILE *file) { |
60 | 0 | if (NULL == file) return NULL; |
61 | 0 | fs_stats *stats = (fs_stats*) malloc(sizeof(fs_stats)); |
62 | 0 | int fd = fileno(file); |
63 | 0 | int e = fstat(fd, stats); |
64 | 0 | if (-1 == e) { |
65 | 0 | free(stats); |
66 | 0 | return NULL; |
67 | 0 | } |
68 | 0 | return stats; |
69 | 0 | } |
70 | | |
71 | | |
72 | | fs_stats * |
73 | 0 | fs_lstat (const char *path) { |
74 | 0 | fs_stats *stats = (fs_stats*) malloc(sizeof(fs_stats)); |
75 | | #ifdef _WIN32 |
76 | | int e = stat(path, stats); |
77 | | #else |
78 | 0 | int e = lstat(path, stats); |
79 | 0 | #endif |
80 | 0 | if (-1 == e) { |
81 | 0 | free(stats); |
82 | 0 | return NULL; |
83 | 0 | } |
84 | 0 | return stats; |
85 | 0 | } |
86 | | |
87 | | |
88 | | int |
89 | 0 | fs_ftruncate (FILE *file, int len) { |
90 | 0 | int fd = fileno(file); |
91 | 0 | return ftruncate(fd, (off_t) len); |
92 | 0 | } |
93 | | |
94 | | |
95 | | int |
96 | 0 | fs_truncate (const char *path, int len) { |
97 | | #ifdef _WIN32 |
98 | | int ret = -1; |
99 | | int fd = open(path, O_RDWR | O_CREAT, S_IREAD | S_IWRITE); |
100 | | if (fd != -1) { |
101 | | ret = ftruncate(fd, (off_t) len); |
102 | | close(fd); |
103 | | } |
104 | | return ret; |
105 | | #else |
106 | 0 | return truncate(path, (off_t) len); |
107 | 0 | #endif |
108 | 0 | } |
109 | | |
110 | | |
111 | | int |
112 | 0 | fs_chown (const char *path, int uid, int gid) { |
113 | | #ifdef _WIN32 |
114 | | errno = ENOSYS; |
115 | | return -1; |
116 | | #else |
117 | 0 | return chown(path, (uid_t) uid, (gid_t) gid); |
118 | 0 | #endif |
119 | 0 | } |
120 | | |
121 | | |
122 | | int |
123 | 0 | fs_fchown (FILE *file, int uid, int gid) { |
124 | | #ifdef _WIN32 |
125 | | errno = ENOSYS; |
126 | | return -1; |
127 | | #else |
128 | 0 | int fd = fileno(file); |
129 | 0 | return fchown(fd, (uid_t) uid, (gid_t) gid); |
130 | 0 | #endif |
131 | 0 | } |
132 | | |
133 | | |
134 | | int |
135 | 0 | fs_lchown (const char *path, int uid, int gid) { |
136 | | #ifdef _WIN32 |
137 | | errno = ENOSYS; |
138 | | return -1; |
139 | | #else |
140 | 0 | return lchown(path, (uid_t) uid, (gid_t) gid); |
141 | 0 | #endif |
142 | 0 | } |
143 | | |
144 | | |
145 | | size_t |
146 | 0 | fs_size (const char *path) { |
147 | 0 | size_t size; |
148 | 0 | FILE *file = fs_open(path, FS_OPEN_READ); |
149 | 0 | if (NULL == file) return -1; |
150 | 0 | fseek(file, 0, SEEK_END); |
151 | 0 | size = ftell(file); |
152 | 0 | fs_close(file); |
153 | 0 | return size; |
154 | 0 | } |
155 | | |
156 | | |
157 | | size_t |
158 | 2.05k | fs_fsize (FILE *file) { |
159 | | // store current position |
160 | 2.05k | unsigned long pos = ftell(file); |
161 | 2.05k | rewind(file); |
162 | 2.05k | fseek(file, 0, SEEK_END); |
163 | 2.05k | size_t size = ftell(file); |
164 | 2.05k | fseek(file, pos, SEEK_SET); |
165 | 2.05k | return size; |
166 | 2.05k | } |
167 | | |
168 | | |
169 | | char * |
170 | 2.05k | fs_read (const char *path) { |
171 | 2.05k | FILE *file = fs_open(path, FS_OPEN_READ); |
172 | 2.05k | if (NULL == file) return NULL; |
173 | 2.05k | char *data = fs_fread(file); |
174 | 2.05k | fclose(file); |
175 | 2.05k | return data; |
176 | 2.05k | } |
177 | | |
178 | | |
179 | | char * |
180 | 0 | fs_nread (const char *path, int len) { |
181 | 0 | FILE *file = fs_open(path, FS_OPEN_READ); |
182 | 0 | if (NULL == file) return NULL; |
183 | 0 | char *buffer = fs_fnread(file, len); |
184 | 0 | fs_close(file); |
185 | 0 | return buffer; |
186 | 0 | } |
187 | | |
188 | | |
189 | | char * |
190 | 2.05k | fs_fread (FILE *file) { |
191 | 2.05k | size_t fsize = fs_fsize(file); |
192 | 2.05k | return fs_fnread(file, fsize); |
193 | 2.05k | } |
194 | | |
195 | | |
196 | | char * |
197 | 2.05k | fs_fnread (FILE *file, int len) { |
198 | 2.05k | char *buffer = (char*) malloc(sizeof(char) * (len + 1)); |
199 | 2.05k | size_t n = fread(buffer, 1, len, file); |
200 | 2.05k | buffer[n] = '\0'; |
201 | 2.05k | return buffer; |
202 | 2.05k | } |
203 | | |
204 | | |
205 | | int |
206 | 0 | fs_write (const char *path, const char *buffer) { |
207 | 0 | return fs_nwrite(path, buffer, strlen(buffer)); |
208 | 0 | } |
209 | | |
210 | | |
211 | | int |
212 | 0 | fs_nwrite (const char *path, const char *buffer, int len) { |
213 | 0 | FILE *file = fs_open(path, FS_OPEN_WRITE); |
214 | 0 | if (NULL == file) return -1; |
215 | 0 | int result = fs_fnwrite(file, buffer, len); |
216 | 0 | fclose(file); |
217 | 0 | return result; |
218 | 0 | } |
219 | | |
220 | | |
221 | | int |
222 | 0 | fs_fwrite (FILE *file, const char *buffer) { |
223 | 0 | return fs_fnwrite(file, buffer, strlen(buffer)); |
224 | 0 | } |
225 | | |
226 | | |
227 | | int |
228 | 0 | fs_fnwrite (FILE *file, const char *buffer, int len) { |
229 | 0 | return (int) fwrite(buffer, 1, len, file); |
230 | 0 | } |
231 | | |
232 | | |
233 | | int |
234 | 0 | fs_mkdir (const char *path, int mode) { |
235 | | #ifdef _WIN32 |
236 | | return mkdir(path); |
237 | | #else |
238 | 0 | return mkdir(path, (mode_t) mode); |
239 | 0 | #endif |
240 | 0 | } |
241 | | |
242 | | |
243 | | int |
244 | 0 | fs_rmdir (const char *path) { |
245 | 0 | return rmdir(path); |
246 | 0 | } |
247 | | |
248 | | |
249 | | int |
250 | 2.05k | fs_exists (const char *path) { |
251 | 2.05k | struct stat b; |
252 | 2.05k | return stat(path, &b); |
253 | 2.05k | } |