Convert makelist to C++

This commit is contained in:
Dees_Troy
2012-09-26 08:58:12 -04:00
parent b46a684971
commit 9df963c9cd
5 changed files with 153 additions and 161 deletions

View File

@@ -31,7 +31,7 @@ LOCAL_SRC_FILES := \
LOCAL_SRC_FILES += \
extra-functions.c \
data.cpp \
makelist.c \
makelist.cpp \
partition.cpp \
partitionmanager.cpp \
mtdutils/mtdutils.c \

View File

@@ -1,156 +0,0 @@
/* 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 "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);
}

133
makelist.cpp Normal file
View File

@@ -0,0 +1,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);
}

View File

@@ -20,7 +20,21 @@
#ifndef _MAKELIST_HEADER
#define _MAKELIST_HEADER
int make_file_list(char* path);
#include <string>
#endif
using namespace std;
// Partition class
class MakeList
{
public:
static int Make_File_List(string Path);
private:
static int Add_Item(string Item_Name);
static int Generate_File_Lists(string Path);
};
#endif // _MAKELIST_HEADER

View File

@@ -38,10 +38,10 @@
#include "partitions.hpp"
#include "data.hpp"
#include "twrp-functions.hpp"
#include "makelist.hpp"
extern "C" {
#include "mtdutils/mtdutils.h"
#include "mtdutils/mounts.h"
#include "makelist.h"
#include "extra-functions.h"
}
@@ -103,6 +103,7 @@ bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
}
string mount_pt(full_line);
Mount_Point = mount_pt;
LOGI("Processing '%s'\n", mount_pt);
Backup_Path = Mount_Point;
index = Mount_Point.size();
while (index < line_len) {
@@ -1096,7 +1097,7 @@ bool TWPartition::Backup_Tar(string backup_folder) {
// This backup needs to be split into multiple archives
ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
sprintf(back_name, "%s", Backup_Path.c_str());
backup_count = make_file_list(back_name);
backup_count = MakeList::Make_File_List(back_name);
if (backup_count < 1) {
LOGE("Error generating file list!\n");
return false;