summaryrefslogtreecommitdiffstats
path: root/makelist.c
blob: ed7cce948c2042337cef9e4500fc6d3d9a7ef6d2 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* 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 "extra-functions.h"
#include "common.h"
#include "data.h"
#include "variables.h"

int makelist_file_count;
unsigned long long makelist_current_size;

unsigned long long getUsedSizeViaDu(const char* path)
{
    char cmd[512];
    sprintf(cmd, "du -sk %s | awk '{ print $1 }'", path);

    FILE *fp;
    fp = __popen(cmd, "r");
    
    char str[512];
    fgets(str, sizeof(str), fp);
    __pclose(fp);

    unsigned long long size = atol(str);
    size *= 1024ULL;

    return size;
}

int add_item(const char* 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) < 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 generate_file_lists(const char* path) {
	DIR* d;
	struct dirent* de;
	struct stat st;
	char path2[255], filename[255];

	if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1 && strlen(path) >= 11 && strncmp(path, "/data/media", 11) == 0)
		return 0; // Skip /data/media

	// Make a copy of path in case the data in the pointer gets overwritten later
	strcpy(path2, path);

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

	while ((de = readdir(d)) != NULL)
	{
		if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
		{
			strcpy(filename, path2);
			strcat(filename, "/");
			strcat(filename, de->d_name);
			if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1 && strlen(filename) >= 11 && strncmp(filename, "/data/media", 11) == 0)
				continue; // Skip /data/media
			unsigned long long folder_size = getUsedSizeViaDu(filename);
			if (makelist_current_size + folder_size > MAX_ARCHIVE_SIZE) {
				if (generate_file_lists(filename) < 0)
					return -1;
			} else {
				strcat(filename, "/");
				if (add_item(filename) < 0)
					return -1;
				makelist_current_size += folder_size;
			}
		}
		else if (de->d_type == DT_REG)
		{
			if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 1 && strlen(path2) >= 11 && strncmp(path2, "/data/media", 11) == 0)
				continue; // Skip /data/media
			strcpy(filename, path2);
			strcat(filename, "/");
			strcat(filename, de->d_name);
			stat(filename, &st);

			if (makelist_current_size != 0 && makelist_current_size + st.st_size > MAX_ARCHIVE_SIZE) {
				makelist_file_count += 1;
				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);
		}
	}
	closedir(d);
	return 0;
}

int make_file_list(char* 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);
}