forked from sisong/HDiffPatch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hpatch_dir_listener.h
242 lines (224 loc) · 10.5 KB
/
hpatch_dir_listener.h
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//hpatch_dir_listener.h
// patch dir listener
//
/*
This is the HDiffPatch copyright.
Copyright (c) 2018-2019 HouSisong All Rights Reserved.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef HPatch_dir_listener_h
#define HPatch_dir_listener_h
#include "file_for_patch.h"
#include "dirDiffPatch/dir_patch/dir_patch.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct IHPatchDirListener {
IDirPatchListener base;
void* listenerImport;
hpatch_BOOL (*patchBegin) (struct IHPatchDirListener* listener,TDirPatcher* dirPatcher);
hpatch_BOOL (*patchFinish)(struct IHPatchDirListener* listener,hpatch_BOOL isPatchSuccess);
} IHPatchDirListener;
//IDirPatchListener
static hpatch_BOOL _makeNewDir(IDirPatchListener* listener,const char* newDir){
return hpatch_makeNewDir(newDir);
}
static hpatch_BOOL _copySameFile(IDirPatchListener* listener,const char* oldFileName,
const char* newFileName,hpatch_ICopyDataListener* copyListener){
return TDirPatcher_copyFile(oldFileName,newFileName,copyListener);
}
static hpatch_BOOL _openNewFile(IDirPatchListener* listener,hpatch_TFileStreamOutput* out_curNewFile,
const char* newFileName,hpatch_StreamPos_t newFileSize){
return hpatch_TFileStreamOutput_open(out_curNewFile,newFileName,newFileSize);
}
static hpatch_BOOL _closeNewFile(IDirPatchListener* listener,hpatch_TFileStreamOutput* curNewFile){
return hpatch_TFileStreamOutput_close(curNewFile);
}
//IHPatchDirListener
static hpatch_BOOL _dirPatchBegin(IHPatchDirListener* listener,TDirPatcher* dirPatcher){
listener->listenerImport=dirPatcher;
return hpatch_TRUE;
}
static hpatch_BOOL _dirPatchFinish(IHPatchDirListener* listener,hpatch_BOOL isPatchSuccess){
TDirPatcher* dirPatcher=(TDirPatcher*)listener->listenerImport;
{//ExecuteFile
size_t i;
size_t count=TDirPatcher_getNewExecuteFileCount(dirPatcher);
for (i=0; i<count; ++i) {
const char* executeFileName=TDirPatcher_getNewExecuteFileByIndex(dirPatcher,i);
if (!hpatch_setIsExecuteFile(executeFileName)){
printf("WARNING: can't set Execute tag to new file \"");
hpatch_printPath_utf8(executeFileName); printf("\"\n");
}
}
}
return hpatch_TRUE;
}
static IHPatchDirListener defaultPatchDirlistener={{0,_makeNewDir,_copySameFile,_openNewFile,_closeNewFile},
0,_dirPatchBegin,_dirPatchFinish};
//IDirPatchListener
static hpatch_BOOL _tempDir_copySameFile(IDirPatchListener* listener,const char* oldFileName,
const char* newFileName,hpatch_ICopyDataListener* copyListener){
//checksum same file
//not copy now
if (copyListener==0) return hpatch_TRUE;
return TDirPatcher_readFile(oldFileName,copyListener);
}
//IHPatchDirListener
static hpatch_BOOL _tempDirPatchBegin(IHPatchDirListener* self,TDirPatcher* dirPatcher){
self->listenerImport=dirPatcher;
assert(dirPatcher->dirDiffInfo.oldPathIsDir&&dirPatcher->dirDiffInfo.newPathIsDir);
return hpatch_TRUE;
}
static hpatch_BOOL _isPathNotExist(const char* pathName){
hpatch_TPathType type;
if (pathName==0) return hpatch_FALSE;
if (!hpatch_getPathStat(pathName,&type,0)) return hpatch_FALSE;
return (kPathType_notExist==type);
}
static hpatch_BOOL _tryRemovePath(const char* pathName){
if (pathName==0) return hpatch_TRUE;
if (_isPathNotExist(pathName)) return hpatch_TRUE;
if (hpatch_getIsDirName(pathName))
return hpatch_removeDir(pathName);
else
return hpatch_removeFile(pathName);
}
static hpatch_BOOL _tempDirPatchFinish(IHPatchDirListener* self,hpatch_BOOL isPatchSuccess){
hpatch_BOOL result=hpatch_TRUE;
TDirPatcher* dirPatcher=(TDirPatcher*)self->listenerImport;
size_t i;
hpatch_BOOL isInitSameRefError=isPatchSuccess?(!TDirPatcher_initOldSameRefCount(dirPatcher)):hpatch_FALSE;
if (isInitSameRefError)
result=hpatch_FALSE;
if (isPatchSuccess && (!isInitSameRefError)){
//move(+ some must copy) same to newTempDir from oldDir;
for (i=dirPatcher->dirDiffHead.sameFilePairCount; i>0; --i) {
size_t sameIndex=i-1;
const char* oldPath;
const char* newPath=TDirPatcher_getNewPathBySameIndex(dirPatcher,sameIndex);
if (newPath==0) { result=hpatch_FALSE; continue; }
oldPath=TDirPatcher_getOldPathBySameIndex(dirPatcher,sameIndex);
if (oldPath==0) { result=hpatch_FALSE; continue; }
if (TDirPatcher_oldSameRefCount(dirPatcher,sameIndex)>1){//copy old to new
if (!TDirPatcher_copyFile(oldPath,newPath,0)){
result=hpatch_FALSE;
fprintf(stderr,"can't copy new file to newTempDir from same old file \"");
hpatch_printStdErrPath_utf8(newPath); fprintf(stderr,"\" ERROR!\n");
}
}else{
if (!hpatch_moveFile(oldPath,newPath)){//move old to new
result=hpatch_FALSE;
fprintf(stderr,"can't move new file to newTempDir from same old file \"");
hpatch_printStdErrPath_utf8(newPath); fprintf(stderr,"\" ERROR!\n");
}
}
TDirPatcher_decOldSameRefCount(dirPatcher,sameIndex);
}
TDirPatcher_finishOldSameRefCount(dirPatcher);
//delete file in oldPathList; //WARNING
//delete dir in oldPathList; //not check
for (i=dirPatcher->dirDiffHead.oldPathCount; i>0; --i) {
size_t oldPathIndex=i-1;
const char* oldPath=TDirPatcher_getOldPathByIndex(dirPatcher,oldPathIndex);
if (oldPath==0) continue;
if (!hpatch_getIsDirName(oldPath)){
if (!_tryRemovePath(oldPath)){
printf("WARNING: can't remove old file \"");
hpatch_printPath_utf8(oldPath); printf("\"\n");
}
}else{
hpatch_removeDir(oldPath);
}
}
//move all files and dir in newTempDir to oldDir;
for (i=0; i<dirPatcher->dirDiffHead.newPathCount; ++i) {//make dir to old
size_t newPathIndex=i;
const char* newPath=TDirPatcher_getNewPathByIndex(dirPatcher,newPathIndex);
if (newPath==0) { result=hpatch_FALSE; continue; }
if (hpatch_getIsDirName(newPath)){
const char* oldDir=TDirPatcher_getOldPathByNewPath(dirPatcher,newPath);
if (oldDir==0) { result=hpatch_FALSE; continue; }
if (!hpatch_makeNewDir(oldDir)) { result=hpatch_FALSE; continue; }
}
}
for (i=dirPatcher->dirDiffHead.newPathCount; i>0; --i) {//move files to old and remove dir
size_t newPathIndex=i-1;
const char* newPath=TDirPatcher_getNewPathByIndex(dirPatcher,newPathIndex);
if (newPath==0) { result=hpatch_FALSE; continue; }
if (hpatch_getIsDirName(newPath)){
hpatch_removeDir(newPath);
}else{
const char* oldPath=TDirPatcher_getOldPathByNewPath(dirPatcher,newPath);
if (oldPath==0) { result=hpatch_FALSE; continue; }
hpatch_removeFile(oldPath);//overwrite
if (!hpatch_moveFile(newPath,oldPath)){//move new to old
result=hpatch_FALSE;
fprintf(stderr,"can't move new file to oldDirectory \"");
hpatch_printStdErrPath_utf8(newPath); fprintf(stderr,"\" ERROR!\n");
continue;
}
}
}
{//ExecuteFile
size_t i;
size_t count=TDirPatcher_getNewExecuteFileCount(dirPatcher);
for (i=0; i<count; ++i) {
const char* executeFileName_new=TDirPatcher_getNewExecuteFileByIndex(dirPatcher,i);
const char* executeFileName=TDirPatcher_getOldPathByNewPath(dirPatcher,executeFileName_new);
if (!hpatch_setIsExecuteFile(executeFileName)){
printf("WARNING: can't set Execute tag to new file \"");
hpatch_printPath_utf8(executeFileName); printf("\"\n");
}
}
}
}
{ //remove all temp file and dir
for (i=dirPatcher->dirDiffHead.newPathCount; i>0; --i) {
size_t newPathIndex=i-1;
const char* newPath=TDirPatcher_getNewPathByIndex(dirPatcher,newPathIndex);
_tryRemovePath(newPath);
}
{//check remove newTempDir result
const char* newTempDir=TDirPatcher_getNewPathRoot(dirPatcher);
result=result && _isPathNotExist(newTempDir);
}
}
return result;
}
// 1. patch new ref to newTempDir,
// make new dir to newTempDir
// checksum same file
// 2. if patch ok then {
// move(+ some must copy) same to newTempDir from oldDir;
// delete file in oldPathList; //WARNING
// delete dir in oldPathList; //not check
// move all files and dir in newTempDir to oldDir;
// delete newTempDir; }
// if patch error then {
// delelte all in newTempDir;//not check
// delete newTempDir; }
static IHPatchDirListener tempDirPatchListener={{&tempDirPatchListener,_makeNewDir,_tempDir_copySameFile,
_openNewFile,_closeNewFile},
0,_tempDirPatchBegin,_tempDirPatchFinish};
#ifdef __cplusplus
}
#endif
#endif