Skip to content

Commit

Permalink
fix mk_diff
Browse files Browse the repository at this point in the history
- now a child image relative locator contains the correct relative path to its parent (=relative to itself)
- adds a generic relative path calculator to utils
  • Loading branch information
maxpat78 committed Aug 5, 2024
1 parent a267c2d commit 3d035e3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 18 additions & 1 deletion FATtools/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: cp1252 -*-
import io, struct, os
import io, struct, os, re
from FATtools.debug import log
DEBUG=int(os.getenv('FATTOOLS_DEBUG', '0'))

Expand Down Expand Up @@ -182,3 +182,20 @@ def raw2chs(t):
def roundMB(n):
"Round n at MiB"
return (n+(1<<20)-1) // (1<<20) * (1<<20)

def calc_rel_path(base, child):
"returns the path of base relative to child"
base_parts = re.split(r'[\\/]+', os.path.abspath(base))
child_parts = re.split(r'[\\/]+', os.path.abspath(child))
# strips common subpath, if any
i=0
while base_parts[i] == child_parts[i]: i += 1
# returns base if they don't share anything
if not i: return base
n = len(child_parts) - 1 - i # counts path separators
relpath = ''
while n:
relpath += '..\\'
n -= 1
relpath += '\\'.join(base_parts[i:])
return relpath
4 changes: 2 additions & 2 deletions FATtools/vhdutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
DEBUG=int(os.getenv('FATTOOLS_DEBUG', '0'))
import FATtools.utils as utils
from FATtools.debug import log
from FATtools.utils import myfile
from FATtools.utils import myfile, calc_rel_path


MAX_VHD_SIZE = 2040<<30 # Windows 11 won't mount bigger VHDs
Expand Down Expand Up @@ -828,7 +828,7 @@ def mk_diff(name, base, overwrite='no', sector=512):
f = myfile(name, 'wb')
f.write(ima.footer.pack()) # stores footer copy

rel_base = os.path.relpath(base, os.path.splitdrive(base)[0])
rel_base = calc_rel_path(base, name) # gets the path of base image relative to its child
if rel_base[0] != '.': rel_base = '.\\'+rel_base
rel_base = rel_base.encode('utf_16_le')
abs_base = os.path.abspath(base).encode('utf_16_le')
Expand Down

0 comments on commit 3d035e3

Please sign in to comment.