summaryrefslogtreecommitdiffstats
path: root/makelist.cpp
blob: 7b3b3eb119f414551cde4f87dbca655888cfac56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * The code was written from scratch by Dees_Troy dees_troy at
 * yahoo
 *
 * Copyright (c) 2012
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

#include "common.h"
#include "variables.h"
#include "data.hpp"
#include "makelist.hpp"
#include "twrp-functions.hpp"

int Makelist_File_Count;
unsigned long long Makelist_Current_Size;

int MakeList::Add_Item(string Item_Name) {
	char actual_filename[255];
	FILE *fp;

	if (Makelist_File_Count > 999) {
		LOGE("File count is too large!\n");
		return -1;
	}

	sprintf(actual_filename, "/tmp/list/filelist%03i", Makelist_File_Count);

	fp = fopen(actual_filename, "a");
	if (fp == NULL) {
		LOGE("Failed to open '%s'\n", actual_filename);
		return -1;
	}
	if (fprintf(fp, "%s\n", Item_Name.c_str()) < 0) {
		LOGE("Failed to write to '%s'\n", actual_filename);
		return -1;
	}
	if (fclose(fp) != 0) {
		LOGE("Failed to close '%s'\n", actual_filename);
		return -1;
	}
	return 0;
}

int MakeList::Generate_File_Lists(string Path) {
	DIR* d;
	struct dirent* de;
	struct stat st;
	string FileName;
	int has_data_media;

	DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
	if (has_data_media == 1 && Path.size() >= 11 && strncmp(Path.c_str(), "/data/media", 11) == 0)
		return 0; // Skip /data/media

	d = opendir(Path.c_str());
	if (d == NULL)
	{
		LOGE("error opening '%s'\n", Path.c_str());
		return -1;
	}

	while ((de = readdir(d)) != NULL)
	{
		FileName = Path + "/";
		FileName += de->d_name;
		if (has_data_media == 1 && FileName.size() >= 11 && strncmp(FileName.c_str(), "/data/media", 11) == 0)
				continue; // Skip /data/media
		if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
		{
			unsigned long long folder_size = TWFunc::Get_Folder_Size(FileName, false);
			if (Makelist_Current_Size + folder_size > MAX_ARCHIVE_SIZE) {
				if (Generate_File_Lists(FileName) < 0)
					return -1;
			} else {
				FileName += "/";
				if (Add_Item(FileName) < 0)
					return -1;
				Makelist_Current_Size += folder_size;
			}
		}
		else if (de->d_type == DT_REG || de->d_type == DT_LNK)
		{
			stat(FileName.c_str(), &st);

			if (Makelist_Current_Size != 0 && Makelist_Current_Size + st.st_size > MAX_ARCHIVE_SIZE) {
				Makelist_File_Count++;
				Makelist_Current_Size = 0;
			}
			if (Add_Item(FileName) < 0)
				return -1;
			Makelist_Current_Size += st.st_size;
			if (st.st_size > 2147483648LL)
				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());
		}
	}
	closedir(d);
	return 0;
}

int MakeList::Make_File_List(string Path)
{
	Makelist_File_Count = 0;
	Makelist_Current_Size = 0;
	system("cd /tmp && rm -rf list");
	system("cd /tmp && mkdir list");
	if (Generate_File_Lists(Path) < 0) {
		LOGE("Error generating file list\n");
		return -1;
	}
	LOGI("Done, generated %i files.\n", (Makelist_File_Count + 1));
	return (Makelist_File_Count + 1);
}