-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathupdatebase.py
executable file
·75 lines (70 loc) · 3.41 KB
/
updatebase.py
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
#!/usr/bin/python3
import os, shutil
from log import logger
def aufs_remove(basefs):
try:
if os.path.isdir(basefs):
shutil.rmtree(basefs)
elif os.path.isfile(basefs):
os.remove(basefs)
except Exception as e:
logger.error(e)
def aufs_clean(basefs):
# clean the aufs mark
allfiles = os.listdir(basefs)
for onefile in allfiles:
if onefile[:4] == ".wh.":
aufs_remove(basefs + "/" + onefile)
def aufs_merge(image, basefs):
allfiles = os.listdir(image)
if ".wh..wh..opq" in allfiles:
#this is a new dir in image, remove the dir in basefs with the same name, and copy it to basefs
shutil.rmtree(basefs)
shutil.copytree(image, basefs, symlinks=True)
aufs_clean(basefs)
return
for onefile in allfiles:
try:
if onefile[:7] == ".wh..wh":
# aufs mark, but not white-out mark, ignore it
continue
elif onefile[:4] == ".wh.":
# white-out mark, remove the file in basefs
aufs_remove(basefs + "/" + onefile[4:])
elif os.path.isdir(image + "/" + onefile):
if os.path.isdir(basefs + "/" + onefile):
# this is a dir in image and basefs, merge it
aufs_merge(image + "/" + onefile, basefs + "/" + onefile)
elif os.path.isfile(basefs + "/" + onefile):
# this is a dir in image but file in basefs, remove the file and copy the dir to basefs
os.remove(basefs + "/" + onefile)
shutil.copytree(image + "/" + onefile, basefs + "/" + onefile, symlinks=True)
elif not os.path.exists(basefs + "/" + onefile):
# this is a dir in image but not exists in basefs, copy the dir to basefs
shutil.copytree(image + "/" + onefile, basefs + "/" + onefile, symlinks=True)
else:
# error
logger.error(basefs + "/" + onefile + " cause error")
elif os.path.isfile(image + "/" + onefile):
if os.path.isdir(basefs + "/" + onefile):
# this is a file in image but dir in basefs, remove the dir and copy the file to basefs
shutil.rmtree(basefs + "/" + onefile)
shutil.copy2(image+ "/" + onefile, basefs + "/" + onefile, follow_symlinks=False)
elif os.path.isfile(basefs + "/" + onefile):
# this is a file in image and basefs, remove the file and copy the file to basefs
os.remove(basefs + "/" + onefile)
shutil.copy2(image+ "/" + onefile, basefs + "/" + onefile, follow_symlinks=False)
elif not os.path.isdir(basefs + "/" + onefile):
# this is a file in image but not exists in basefs, copy the file to basefs
shutil.copy2(image+ "/" + onefile, basefs + "/" + onefile, follow_symlinks=False)
else:
# error
logger.error(basefs + "/" + onefile + " cause error")
except Exception as e:
logger.error(e)
def aufs_update_base(image, basefs):
if not os.path.isdir(basefs):
logger.error("basefs:%s doesn't exists" % basefs)
if not os.path.isdir(image):
logger.error("image:%s doesn't exists" % image)
aufs_merge(image, basefs)