From 2400568963ed490178933d3b615de11aff98fe7f Mon Sep 17 00:00:00 2001 From: Felicitas Pojtinger Date: Mon, 20 Dec 2021 23:09:17 +0100 Subject: [PATCH] feat: Implement `Move` for files and directories --- internal/fs/filesystem.go | 2 +- pkg/operations/move.go | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/internal/fs/filesystem.go b/internal/fs/filesystem.go index 2941bc6..1859201 100644 --- a/internal/fs/filesystem.go +++ b/internal/fs/filesystem.go @@ -130,7 +130,7 @@ func (f *FileSystem) RemoveAll(path string) error { func (f *FileSystem) Rename(oldname, newname string) error { log.Println("FileSystem.Rename", oldname, newname) - panic(ErrNotImplemented) + return f.writeOps.Move(oldname, newname) } func (f *FileSystem) Stat(name string) (os.FileInfo, error) { diff --git a/pkg/operations/move.go b/pkg/operations/move.go index 7b0bfec..35ca76e 100644 --- a/pkg/operations/move.go +++ b/pkg/operations/move.go @@ -3,6 +3,7 @@ package operations import ( "archive/tar" "context" + "path" "path/filepath" "strings" @@ -50,6 +51,16 @@ func (o *Operations) Move(from string, to string) error { } headersToMove = append(headersToMove, dbhdr) + // Prevent moving from relative to absolute path + if path.IsAbs(to) && !path.IsAbs(dbhdr.Name) { + to = strings.TrimPrefix(to, "/") + } + + // Ignore no-op move operation + if from == to { + return nil + } + // If the header refers to a directory, get it's children if dbhdr.Typeflag == tar.TypeDir { dbhdrs, err := o.metadata.Metadata.GetHeaderChildren(context.Background(), from) @@ -69,7 +80,7 @@ func (o *Operations) Move(from string, to string) error { } hdr.Size = 0 // Don't try to seek after the record - hdr.Name = strings.TrimSuffix(to, "/") + strings.TrimPrefix(hdr.Name, strings.TrimSuffix(from, "/")) + hdr.Name = path.Join(to, strings.TrimPrefix(strings.TrimPrefix(dbhdr.Name, "/"), strings.TrimPrefix(from, "/"))) hdr.PAXRecords[records.STFSRecordVersion] = records.STFSRecordVersion1 hdr.PAXRecords[records.STFSRecordAction] = records.STFSRecordActionUpdate hdr.PAXRecords[records.STFSRecordReplacesName] = dbhdr.Name