tdrimg: add recover command
- recover reads old file versions from the sdcard image
This commit is contained in:
parent
2f81ee73e1
commit
f18176e3fa
1 changed files with 39 additions and 1 deletions
|
|
@ -239,7 +239,7 @@ def listvolumes(img):
|
||||||
return firstvolume
|
return firstvolume
|
||||||
|
|
||||||
|
|
||||||
def listdir(img, part, verbose=False):
|
def listdir(img, part, verbose=False, deleted=False):
|
||||||
print("Directory of {}:".format(part.name))
|
print("Directory of {}:".format(part.name))
|
||||||
slotno = 0
|
slotno = 0
|
||||||
done = False
|
done = False
|
||||||
|
|
@ -247,6 +247,8 @@ def listdir(img, part, verbose=False):
|
||||||
slot = getdirslot(img, part, slotno)
|
slot = getdirslot(img, part, slotno)
|
||||||
if (slot.flags & SlotFirst):
|
if (slot.flags & SlotFirst):
|
||||||
print(slot.name, slot.sizeBytes, slotno)
|
print(slot.name, slot.sizeBytes, slotno)
|
||||||
|
elif deleted and (slot.flags & SlotDeleted):
|
||||||
|
print(slot.name, slot.sizeBytes, slotno, slot.generation)
|
||||||
else:
|
else:
|
||||||
if verbose:
|
if verbose:
|
||||||
print(flags2str(slot.flags))
|
print(flags2str(slot.flags))
|
||||||
|
|
@ -272,6 +274,21 @@ def findfile(img, part, name):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def finddeleted(img, part, name, gen):
|
||||||
|
slotno = 0
|
||||||
|
done = False
|
||||||
|
while not done:
|
||||||
|
slot = getdirslot(img, part, slotno)
|
||||||
|
if slot.flags & SlotDeleted:
|
||||||
|
if slot.name == name and slot.generation == gen:
|
||||||
|
return slotno
|
||||||
|
slotno += 1
|
||||||
|
if (slot.flags & SlotEndScan) or (slotno >= part.dirSize):
|
||||||
|
done = True
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def readfile(img, part, slotno):
|
def readfile(img, part, slotno):
|
||||||
pos = part.startBlock * 512 + slotno * part.extentSize
|
pos = part.startBlock * 512 + slotno * part.extentSize
|
||||||
dirslot = getdirslot(img, part, slotno)
|
dirslot = getdirslot(img, part, slotno)
|
||||||
|
|
@ -317,6 +334,24 @@ def readfromimg(img, pathname,outfilepath):
|
||||||
f.write(data)
|
f.write(data)
|
||||||
|
|
||||||
|
|
||||||
|
def recoverfromimg(img, pathname, gen, outfilepath):
|
||||||
|
vol, filename = parsepath(img, pathname)
|
||||||
|
if vol is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
listdir(img, vol, deleted=True)
|
||||||
|
|
||||||
|
slotno = finddeleted(img, vol, filename, gen)
|
||||||
|
if slotno is None:
|
||||||
|
print("File", filename,"not found with generation no", gen)
|
||||||
|
return
|
||||||
|
|
||||||
|
data = readfile(img, vol, slotno)
|
||||||
|
|
||||||
|
with open(outfilepath, "wb") as f:
|
||||||
|
f.write(data)
|
||||||
|
|
||||||
|
|
||||||
def writetoimg(img, pathname, infilepath):
|
def writetoimg(img, pathname, infilepath):
|
||||||
vol, filename = parsepath(img, pathname)
|
vol, filename = parsepath(img, pathname)
|
||||||
if vol is None:
|
if vol is None:
|
||||||
|
|
@ -555,6 +590,9 @@ if __name__ == "__main__":
|
||||||
if sys.argv[1] == "get":
|
if sys.argv[1] == "get":
|
||||||
f = open(sys.argv[2], "rb")
|
f = open(sys.argv[2], "rb")
|
||||||
readfromimg(f, sys.argv[3], sys.argv[4])
|
readfromimg(f, sys.argv[3], sys.argv[4])
|
||||||
|
elif sys.argv[1] == "recover":
|
||||||
|
f = open(sys.argv[2], "rb")
|
||||||
|
recoverfromimg(f, sys.argv[3], int(sys.argv[4]), sys.argv[5])
|
||||||
elif sys.argv[1] == "put":
|
elif sys.argv[1] == "put":
|
||||||
imgfile = open(sys.argv[2], "r+b")
|
imgfile = open(sys.argv[2], "r+b")
|
||||||
infilepath = sys.argv[3]
|
infilepath = sys.argv[3]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue