blob: 7b3b3eb119f414551cde4f87dbca655888cfac56 [file] [log] [blame]
Dees_Troy9df963c2012-09-26 08:58:12 -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 "common.h"
30#include "variables.h"
31#include "data.hpp"
32#include "makelist.hpp"
33#include "twrp-functions.hpp"
34
35int Makelist_File_Count;
36unsigned long long Makelist_Current_Size;
37
38int MakeList::Add_Item(string Item_Name) {
39 char actual_filename[255];
40 FILE *fp;
41
42 if (Makelist_File_Count > 999) {
43 LOGE("File count is too large!\n");
44 return -1;
45 }
46
47 sprintf(actual_filename, "/tmp/list/filelist%03i", Makelist_File_Count);
48
49 fp = fopen(actual_filename, "a");
50 if (fp == NULL) {
51 LOGE("Failed to open '%s'\n", actual_filename);
52 return -1;
53 }
54 if (fprintf(fp, "%s\n", Item_Name.c_str()) < 0) {
55 LOGE("Failed to write to '%s'\n", actual_filename);
56 return -1;
57 }
58 if (fclose(fp) != 0) {
59 LOGE("Failed to close '%s'\n", actual_filename);
60 return -1;
61 }
62 return 0;
63}
64
65int MakeList::Generate_File_Lists(string Path) {
66 DIR* d;
67 struct dirent* de;
68 struct stat st;
69 string FileName;
70 int has_data_media;
71
72 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
73 if (has_data_media == 1 && Path.size() >= 11 && strncmp(Path.c_str(), "/data/media", 11) == 0)
74 return 0; // Skip /data/media
75
76 d = opendir(Path.c_str());
77 if (d == NULL)
78 {
79 LOGE("error opening '%s'\n", Path.c_str());
80 return -1;
81 }
82
83 while ((de = readdir(d)) != NULL)
84 {
85 FileName = Path + "/";
86 FileName += de->d_name;
87 if (has_data_media == 1 && FileName.size() >= 11 && strncmp(FileName.c_str(), "/data/media", 11) == 0)
88 continue; // Skip /data/media
89 if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
90 {
91 unsigned long long folder_size = TWFunc::Get_Folder_Size(FileName, false);
92 if (Makelist_Current_Size + folder_size > MAX_ARCHIVE_SIZE) {
93 if (Generate_File_Lists(FileName) < 0)
94 return -1;
95 } else {
96 FileName += "/";
97 if (Add_Item(FileName) < 0)
98 return -1;
99 Makelist_Current_Size += folder_size;
100 }
101 }
102 else if (de->d_type == DT_REG || de->d_type == DT_LNK)
103 {
104 stat(FileName.c_str(), &st);
105
106 if (Makelist_Current_Size != 0 && Makelist_Current_Size + st.st_size > MAX_ARCHIVE_SIZE) {
107 Makelist_File_Count++;
108 Makelist_Current_Size = 0;
109 }
110 if (Add_Item(FileName) < 0)
111 return -1;
112 Makelist_Current_Size += st.st_size;
113 if (st.st_size > 2147483648LL)
114 LOGE("There is a file that is larger than 2GB in the file system\n'%s'\nThis file may not restore properly\n", FileName.c_str());
115 }
116 }
117 closedir(d);
118 return 0;
119}
120
121int MakeList::Make_File_List(string Path)
122{
123 Makelist_File_Count = 0;
124 Makelist_Current_Size = 0;
125 system("cd /tmp && rm -rf list");
126 system("cd /tmp && mkdir list");
127 if (Generate_File_Lists(Path) < 0) {
128 LOGE("Error generating file list\n");
129 return -1;
130 }
131 LOGI("Done, generated %i files.\n", (Makelist_File_Count + 1));
132 return (Makelist_File_Count + 1);
133}