Skip to content

Commit ac557c9

Browse files
committed
support removing files and set file buffer
1 parent f5ea13f commit ac557c9

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

zip/shadow_zip.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include <dirent.h>
77
#include <assert.h>
88
#include <string.h>
9+
#include <fstream>
10+
#include <set>
11+
#include <algorithm>
912

1013
using namespace android;
1114

@@ -38,6 +41,8 @@ static void get_files(const char* _apk_patch_path, std::vector<std::string>& _fi
3841
closedir(dir);
3942
}
4043

44+
static bool is_illegal_path_char(char ch){return ch == '\r' || ch == '\n' || ch == '"' || ch == '\'';}
45+
4146
static int parse_apk(const char* _path, std::vector<ZipEntry*>& _all_entries)
4247
{
4348
off_t fileLength, seekStart;
@@ -322,6 +327,23 @@ int ShadowZip::init(const char* _patch_dir, const char* _sys_apk_file, ShadowZip
322327
MY_INFO("no apk patches:[%s/assets_bin_Data]", _patch_dir);
323328
return -1;
324329
}
330+
331+
//all removed files
332+
char removed_files_path[512] = {0};
333+
snprintf(removed_files_path, sizeof(removed_files_path), "%s/removed_files.txt", _patch_dir );
334+
std::set<std::string> removed_files;
335+
std::ifstream rmfiles_stream(removed_files_path);
336+
if (rmfiles_stream.good()){
337+
std::string line;
338+
while( std::getline( rmfiles_stream, line ) ) {
339+
line.erase(std::remove_if(line.begin(), line.end(), is_illegal_path_char), line.end());
340+
if (line.length() == 0){continue;}
341+
MY_INFO("remove file: [%s]", line.c_str());
342+
removed_files.insert(line);
343+
}
344+
rmfiles_stream.close();
345+
}
346+
325347

326348
//find all entries in patches
327349
std::vector<std::vector<ZipEntry*> > entries_in_zip_file(global_data->all_files_.size());
@@ -337,7 +359,7 @@ int ShadowZip::init(const char* _patch_dir, const char* _sys_apk_file, ShadowZip
337359
return -1;
338360
}
339361
for(int j = 0; j < zip_entries.size(); j++)
340-
{
362+
{
341363
ZipEntry* entry = zip_entries[j];
342364
entry->mUserData1 = i;
343365
std::string filename = entry->getFileName();
@@ -347,6 +369,11 @@ int ShadowZip::init(const char* _patch_dir, const char* _sys_apk_file, ShadowZip
347369
clean_file_entries_map(entries_in_zip_file);
348370
return -1;
349371
}
372+
373+
if (removed_files.find(filename) != removed_files.end()){
374+
MY_LOG("rm file:%s in %s", filename.c_str(), zip_path.c_str());
375+
continue;
376+
}
350377
filename_2_entry[filename] = entry;
351378
}
352379
}
@@ -370,7 +397,11 @@ int ShadowZip::init(const char* _patch_dir, const char* _sys_apk_file, ShadowZip
370397
for(int i = 0; i < apk_entries.size(); i++)
371398
{
372399
ZipEntry* entry = apk_entries[i];
373-
std::string name(entry->getFileName());
400+
std::string name(entry->getFileName());
401+
if (removed_files.find(name) != removed_files.end()){
402+
MY_LOG("rm file:%s in apk", name.c_str());
403+
continue;
404+
}
374405
std::map<std::string, ZipEntry*>::iterator it = filename_2_entry.find(name);
375406
if (it != filename_2_entry.end()){ entry = it->second; }
376407
entry->mUserData2 = 1;
@@ -642,6 +673,8 @@ FILE* ShadowZip::prepare_file(int _file_index)
642673
}
643674
MY_METHOD("prepare_file %s -> 0x%08zx", path.c_str(), (size_t)fp);
644675
fp_array_[_file_index] = fp;
676+
677+
setvbuf( fp , NULL , _IOFBF , 1024);
645678
return fp;
646679
}
647680

0 commit comments

Comments
 (0)