Skip to content

Commit

Permalink
Add more fs methods to Filesystem interface
Browse files Browse the repository at this point in the history
Interface is extended as a part of ext4 improvements effort
diskfs#9 (comment)

Also, Remove method is introduced for all supported filesystems.
  • Loading branch information
aol-nnov committed Nov 9, 2024
1 parent f66ea89 commit 305d4c1
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 6 deletions.
28 changes: 26 additions & 2 deletions filesystem/ext4/ext4.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,9 @@ func Read(file util.File, size, start, sectorsize int64) (*FileSystem, error) {
}, nil
}

// interface guard
var _ filesystem.FileSystem = (*FileSystem)(nil)

// Type returns the type code for the filesystem. Always returns filesystem.TypeExt4
func (fs *FileSystem) Type() filesystem.Type {
return filesystem.TypeExt4
Expand All @@ -699,6 +702,22 @@ func (fs *FileSystem) Mkdir(p string) error {
return err
}

// creates a filesystem node (file, device special file, or named pipe) named pathname,
// with attributes specified by mode and dev
func (fs *FileSystem) Mknod(path string, mode uint32, dev int) error {
return filesystem.ErrNotImplemented
}

// creates a new link (also known as a hard link) to an existing file.
func (fs *FileSystem) Link(oldpath string, newpath string) error {
return filesystem.ErrNotImplemented
}

// creates a symbolic link named linkpath which contains the string target.
func (fs *FileSystem) Symlink(oldpath string, newpath string) error {
return filesystem.ErrNotImplemented
}

// ReadDir return the contents of a given directory in a given filesystem.
//
// Returns a slice of os.FileInfo with all of the entries in the directory.
Expand Down Expand Up @@ -808,12 +827,17 @@ func (fs *FileSystem) Label() string {
return fs.superblock.volumeLabel
}

// Rm remove file or directory at path.
// FIXME: API backwards compatibility
func (fs *FileSystem) Rm(p string) error {
return fs.Remove(p)
}

// Removes file or directory at path.
// If path is directory, it only will remove if it is empty.
// If path is a file, it will remove the file.
// Will not remove any parents.
// Error if the file does not exist or is not an empty directory
func (fs *FileSystem) Rm(p string) error {
func (fs *FileSystem) Remove(p string) error {
parentDir, entry, err := fs.getEntryAndParent(p)
if err != nil {
return err
Expand Down
23 changes: 23 additions & 0 deletions filesystem/fat32/fat32.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ func (fs *FileSystem) writeFat() error {
return nil
}

// interface guard
var _ filesystem.FileSystem = (*FileSystem)(nil)

// Type returns the type code for the filesystem. Always returns filesystem.TypeFat32
func (fs *FileSystem) Type() filesystem.Type {
return filesystem.TypeFat32
Expand All @@ -485,6 +488,22 @@ func (fs *FileSystem) Mkdir(p string) error {
return err
}

// creates a filesystem node (file, device special file, or named pipe) named pathname,
// with attributes specified by mode and dev
func (fs *FileSystem) Mknod(path string, mode uint32, dev int) error {
return filesystem.ErrNotSupported
}

// creates a new link (also known as a hard link) to an existing file.
func (fs *FileSystem) Link(oldpath string, newpath string) error {
return filesystem.ErrNotSupported
}

// creates a symbolic link named linkpath which contains the string target.
func (fs *FileSystem) Symlink(oldpath string, newpath string) error {
return filesystem.ErrNotSupported
}

// ReadDir return the contents of a given directory in a given filesystem.
//
// Returns a slice of os.FileInfo with all of the entries in the directory.
Expand Down Expand Up @@ -606,6 +625,10 @@ func (fs *FileSystem) OpenFile(p string, flag int) (filesystem.File, error) {
}, nil
}

func (fs *FileSystem) Remove(p string) error {
return filesystem.ErrNotImplemented
}

// Label get the label of the filesystem from the secial file in the root directory.
// The label stored in the boot sector is ignored to mimic Windows behavior which
// only stores and reads the label from the special file in the root directory.
Expand Down
23 changes: 19 additions & 4 deletions filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,40 @@
package filesystem

import (
"errors"
"os"
)

var (
ErrNotSupported = errors.New("method not supported by this filesystem")
ErrNotImplemented = errors.New("method not implemented (patches are welcome)")
)

// FileSystem is a reference to a single filesystem on a disk
type FileSystem interface {
// Type return the type of filesystem
Type() Type
// Mkdir make a directory
Mkdir(string) error
Mkdir(path string) error
// creates a filesystem node (file, device special file, or named pipe) named pathname,
// with attributes specified by mode and dev
Mknod(path string, mode uint32, dev int) error
// creates a new link (also known as a hard link) to an existing file.
Link(oldpath string, newpath string) error
// creates a symbolic link named linkpath which contains the string target.
Symlink(oldpath string, newpath string) error
// ReadDir read the contents of a directory
ReadDir(string) ([]os.FileInfo, error)
ReadDir(path string) ([]os.FileInfo, error)
// OpenFile open a handle to read or write to a file
OpenFile(string, int) (File, error)
OpenFile(path string, flag int) (File, error)
// removes the named file or (empty) directory.
Remove(path string) error
// Label get the label for the filesystem, or "" if none. Be careful to trim it, as it may contain
// leading or following whitespace. The label is passed as-is and not cleaned up at all.
Label() string
// SetLabel changes the label on the writable filesystem. Different file system may hav different
// length constraints.
SetLabel(string) error
SetLabel(label string) error
}

// Type represents the type of disk this is
Expand Down
26 changes: 26 additions & 0 deletions filesystem/iso9660/iso9660.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ func Read(file util.File, size, start, blocksize int64) (*FileSystem, error) {
return fs, nil
}

// interface guard
var _ filesystem.FileSystem = (*FileSystem)(nil)

// Type returns the type code for the filesystem. Always returns filesystem.TypeFat32
func (fsm *FileSystem) Type() filesystem.Type {
return filesystem.TypeISO9660
Expand All @@ -305,6 +308,22 @@ func (fsm *FileSystem) Mkdir(p string) error {
return err
}

// creates a filesystem node (file, device special file, or named pipe) named pathname,
// with attributes specified by mode and dev
func (fs *FileSystem) Mknod(path string, mode uint32, dev int) error {
return filesystem.ErrNotSupported
}

// creates a new link (also known as a hard link) to an existing file.
func (fs *FileSystem) Link(oldpath string, newpath string) error {
return filesystem.ErrNotSupported
}

// creates a symbolic link named linkpath which contains the string target.
func (fs *FileSystem) Symlink(oldpath string, newpath string) error {
return filesystem.ErrNotSupported
}

// ReadDir return the contents of a given directory in a given filesystem.
//
// Returns a slice of os.FileInfo with all of the entries in the directory.
Expand Down Expand Up @@ -414,6 +433,13 @@ func (fsm *FileSystem) OpenFile(p string, flag int) (filesystem.File, error) {
return f, nil
}

func (fsm *FileSystem) Remove(p string) error {
if fsm.workspace == "" {
return filesystem.ErrNotSupported
}
return os.Remove(path.Join(fsm.workspace, p))
}

// readDirectory - read directory entry on iso only (not workspace)
func (fsm *FileSystem) readDirectory(p string) ([]*directoryEntry, error) {
var (
Expand Down
26 changes: 26 additions & 0 deletions filesystem/squashfs/squashfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ func Read(file util.File, size, start, blocksize int64) (*FileSystem, error) {
return fs, nil
}

// interface guard
var _ filesystem.FileSystem = (*FileSystem)(nil)

// Type returns the type code for the filesystem. Always returns filesystem.TypeFat32
func (fs *FileSystem) Type() filesystem.Type {
return filesystem.TypeSquashfs
Expand Down Expand Up @@ -276,6 +279,22 @@ func (fs *FileSystem) Mkdir(p string) error {
return err
}

// creates a filesystem node (file, device special file, or named pipe) named pathname,
// with attributes specified by mode and dev
func (fs *FileSystem) Mknod(path string, mode uint32, dev int) error {
return filesystem.ErrNotSupported
}

// creates a new link (also known as a hard link) to an existing file.
func (fs *FileSystem) Link(oldpath string, newpath string) error {
return filesystem.ErrNotSupported
}

// creates a symbolic link named linkpath which contains the string target.
func (fs *FileSystem) Symlink(oldpath string, newpath string) error {
return filesystem.ErrNotSupported
}

// ReadDir return the contents of a given directory in a given filesystem.
//
// Returns a slice of os.FileInfo with all of the entries in the directory.
Expand Down Expand Up @@ -379,6 +398,13 @@ func (fs *FileSystem) OpenFile(p string, flag int) (filesystem.File, error) {
return f, nil
}

func (fs *FileSystem) Remove(p string) error {
if fs.workspace == "" {
return filesystem.ErrNotSupported
}
return os.Remove(path.Join(fs.workspace, p))
}

// readDirectory - read directory entry on squashfs only (not workspace)
func (fs *FileSystem) readDirectory(p string) ([]*directoryEntry, error) {
// use the root inode to find the location of the root direectory in the table
Expand Down

0 comments on commit 305d4c1

Please sign in to comment.