Skip to content

Commit

Permalink
fix tests to properly work with volumes & update volumefs implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
john-behm-bertelsmann committed Jan 24, 2023
1 parent 07d77a3 commit 7f37a5a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
15 changes: 11 additions & 4 deletions backupfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,23 @@ func NewTestPrefixFs(prefix string) *PrefixFs {
func NewTempdirPrefixFs(prefix string) *PrefixFs {
osFs := newOsFs()

binPath := os.Args[0]
tempDir := os.TempDir()
err := os.MkdirAll(tempDir, 0700)
if err != nil {
log.Fatalln(err)
}
volume := filepath.VolumeName(tempDir)
volumeFs := NewVolumeFs(volume, osFs)
tempDir = TrimVolume(tempDir)

volume := NewVolumeFs(filepath.VolumeName(binPath), osFs)
// remove volume from temp dir

prefix, err := afero.TempDir(volume, "", prefix)
prefix, err = afero.TempDir(volumeFs, tempDir, prefix)
if err != nil {
log.Fatalln(err)
}

return NewPrefixFs(prefix, volume)
return NewPrefixFs(prefix, volumeFs)
}

func NewTestBackupFs(basePrefix, backupPrefix string) (root, base, backup afero.Fs, backupFs *BackupFs) {
Expand Down
31 changes: 18 additions & 13 deletions volumefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,21 @@ type VolumeFs struct {
base afero.Fs
}

// the passed file path must not contain any os specific volume prefix.
// primarily no windows volumes like c:, d:, etc.
func (v *VolumeFs) prefixPath(name string) (string, error) {
name = filepath.Clean(name)

if v.volume == "" {
return name, nil
}

pathVolumeName := filepath.VolumeName(name)
if pathVolumeName == "" {
return filepath.Clean(filepath.Join(v.volume, name)), nil
volumePrefix := filepath.VolumeName(name)
if volumePrefix != "" {
return "", os.ErrNotExist
}

if pathVolumeName == v.volume {
return name, nil
}

// filepath contains an invalid volume name which we do not expect
// we also do not want to remove the volume and put our expected volume in
// front of the path name (for now)
return "", os.ErrNotExist
return filepath.Clean(filepath.Join(v.volume, name)), nil
}

func NewVolumeFs(volume string, fs afero.Fs) *VolumeFs {
Expand All @@ -61,6 +56,9 @@ func NewVolumeFs(volume string, fs afero.Fs) *VolumeFs {
// error, if any happens.
func (v *VolumeFs) Create(name string) (File, error) {
path, err := v.prefixPath(name)
if err != nil {
return nil, err
}

f, err := v.base.Create(path)
if f == nil {
Expand Down Expand Up @@ -200,7 +198,7 @@ func (v *VolumeFs) Chown(name string, uid, gid int) error {
return v.base.Chown(path, uid, gid)
}

//Chtimes changes the access and modification times of the named file
// Chtimes changes the access and modification times of the named file
func (v *VolumeFs) Chtimes(name string, atime, mtime time.Time) error {
path, err := v.prefixPath(name)
if err != nil {
Expand Down Expand Up @@ -233,7 +231,7 @@ func (v *VolumeFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {

}

//SymlinkIfPossible changes the access and modification times of the named file
// SymlinkIfPossible changes the access and modification times of the named file
func (v *VolumeFs) SymlinkIfPossible(oldname, newname string) error {
// links may be relative paths

Expand Down Expand Up @@ -297,3 +295,10 @@ func (v *VolumeFs) LchownIfPossible(name string, uid, gid int) error {
}
return &os.PathError{Op: "lchown", Path: name, Err: internal.ErrNoLchown}
}

// TrimVolume trims the volume prefix of a given filepath. C:\A\B\C -> \A\B\C
// highly OS-dependent. On unix systems there is no such thing as a volume path prefix.
func TrimVolume(filePath string) string {
volume := filepath.VolumeName(filePath)
return filePath[len(volume):]
}

0 comments on commit 7f37a5a

Please sign in to comment.