blob: ed7cce948c2042337cef9e4500fc6d3d9a7ef6d2 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* 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
29#include "extra-functions.h"
30#include "common.h"
31#include "data.h"
32#include "variables.h"
33
34int makelist_file_count;
35unsigned long long makelist_current_size;
36
37unsigned long long getUsedSizeViaDu(const char* path)
38{
39 char cmd[512];
40 sprintf(cmd, "du -sk %s | awk '{ print $1 }'", path);
41
42 FILE *fp;
43 fp = __popen(cmd, "r");
44
45 char str[512];
46 fgets(str, sizeof(str), fp);
47 __pclose(fp);
48
49 unsigned long long size = atol(str);
50 size *= 1024ULL;
51
52 return size;
53}
54
55int add_item(const char* item_name) {
56 char actual_filename[255];
57 FILE *fp;
58
59 if (makelist_file_count > 999) {
60 LOGE("File count is too large!\n");
61 return -1;
62 }
63
64 sprintf(actual_filename, "/tmp/list/filelist%03i", makelist_file_count);
65
66 fp = fopen(actual_filename, "a");
67 if (fp == NULL) {
68 LOGE("Failed to open '%s'\n", actual_filename);
69 return -1;
70 }
71 if (fprintf(fp, "%s\n", item_name) < 0) {
72 LOGE("Failed to write to '%s'\n", actual_filename);
73 return -1;
74 }
75 if (fclose(fp) != 0) {
76 LOGE("Failed to close '%s'\n", actual_filename);
77 return -1;
78 }
79 return 0;
80}
81
82int generate_file_lists(const char* path) {
83 DIR* d;
84 struct dirent* de;
85 struct stat st;
86 char path2[255], filename[255];
87
88 if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1 && strlen(path) >= 11 && strncmp(path, "/data/media", 11) == 0)
89 return 0; // Skip /data/media
90
91 // Make a copy of path in case the data in the pointer gets overwritten later
92 strcpy(path2, path);
93
94 d = opendir(path2);
95 if (d == NULL)
96 {
97 LOGE("error opening '%s'\n", path2);
98 return -1;
99 }
100
101 while ((de = readdir(d)) != NULL)
102 {
103 if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
104 {
105 strcpy(filename, path2);
106 strcat(filename, "/");
107 strcat(filename, de->d_name);
108 if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1 && strlen(filename) >= 11 && strncmp(filename, "/data/media", 11) == 0)
109 continue; // Skip /data/media
110 unsigned long long folder_size = getUsedSizeViaDu(filename);
111 if (makelist_current_size + folder_size > MAX_ARCHIVE_SIZE) {
112 if (generate_file_lists(filename) < 0)
113 return -1;
114 } else {
115 strcat(filename, "/");
116 if (add_item(filename) < 0)
117 return -1;
118 makelist_current_size += folder_size;
119 }
120 }
121 else if (de->d_type == DT_REG)
122 {
123 if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1 && strlen(path2) >= 11 && strncmp(path2, "/data/media", 11) == 0)
124 continue; // Skip /data/media
125 strcpy(filename, path2);
126 strcat(filename, "/");
127 strcat(filename, de->d_name);
128 stat(filename, &st);
129
130 if (makelist_current_size != 0 && makelist_current_size + st.st_size > MAX_ARCHIVE_SIZE) {
131 makelist_file_count += 1;
132 makelist_current_size = 0;
133 }
134 if (add_item(filename) < 0)
135 return -1;
136 makelist_current_size += st.st_size;
137 if (st.st_size > 2147483648LL)
138 LOGE("There is a file that is larger than 2GB in the file system\n'%s'\nThis file may not restore properly\n", filename);
139 }
140 }
141 closedir(d);
142 return 0;
143}
144
145int make_file_list(char* path)
146{
147 makelist_file_count = 0;
148 makelist_current_size = 0;
149 __system("cd /tmp && rm -rf list");
150 __system("cd /tmp && mkdir list");
151 if (generate_file_lists(path) < 0) {
152 LOGE("Error generating file list\n");
153 return -1;
154 }
155 LOGI("Done, generated %i files.\n", (makelist_file_count + 1));
156 return (makelist_file_count + 1);
157}