diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/idevicebackup2.c | 101 |
1 files changed, 78 insertions, 23 deletions
diff --git a/tools/idevicebackup2.c b/tools/idevicebackup2.c index be5a1a0..c822d7f 100644 --- a/tools/idevicebackup2.c +++ b/tools/idevicebackup2.c | |||
| @@ -192,6 +192,74 @@ static int mkdir_with_parents(const char *dir, int mode) | |||
| 192 | return res; | 192 | return res; |
| 193 | } | 193 | } |
| 194 | 194 | ||
| 195 | static int remove_file(const char* path) | ||
| 196 | { | ||
| 197 | int e = 0; | ||
| 198 | #ifdef WIN32 | ||
| 199 | if (!DeleteFile(path)) { | ||
| 200 | e = win32err_to_errno(GetLastError()); | ||
| 201 | } | ||
| 202 | #else | ||
| 203 | if (remove(path) < 0) { | ||
| 204 | e = errno; | ||
| 205 | } | ||
| 206 | #endif | ||
| 207 | return e; | ||
| 208 | } | ||
| 209 | |||
| 210 | static int remove_directory(const char* path) | ||
| 211 | { | ||
| 212 | int e = 0; | ||
| 213 | #ifdef WIN32 | ||
| 214 | if (!RemoveDirectory(path)) { | ||
| 215 | e = win32err_to_errno(GetLastError()); | ||
| 216 | } | ||
| 217 | #else | ||
| 218 | if (remove(path) < 0) { | ||
| 219 | e = errno; | ||
| 220 | } | ||
| 221 | #endif | ||
| 222 | return e; | ||
| 223 | } | ||
| 224 | |||
| 225 | static int rmdir_recursive(const char* path) | ||
| 226 | { | ||
| 227 | DIR* cur_dir = opendir(path); | ||
| 228 | if (cur_dir) { | ||
| 229 | struct dirent* ep; | ||
| 230 | while ((ep = readdir(cur_dir))) { | ||
| 231 | if ((strcmp(ep->d_name, ".") == 0) || (strcmp(ep->d_name, "..") == 0)) { | ||
| 232 | continue; | ||
| 233 | } | ||
| 234 | char *fpath = string_build_path(path, ep->d_name, NULL); | ||
| 235 | if (fpath) { | ||
| 236 | struct stat st; | ||
| 237 | if (stat(fpath, &st) == 0) { | ||
| 238 | int res = 0; | ||
| 239 | if (S_ISDIR(st.st_mode)) { | ||
| 240 | res = rmdir_recursive(fpath); | ||
| 241 | } else { | ||
| 242 | res = remove_file(fpath); | ||
| 243 | } | ||
| 244 | if (res != 0) { | ||
| 245 | free(fpath); | ||
| 246 | closedir(cur_dir); | ||
| 247 | return res; | ||
| 248 | } | ||
| 249 | } else { | ||
| 250 | free(fpath); | ||
| 251 | closedir(cur_dir); | ||
| 252 | return errno; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | free(fpath); | ||
| 256 | } | ||
| 257 | closedir(cur_dir); | ||
| 258 | } | ||
| 259 | |||
| 260 | return remove_directory(path); | ||
| 261 | } | ||
| 262 | |||
| 195 | static plist_t mobilebackup_factory_info_plist_new(const char* udid, lockdownd_client_t lockdown, afc_client_t afc) | 263 | static plist_t mobilebackup_factory_info_plist_new(const char* udid, lockdownd_client_t lockdown, afc_client_t afc) |
| 196 | { | 264 | { |
| 197 | /* gather data from lockdown */ | 265 | /* gather data from lockdown */ |
| @@ -1895,14 +1963,10 @@ checkpoint: | |||
| 1895 | free(str); | 1963 | free(str); |
| 1896 | char *oldpath = string_build_path(backup_directory, key, NULL); | 1964 | char *oldpath = string_build_path(backup_directory, key, NULL); |
| 1897 | 1965 | ||
| 1898 | #ifdef WIN32 | ||
| 1899 | if ((stat(newpath, &st) == 0) && S_ISDIR(st.st_mode)) | 1966 | if ((stat(newpath, &st) == 0) && S_ISDIR(st.st_mode)) |
| 1900 | RemoveDirectory(newpath); | 1967 | rmdir_recursive(newpath); |
| 1901 | else | 1968 | else |
| 1902 | DeleteFile(newpath); | 1969 | remove_file(newpath); |
| 1903 | #else | ||
| 1904 | remove(newpath); | ||
| 1905 | #endif | ||
| 1906 | if (rename(oldpath, newpath) < 0) { | 1970 | if (rename(oldpath, newpath) < 0) { |
| 1907 | printf("Renameing '%s' to '%s' failed: %s (%d)\n", oldpath, newpath, strerror(errno), errno); | 1971 | printf("Renameing '%s' to '%s' failed: %s (%d)\n", oldpath, newpath, strerror(errno), errno); |
| 1908 | errcode = errno_to_device_error(errno); | 1972 | errcode = errno_to_device_error(errno); |
| @@ -1951,27 +2015,18 @@ checkpoint: | |||
| 1951 | } | 2015 | } |
| 1952 | char *newpath = string_build_path(backup_directory, str, NULL); | 2016 | char *newpath = string_build_path(backup_directory, str, NULL); |
| 1953 | free(str); | 2017 | free(str); |
| 1954 | #ifdef WIN32 | ||
| 1955 | int res = 0; | 2018 | int res = 0; |
| 1956 | if ((stat(newpath, &st) == 0) && S_ISDIR(st.st_mode)) | 2019 | if ((stat(newpath, &st) == 0) && S_ISDIR(st.st_mode)) { |
| 1957 | res = RemoveDirectory(newpath); | 2020 | res = rmdir_recursive(newpath); |
| 1958 | else | 2021 | } else { |
| 1959 | res = DeleteFile(newpath); | 2022 | res = remove_file(newpath); |
| 1960 | if (!res) { | ||
| 1961 | int e = win32err_to_errno(GetLastError()); | ||
| 1962 | if (!suppress_warning) | ||
| 1963 | printf("Could not remove '%s': %s (%d)\n", newpath, strerror(e), e); | ||
| 1964 | errcode = errno_to_device_error(e); | ||
| 1965 | errdesc = strerror(e); | ||
| 1966 | } | 2023 | } |
| 1967 | #else | 2024 | if (res != 0) { |
| 1968 | if (remove(newpath) < 0) { | ||
| 1969 | if (!suppress_warning) | 2025 | if (!suppress_warning) |
| 1970 | printf("Could not remove '%s': %s (%d)\n", newpath, strerror(errno), errno); | 2026 | printf("Could not remove '%s': %s (%d)\n", newpath, strerror(res), res); |
| 1971 | errcode = errno_to_device_error(errno); | 2027 | errcode = errno_to_device_error(res); |
| 1972 | errdesc = strerror(errno); | 2028 | errdesc = strerror(res); |
| 1973 | } | 2029 | } |
| 1974 | #endif | ||
| 1975 | free(newpath); | 2030 | free(newpath); |
| 1976 | } | 2031 | } |
| 1977 | } | 2032 | } |
