feat: Implement Move for files and directories

This commit is contained in:
Felicitas Pojtinger
2021-12-20 23:09:17 +01:00
parent ed1299260b
commit 2400568963
2 changed files with 13 additions and 2 deletions

View File

@@ -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) {

View File

@@ -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