-
Notifications
You must be signed in to change notification settings - Fork 12
/
fileaccess.cpp
426 lines (391 loc) · 9.42 KB
/
fileaccess.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <libgen.h>
#include "fileaccess.h"
FileAccess::FileAccess(service_conn_t fd)
:_fd(fd), _afc(NULL)
{
}
FileAccess::~FileAccess()
{
}
int FileAccess::connect()
{
mach_error_t status = AFCConnectionOpen(_fd, 0, &_afc);
if (status != MDERR_OK) {
return -1;
}
return 0;
}
int FileAccess::listDirectory(const char *dir_path)
{
if (_afc == NULL) {
return -1;
}
struct afc_directory *dir;
if (AFCDirectoryOpen(_afc, dir_path, &dir) != MDERR_OK) {
return -1;
}
char *d = NULL;
char path[256];
char tmbuf[64];
while(true) {
AFCDirectoryRead(_afc, dir, &d);
if (d == NULL) {
break;
}
if (strcmp(d, ".") == 0 || strcmp(d, "..") == 0) {
continue;
}
snprintf(path, sizeof(path), "%s/%s", dir_path, d);
CFMutableDictionaryRef file_dict = getFileInfo(path);
if (file_dict == NULL) {
continue;
}
//CFShow(file_dict);
CFStringRef ifmt = (CFStringRef)CFDictionaryGetValue(file_dict, CFSTR("st_ifmt"));
const char *ifmt_cstr = (CFStringCompare(ifmt, CFSTR("S_IFDIR"), kCFCompareLocalized) == kCFCompareEqualTo) ? "d" : "-";
CFStringRef size = (CFStringRef)CFDictionaryGetValue(file_dict,CFSTR("st_size"));
CFStringRef mtime = (CFStringRef)CFDictionaryGetValue(file_dict,CFSTR("st_mtime"));
time_t mtimel = atoll(CFStringGetCStringPtr(mtime, kCFStringEncodingMacRoman)) / 1000000000L;
struct tm *mtimetm = localtime(&mtimel);
strftime(tmbuf, sizeof tmbuf, "%b %d %H:%M", mtimetm);
/*
* not work in OS 10.10
printf("%s %16s %6s %s\n",
ifmt_cstr,
tmbuf,
CFStringGetCStringPtr(size, kCFStringEncodingMacRoman),
d
);
*/
printf("%s %16s %6d %s\n",
ifmt_cstr,
tmbuf,
CFStringGetIntValue(size),
d
);
CFRelease(file_dict);
}
AFCDirectoryClose(_afc, dir);
return 0;
}
int FileAccess::push(const char *local, const char *remote)
{
return copy(local, remote, false, true);
}
int FileAccess::pull(const char *remote, const char *local)
{
return copy(remote, local, true, false);
}
int FileAccess::copy(const char *from, const char *to, bool isfromdevice, bool istodevice)
{
if (_afc == NULL) {
return -1;
}
FileType from_file_type = getFileType(from, isfromdevice);
if (from_file_type == F_NOEXIST) {
printf("From path is no exist!\n");
return -1;
}
FileType to_file_type = getFileType(to, istodevice);
if (to_file_type == F_NOEXIST) {
if (getFileType(dirname((char *)to), istodevice) != F_DIR) {
printf("To path is not valid!\n");
return -1;
}
}
char *basepath = basename((char *)from);
char tmp[256];
if (from_file_type == F_FILE) {
if (to_file_type == F_DIR) {
snprintf(tmp, sizeof(tmp), "%s/%s", to, basepath);
return copyFile(from, tmp, isfromdevice, istodevice);
}
return copyFile(from, to, isfromdevice, istodevice);
} else {
if (to_file_type == F_FILE) {
printf("To path is file!\n");
return -1;
}
char *newtopath = (char *)to;
if (to_file_type == F_DIR) {
snprintf(tmp, sizeof(tmp), "%s/%s", to, basepath);
newtopath = tmp;;
}
if (this->mkdir(newtopath, istodevice) < 0) {
printf("mkdir <%s> fail!\n", newtopath);
return -1;
}
return copyDirectory(from, newtopath, isfromdevice, istodevice);
}
return 0;
}
int FileAccess::remove(const char *path)
{
if (_afc == NULL) {
return -1;
}
FileType file_type = getFileType(path, true);
if (file_type == F_NOEXIST) {
printf("Path is no exist!\n");
return -1;
}
int ret = 0;
if (file_type == F_DIR) {
char newpath[256];
char *d = NULL;
FDIR *dir = this->opendir(path, true);
if (dir == NULL) {
return -1;
}
while (true) {
d = this->readir(dir, true);
if (d == NULL) {
break;
}
if (strcmp(d, ".") == 0 || strcmp(d, "..") == 0) {
continue;
}
snprintf(newpath, sizeof(newpath), "%s/%s", path, d);
if (this->remove(newpath) != 0) {
ret = -1;
break;
}
}
this->closedir(dir, true);
}
if (ret == 0) {
return AFCRemovePath(_afc, path);
}
return ret;
}
int FileAccess::copyDirectory(const char *from, const char *to, bool isfromdevice, bool istodevice)
{
FDIR *dir = this->opendir(from, isfromdevice);
if (dir == NULL) {
return -1;
}
char nfrom[256], nto[256];
int ret = 0;
char *d = NULL;
while (true) {
d = this->readir(dir, isfromdevice);
if (d == NULL) {
break;
}
if (strcmp(d, ".") == 0 || strcmp(d, "..") == 0) {
continue;
}
snprintf(nfrom, sizeof(nfrom), "%s/%s", from, d);
FileType type = getFileType(nfrom, isfromdevice);
if (type == F_NOEXIST) {
continue;
}
snprintf(nto, sizeof(nto), "%s/%s", to, d);
if (type == F_DIR) {
if (this->mkdir(nto, istodevice) < 0) {
printf("mkdir <%s> fail!\n", nto);
ret = -1;
break;
}
if (copyDirectory(nfrom, nto, isfromdevice, istodevice) != 0) {
ret = -1;
break;
}
} else {
if (copyFile(nfrom, nto, isfromdevice, istodevice) != 0) {
ret = -1;
break;
}
}
}
this->closedir(dir, isfromdevice);
return ret;
}
int FileAccess::copyFile(const char *from, const char *to, bool isfromdevice, bool istodevice)
{
FFILE *from_fd = openfile(from, FM_READ, isfromdevice);
if (from_fd == NULL) {
printf("Open \"%s\" fail!\n", from);
return -1;
}
FFILE *to_fd = openfile(to, FM_WRITE, istodevice);
if (to_fd == NULL) {
printf("Open \"%s\" fail!\n", to);
closefile(from_fd, isfromdevice);
return -1;
}
//FIXME: increase bufsize, in OS 10.10 AFCFileRefRead read the whole file data once
int bufsize = 1024 * 1024 * 10;
char *buf = (char *)malloc(bufsize);
if (buf == NULL) {
closefile(to_fd, istodevice);
closefile(from_fd, isfromdevice);
return -1;
}
int ret = 0;
while (true) {
int readed = readfile(from_fd, buf, bufsize, isfromdevice);
if (readed == 0) {
break;
}
if (writefile(to_fd, buf, readed, istodevice) == 0) {
ret = -1;
break;
}
}
free(buf);
closefile(to_fd, istodevice);
closefile(from_fd, isfromdevice);
printf("Copy \"%s\" done\n", from);
return ret;
}
CFMutableDictionaryRef FileAccess::getFileInfo(const char *path)
{
struct afc_dictionary *file_info;
if (AFCFileInfoOpen(_afc, path, &file_info) != MDERR_OK) {
return NULL;
}
CFMutableDictionaryRef file_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
char *key = NULL, *value = NULL;
while(AFCKeyValueRead(file_info, &key, &value) == MDERR_OK) {
if (key == NULL || value == NULL) {
break;
}
CFStringRef ckey = CFStringCreateWithCString(NULL, key, kCFStringEncodingASCII);
CFStringRef cval = CFStringCreateWithCString(NULL, value, kCFStringEncodingASCII);
CFDictionarySetValue(file_dict, ckey, cval);
}
AFCKeyValueClose(file_info);
return file_dict;
}
FileAccess::FileType FileAccess::getFileType(const char *path, bool isdevice)
{
FileType type = F_ERROR;
if (isdevice) {
CFMutableDictionaryRef file_dict = getFileInfo(path);
if (file_dict == NULL) {
return F_NOEXIST;
}
CFStringRef ifmt = (CFStringRef)CFDictionaryGetValue(file_dict, CFSTR("st_ifmt"));
type = (CFStringCompare(ifmt, CFSTR("S_IFDIR"), kCFCompareLocalized) == kCFCompareEqualTo) ? F_DIR : F_FILE;
CFRelease(file_dict);
} else {
struct stat s;
if (stat(path, &s) != 0) {
return F_NOEXIST;
}
type = (s.st_mode & S_IFDIR) ? F_DIR : F_FILE;
}
return type;
}
int FileAccess::mkdir(const char *path, bool isdevice)
{
if (isdevice) {
if (AFCDirectoryCreate(_afc, path) != MDERR_OK) {
return -1;
}
} else {
if (::mkdir(path, 0755) < 0) {
return -1;
}
}
return 0;
}
FileAccess::FDIR * FileAccess::opendir(const char *path, bool isdevice)
{
if (isdevice) {
struct afc_directory *dir;
if (AFCDirectoryOpen(_afc, path, &dir) == MDERR_OK) {
return (FDIR *)dir;
}
} else {
return (FDIR *)::opendir(path);
}
return NULL;
}
char * FileAccess::readir(FDIR *dir, bool isdevice)
{
if (isdevice) {
char *d = NULL;
struct afc_directory *ndir = (struct afc_directory *)dir;
if (AFCDirectoryRead(_afc, ndir, &d) != MDERR_OK) {
return NULL;
}
return d;
} else {
DIR *ndir = (DIR *)dir;
struct dirent* dp = ::readdir(ndir);
if (dp == NULL) {
return NULL;
}
return dp->d_name;
}
return NULL;
}
int FileAccess::closedir(FDIR *dir, bool isdevice)
{
if (isdevice) {
return AFCDirectoryClose(_afc, (struct afc_directory *)dir);
} else {
return ::closedir((DIR *)dir);
}
return 0;
}
FileAccess::FFILE * FileAccess::openfile(const char *path, int mode, bool isdevice)
{
if (isdevice) {
afc_file_ref rfd;
int r = AFCFileRefOpen(_afc, path, mode, &rfd);
if (r != 0) {
return NULL;
}
return (FFILE *)rfd;
} else {
const char *m = (mode == FM_READ) ? "r" : "w";
FILE *fp = fopen(path, m);
return (FFILE *)fp;
}
return NULL;
}
size_t FileAccess::readfile(FFILE *fd, char *buf, size_t bufsize, bool isdevice)
{
if (isdevice) {
unsigned int readed = bufsize;
int ret = AFCFileRefRead(_afc, (afc_file_ref)fd, buf, &readed);
if (ret != MDERR_OK) {
return 0;
}
return readed;
} else {
return fread(buf, 1, bufsize, (FILE *)fd);
}
return 0;
}
size_t FileAccess::writefile(FFILE *fd, char *buf, size_t bufsize, bool isdevice)
{
if (isdevice) {
int ret = AFCFileRefWrite(_afc, (afc_file_ref)fd, buf, bufsize);
if (ret != MDERR_OK) {
return 0;
}
return 1;
} else {
return fwrite(buf, bufsize, 1, (FILE *)fd);
}
return 0;
}
int FileAccess::closefile(FFILE *fd, bool isdevice)
{
if (fd == NULL) {
return 0;
}
if (isdevice) {
return AFCFileRefClose(_afc, (afc_file_ref)fd);
} else {
return fclose((FILE *)fd);
}
return 0;
}