summaryrefslogtreecommitdiffstats
path: root/tools/idevicebackup2.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/idevicebackup2.c')
-rw-r--r--tools/idevicebackup2.c75
1 files changed, 55 insertions, 20 deletions
diff --git a/tools/idevicebackup2.c b/tools/idevicebackup2.c
index 6d4785b..02611a1 100644
--- a/tools/idevicebackup2.c
+++ b/tools/idevicebackup2.c
@@ -2,8 +2,8 @@
2 * idevicebackup2.c 2 * idevicebackup2.c
3 * Command line interface to use the device's backup and restore service 3 * Command line interface to use the device's backup and restore service
4 * 4 *
5 * Copyright (c) 2010-2018 Nikias Bassen All Rights Reserved.
5 * Copyright (c) 2009-2010 Martin Szulecki All Rights Reserved. 6 * Copyright (c) 2009-2010 Martin Szulecki All Rights Reserved.
6 * Copyright (c) 2010 Nikias Bassen All Rights Reserved.
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public 9 * modify it under the terms of the GNU Lesser General Public
@@ -238,7 +238,12 @@ static int remove_directory(const char* path)
238 return e; 238 return e;
239} 239}
240 240
241static int rmdir_recursive(const char* path) 241struct entry {
242 char *name;
243 struct entry *next;
244};
245
246static void scan_directory(const char *path, struct entry **files, struct entry **directories)
242{ 247{
243 DIR* cur_dir = opendir(path); 248 DIR* cur_dir = opendir(path);
244 if (cur_dir) { 249 if (cur_dir) {
@@ -249,31 +254,61 @@ static int rmdir_recursive(const char* path)
249 } 254 }
250 char *fpath = string_build_path(path, ep->d_name, NULL); 255 char *fpath = string_build_path(path, ep->d_name, NULL);
251 if (fpath) { 256 if (fpath) {
252 struct stat st; 257 if (ep->d_type & DT_DIR) {
253 if (stat(fpath, &st) == 0) { 258 struct entry *ent = malloc(sizeof(struct entry));
254 int res = 0; 259 if (!ent) return;
255 if (S_ISDIR(st.st_mode)) { 260 ent->name = fpath;
256 res = rmdir_recursive(fpath); 261 ent->next = *directories;
257 } else { 262 *directories = ent;
258 res = remove_file(fpath); 263 scan_directory(fpath, files, directories);
259 } 264 fpath = NULL;
260 if (res != 0) {
261 free(fpath);
262 closedir(cur_dir);
263 return res;
264 }
265 } else { 265 } else {
266 free(fpath); 266 struct entry *ent = malloc(sizeof(struct entry));
267 closedir(cur_dir); 267 if (!ent) return;
268 return errno; 268 ent->name = fpath;
269 ent->next = *files;
270 *files = ent;
271 fpath = NULL;
269 } 272 }
270 } 273 }
271 free(fpath);
272 } 274 }
273 closedir(cur_dir); 275 closedir(cur_dir);
274 } 276 }
277}
278
279static int rmdir_recursive(const char* path)
280{
281 int res = 0;
282 struct entry *files = NULL;
283 struct entry *directories = NULL;
284 struct entry *ent;
285
286 ent = malloc(sizeof(struct entry));
287 if (!ent) return ENOMEM;
288 ent->name = strdup(path);
289 ent->next = NULL;
290 directories = ent;
291
292 scan_directory(path, &files, &directories);
293
294 ent = files;
295 while (ent) {
296 struct entry *del = ent;
297 res = remove_file(ent->name);
298 free(ent->name);
299 ent = ent->next;
300 free(del);
301 }
302 ent = directories;
303 while (ent) {
304 struct entry *del = ent;
305 res = remove_directory(ent->name);
306 free(ent->name);
307 ent = ent->next;
308 free(del);
309 }
275 310
276 return remove_directory(path); 311 return res;
277} 312}
278 313
279static char* get_uuid() 314static char* get_uuid()