Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* This program is free software; you can redistribute it and/or modify |
| 2 | * it under the terms of the GNU General Public License version 2 and |
| 3 | * only version 2 as published by the Free Software Foundation. |
| 4 | * |
| 5 | * This program is distributed in the hope that it will be useful, |
| 6 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 7 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 8 | * GNU General Public License for more details. |
| 9 | * |
| 10 | * You should have received a copy of the GNU General Public License |
| 11 | * along with this program; if not, write to the Free Software |
| 12 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 13 | * 02110-1301, USA. |
| 14 | * |
| 15 | * The code was written from scratch by Dees_Troy dees_troy at |
| 16 | * yahoo |
| 17 | * |
| 18 | * Copyright (c) 2012 |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <ctype.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <dirent.h> |
| 28 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 29 | #include "common.h" |
| 30 | #include "data.h" |
| 31 | #include "variables.h" |
| 32 | |
| 33 | int makelist_file_count; |
| 34 | unsigned long long makelist_current_size; |
| 35 | |
| 36 | unsigned long long getUsedSizeViaDu(const char* path) |
| 37 | { |
| 38 | char cmd[512]; |
| 39 | sprintf(cmd, "du -sk %s | awk '{ print $1 }'", path); |
| 40 | |
| 41 | FILE *fp; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 42 | fp = popen(cmd, "r"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
| 44 | char str[512]; |
| 45 | fgets(str, sizeof(str), fp); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 46 | pclose(fp); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | |
| 48 | unsigned long long size = atol(str); |
| 49 | size *= 1024ULL; |
| 50 | |
| 51 | return size; |
| 52 | } |
| 53 | |
| 54 | int add_item(const char* item_name) { |
| 55 | char actual_filename[255]; |
| 56 | FILE *fp; |
| 57 | |
| 58 | if (makelist_file_count > 999) { |
| 59 | LOGE("File count is too large!\n"); |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | sprintf(actual_filename, "/tmp/list/filelist%03i", makelist_file_count); |
| 64 | |
| 65 | fp = fopen(actual_filename, "a"); |
| 66 | if (fp == NULL) { |
| 67 | LOGE("Failed to open '%s'\n", actual_filename); |
| 68 | return -1; |
| 69 | } |
| 70 | if (fprintf(fp, "%s\n", item_name) < 0) { |
| 71 | LOGE("Failed to write to '%s'\n", actual_filename); |
| 72 | return -1; |
| 73 | } |
| 74 | if (fclose(fp) != 0) { |
| 75 | LOGE("Failed to close '%s'\n", actual_filename); |
| 76 | return -1; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | int generate_file_lists(const char* path) { |
| 82 | DIR* d; |
| 83 | struct dirent* de; |
| 84 | struct stat st; |
| 85 | char path2[255], filename[255]; |
| 86 | |
| 87 | if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1 && strlen(path) >= 11 && strncmp(path, "/data/media", 11) == 0) |
| 88 | return 0; // Skip /data/media |
| 89 | |
| 90 | // Make a copy of path in case the data in the pointer gets overwritten later |
| 91 | strcpy(path2, path); |
| 92 | |
| 93 | d = opendir(path2); |
| 94 | if (d == NULL) |
| 95 | { |
| 96 | LOGE("error opening '%s'\n", path2); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | while ((de = readdir(d)) != NULL) |
| 101 | { |
| 102 | if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) |
| 103 | { |
| 104 | strcpy(filename, path2); |
| 105 | strcat(filename, "/"); |
| 106 | strcat(filename, de->d_name); |
| 107 | if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1 && strlen(filename) >= 11 && strncmp(filename, "/data/media", 11) == 0) |
| 108 | continue; // Skip /data/media |
| 109 | unsigned long long folder_size = getUsedSizeViaDu(filename); |
| 110 | if (makelist_current_size + folder_size > MAX_ARCHIVE_SIZE) { |
| 111 | if (generate_file_lists(filename) < 0) |
| 112 | return -1; |
| 113 | } else { |
| 114 | strcat(filename, "/"); |
| 115 | if (add_item(filename) < 0) |
| 116 | return -1; |
| 117 | makelist_current_size += folder_size; |
| 118 | } |
| 119 | } |
| 120 | else if (de->d_type == DT_REG) |
| 121 | { |
| 122 | if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1 && strlen(path2) >= 11 && strncmp(path2, "/data/media", 11) == 0) |
| 123 | continue; // Skip /data/media |
| 124 | strcpy(filename, path2); |
| 125 | strcat(filename, "/"); |
| 126 | strcat(filename, de->d_name); |
| 127 | stat(filename, &st); |
| 128 | |
| 129 | if (makelist_current_size != 0 && makelist_current_size + st.st_size > MAX_ARCHIVE_SIZE) { |
| 130 | makelist_file_count += 1; |
| 131 | makelist_current_size = 0; |
| 132 | } |
| 133 | if (add_item(filename) < 0) |
| 134 | return -1; |
| 135 | makelist_current_size += st.st_size; |
| 136 | if (st.st_size > 2147483648LL) |
| 137 | LOGE("There is a file that is larger than 2GB in the file system\n'%s'\nThis file may not restore properly\n", filename); |
| 138 | } |
| 139 | } |
| 140 | closedir(d); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | int make_file_list(char* path) |
| 145 | { |
| 146 | makelist_file_count = 0; |
| 147 | makelist_current_size = 0; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 148 | system("cd /tmp && rm -rf list"); |
| 149 | system("cd /tmp && mkdir list"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 150 | if (generate_file_lists(path) < 0) { |
| 151 | LOGE("Error generating file list\n"); |
| 152 | return -1; |
| 153 | } |
| 154 | LOGI("Done, generated %i files.\n", (makelist_file_count + 1)); |
| 155 | return (makelist_file_count + 1); |
| 156 | } |