Skip to content

Commit

Permalink
[fix] File deletion with --commit-overlay did not work
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmlnkn committed Apr 29, 2024
1 parent 9e213f9 commit 76820c4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
19 changes: 10 additions & 9 deletions ratarmount.py
Original file line number Diff line number Diff line change
Expand Up @@ -1629,14 +1629,21 @@ def cli(rawArgs: Optional[List[str]] = None) -> None:
deletionList = os.path.join(tmpFolder, "deletions.lst")
appendList = os.path.join(tmpFolder, "append.lst")

def addToDeletionFile(deletionListFile, pathRelativeToRoot: str):
# Delete with and without leading slash because GNU tar matches exactly while
# ratarmount does not discern between these two cases.
deletionListFile.write(f"{pathRelativeToRoot}\0")
deletionListFile.write(f"/{pathRelativeToRoot}\0")
deletionListFile.write(f"./{pathRelativeToRoot}\0")

databasePath = os.path.join(args.write_overlay, WritableFolderMountSource.hiddenDatabaseName)
if os.path.exists(databasePath):
uriPath = urllib.parse.quote(databasePath)
sqlConnection = sqlite3.connect(f"file:{uriPath}?mode=ro", uri=True)

with open(deletionList, 'at', encoding=args.encoding) as file:
with open(deletionList, 'at', encoding=args.encoding) as deletionListFile:
for path, name in sqlConnection.execute("SELECT path,name FROM files WHERE deleted == 1;"):
file.write(f"{path}/{name}\0")
addToDeletionFile(deletionListFile, f"{path}/{name}".lstrip('/'))

# Delete all files to be replaced with other files
with open(deletionList, 'at', encoding=args.encoding) as deletionListFile, open(
Expand All @@ -1661,13 +1668,7 @@ def cli(rawArgs: Optional[List[str]] = None) -> None:
pathRelativeToRoot = f"{dirpath}/{name}".lstrip('/')
if pathRelativeToRoot in toBeIgnored:
continue

# Delete with and without leading slash because GNU tar matches exactly while
# ratarmount does not discern between these two cases.
deletionListFile.write(f"{pathRelativeToRoot}\0")
deletionListFile.write(f"/{pathRelativeToRoot}\0")
deletionListFile.write(f"./{pathRelativeToRoot}\0")

addToDeletionFile(deletionListFile, pathRelativeToRoot)
appendListFile.write(f"{pathRelativeToRoot}\0")

# Append empty folders
Expand Down
56 changes: 56 additions & 0 deletions tests/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,61 @@ checkWriteOverlayWithSymbolicLinks()
}


checkWriteOverlayCommitDelete()
{
local tmpFolder;
tmpFolder="$( mktemp -d )" || returnError "$LINENO" 'Failed to create temporary directory'
mkdir "$tmpFolder/overlay"
TMP_FILES_TO_CLEANUP+=( "$tmpFolder" )

local archive='single-nested-folder.tar'
cp "tests/$archive" "$tmpFolder/"
archive="$tmpFolder/$archive"
TMP_FILES_TO_CLEANUP+=( "$archive" "$archive.index.sqlite" )

[[ $( tar -tvlf "$archive" | wc -l ) -eq 2 ]] || returnError "$LINENO" 'Expected two entries in TAR'

rm -f ratarmount.{stdout,stderr}.log

local mountFolder="$tmpFolder/mounted"
mkdir "$mountFolder"
MOUNT_POINTS_TO_CLEANUP+=( "$mountFolder" )

local overlayFolder="$tmpFolder/overlay"
TMP_FILES_TO_CLEANUP+=( "$overlayFolder" "$overlayFolder/.ratarmount.overlay.sqlite" )

local args=( -P "$parallelization" -c --write-overlay "$overlayFolder" "$archive" "$mountFolder" )
{
runAndCheckRatarmount "${args[@]}"
if [[ -z "$( find "$mountFolder" -mindepth 1 2>/dev/null )" ]]; then returnError "$LINENO" 'Expected files in mount point'; fi
} || returnError "$LINENO" "$RATARMOUNT_CMD ${args[*]}"

verifyCheckSum "$mountFolder" 'foo/fighter/ufo' 'tests/single-nested-folder.tar' 2709a3348eb2c52302a7606ecf5860bc ||
returnError "$LINENO" 'Mismatching checksum'

# Delete file
'rm' "$mountFolder/foo/fighter/ufo" || returnError "$LINENO" 'Failed to delete ufo file'
overlayIndex="$overlayFolder/.ratarmount.overlay.sqlite"
[[ -f "$overlayIndex" ]] || returnError "$LINENO" "Expected $overlayIndex to be created"

funmount "$mountFolder"

args=( --commit-overlay "${args[@]}" )
{
echo commit | $RATARMOUNT_CMD "${args[@]}" >ratarmount.stdout.log 2>ratarmount.stderr.log.tmp
! 'grep' -C 5 -Ei '(warn|error)' ratarmount.stdout.log ratarmount.stderr.log ||
returnError "$LINENO" "Found warnings while executing: $RATARMOUNT_CMD ${args[*]}"
} || returnError "$LINENO" "$RATARMOUNT_CMD ${args[*]}"

tar -tvlf "$archive"
[[ $( tar -tvlf "$archive" | wc -l ) -eq 1 ]] || returnError "$LINENO" 'Expected one less entry in TAR'

cleanup

echoerr "[${FUNCNAME[0]}] Tested successfully file modifications for overlay files."
}


checkSymbolicLinkRecursion()
{
rm -f ratarmount.{stdout,stderr}.log
Expand Down Expand Up @@ -1907,6 +1962,7 @@ checkSymbolicLinkRecursion || returnError "$LINENO" 'Symbolic link recursion fai
checkWriteOverlayWithSymbolicLinks || returnError "$LINENO" 'Write overlay tests with symbolic links failed!'
checkWriteOverlayWithNewFiles || returnError "$LINENO" 'Write overlay tests failed!'
checkWriteOverlayWithArchivedFiles || returnError "$LINENO" 'Write overlay tests for archive files failed!'
checkWriteOverlayCommitDelete || returnError "$LINENO" 'Write overlay committing deletions failed!'

checkTruncated tests/truncated.tar foo/foo 5753d2a2da40d04ad7f3cc7a024b6e90

Expand Down

0 comments on commit 76820c4

Please sign in to comment.