Skip to content

Commit 3b148db

Browse files
committed
Add more fs methods to Filesystem interface
Interface is extended as a part of ext4 improvements effort #9 (comment) Also, Remove method is intruduced for all supported filesystems.
1 parent f66ea89 commit 3b148db

File tree

5 files changed

+120
-6
lines changed

5 files changed

+120
-6
lines changed

filesystem/ext4/ext4.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,9 @@ func Read(file util.File, size, start, sectorsize int64) (*FileSystem, error) {
684684
}, nil
685685
}
686686

687+
// interface guard
688+
var _ filesystem.FileSystem = (*FileSystem)(nil)
689+
687690
// Type returns the type code for the filesystem. Always returns filesystem.TypeExt4
688691
func (fs *FileSystem) Type() filesystem.Type {
689692
return filesystem.TypeExt4
@@ -699,6 +702,22 @@ func (fs *FileSystem) Mkdir(p string) error {
699702
return err
700703
}
701704

705+
// creates a filesystem node (file, device special file, or named pipe) named pathname,
706+
// with attributes specified by mode and dev
707+
func (fs *FileSystem) Mknod(path string, mode uint32, dev int) error {
708+
return filesystem.ErrNotImplemented
709+
}
710+
711+
// creates a new link (also known as a hard link) to an existing file.
712+
func (fs *FileSystem) Link(oldpath string, newpath string) error {
713+
return filesystem.ErrNotImplemented
714+
}
715+
716+
// creates a symbolic link named linkpath which contains the string target.
717+
func (fs *FileSystem) Symlink(oldpath string, newpath string) error {
718+
return filesystem.ErrNotImplemented
719+
}
720+
702721
// ReadDir return the contents of a given directory in a given filesystem.
703722
//
704723
// Returns a slice of os.FileInfo with all of the entries in the directory.
@@ -808,12 +827,17 @@ func (fs *FileSystem) Label() string {
808827
return fs.superblock.volumeLabel
809828
}
810829

811-
// Rm remove file or directory at path.
830+
// FIXME: API backwards compatibility
831+
func (fs *FileSystem) Rm(p string) error {
832+
return fs.Remove(p)
833+
}
834+
835+
// Removes file or directory at path.
812836
// If path is directory, it only will remove if it is empty.
813837
// If path is a file, it will remove the file.
814838
// Will not remove any parents.
815839
// Error if the file does not exist or is not an empty directory
816-
func (fs *FileSystem) Rm(p string) error {
840+
func (fs *FileSystem) Remove(p string) error {
817841
parentDir, entry, err := fs.getEntryAndParent(p)
818842
if err != nil {
819843
return err

filesystem/fat32/fat32.go

+23
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,9 @@ func (fs *FileSystem) writeFat() error {
470470
return nil
471471
}
472472

473+
// interface guard
474+
var _ filesystem.FileSystem = (*FileSystem)(nil)
475+
473476
// Type returns the type code for the filesystem. Always returns filesystem.TypeFat32
474477
func (fs *FileSystem) Type() filesystem.Type {
475478
return filesystem.TypeFat32
@@ -485,6 +488,22 @@ func (fs *FileSystem) Mkdir(p string) error {
485488
return err
486489
}
487490

491+
// creates a filesystem node (file, device special file, or named pipe) named pathname,
492+
// with attributes specified by mode and dev
493+
func (fs *FileSystem) Mknod(path string, mode uint32, dev int) error {
494+
return filesystem.ErrNotSupported
495+
}
496+
497+
// creates a new link (also known as a hard link) to an existing file.
498+
func (fs *FileSystem) Link(oldpath string, newpath string) error {
499+
return filesystem.ErrNotSupported
500+
}
501+
502+
// creates a symbolic link named linkpath which contains the string target.
503+
func (fs *FileSystem) Symlink(oldpath string, newpath string) error {
504+
return filesystem.ErrNotSupported
505+
}
506+
488507
// ReadDir return the contents of a given directory in a given filesystem.
489508
//
490509
// Returns a slice of os.FileInfo with all of the entries in the directory.
@@ -606,6 +625,10 @@ func (fs *FileSystem) OpenFile(p string, flag int) (filesystem.File, error) {
606625
}, nil
607626
}
608627

628+
func (fs *FileSystem) Remove(p string) error {
629+
return filesystem.ErrNotImplemented
630+
}
631+
609632
// Label get the label of the filesystem from the secial file in the root directory.
610633
// The label stored in the boot sector is ignored to mimic Windows behavior which
611634
// only stores and reads the label from the special file in the root directory.

filesystem/filesystem.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,40 @@
33
package filesystem
44

55
import (
6+
"errors"
67
"os"
78
)
89

10+
var (
11+
ErrNotSupported = errors.New("method not supported by this filesystem")
12+
ErrNotImplemented = errors.New("method not implemented (patches are welcome)")
13+
)
14+
915
// FileSystem is a reference to a single filesystem on a disk
1016
type FileSystem interface {
1117
// Type return the type of filesystem
1218
Type() Type
1319
// Mkdir make a directory
14-
Mkdir(string) error
20+
Mkdir(path string) error
21+
// creates a filesystem node (file, device special file, or named pipe) named pathname,
22+
// with attributes specified by mode and dev
23+
Mknod(path string, mode uint32, dev int) error
24+
// creates a new link (also known as a hard link) to an existing file.
25+
Link(oldpath string, newpath string) error
26+
// creates a symbolic link named linkpath which contains the string target.
27+
Symlink(oldpath string, newpath string) error
1528
// ReadDir read the contents of a directory
16-
ReadDir(string) ([]os.FileInfo, error)
29+
ReadDir(path string) ([]os.FileInfo, error)
1730
// OpenFile open a handle to read or write to a file
18-
OpenFile(string, int) (File, error)
31+
OpenFile(path string, flag int) (File, error)
32+
// removes the named file or (empty) directory.
33+
Remove(path string) error
1934
// Label get the label for the filesystem, or "" if none. Be careful to trim it, as it may contain
2035
// leading or following whitespace. The label is passed as-is and not cleaned up at all.
2136
Label() string
2237
// SetLabel changes the label on the writable filesystem. Different file system may hav different
2338
// length constraints.
24-
SetLabel(string) error
39+
SetLabel(label string) error
2540
}
2641

2742
// Type represents the type of disk this is

filesystem/iso9660/iso9660.go

+26
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ func Read(file util.File, size, start, blocksize int64) (*FileSystem, error) {
282282
return fs, nil
283283
}
284284

285+
// interface guard
286+
var _ filesystem.FileSystem = (*FileSystem)(nil)
287+
285288
// Type returns the type code for the filesystem. Always returns filesystem.TypeFat32
286289
func (fsm *FileSystem) Type() filesystem.Type {
287290
return filesystem.TypeISO9660
@@ -305,6 +308,22 @@ func (fsm *FileSystem) Mkdir(p string) error {
305308
return err
306309
}
307310

311+
// creates a filesystem node (file, device special file, or named pipe) named pathname,
312+
// with attributes specified by mode and dev
313+
func (fs *FileSystem) Mknod(path string, mode uint32, dev int) error {
314+
return filesystem.ErrNotSupported
315+
}
316+
317+
// creates a new link (also known as a hard link) to an existing file.
318+
func (fs *FileSystem) Link(oldpath string, newpath string) error {
319+
return filesystem.ErrNotSupported
320+
}
321+
322+
// creates a symbolic link named linkpath which contains the string target.
323+
func (fs *FileSystem) Symlink(oldpath string, newpath string) error {
324+
return filesystem.ErrNotSupported
325+
}
326+
308327
// ReadDir return the contents of a given directory in a given filesystem.
309328
//
310329
// Returns a slice of os.FileInfo with all of the entries in the directory.
@@ -414,6 +433,13 @@ func (fsm *FileSystem) OpenFile(p string, flag int) (filesystem.File, error) {
414433
return f, nil
415434
}
416435

436+
func (fsm *FileSystem) Remove(p string) error {
437+
if fsm.workspace == "" {
438+
return filesystem.ErrNotSupported
439+
}
440+
return os.Remove(path.Join(fsm.workspace, p))
441+
}
442+
417443
// readDirectory - read directory entry on iso only (not workspace)
418444
func (fsm *FileSystem) readDirectory(p string) ([]*directoryEntry, error) {
419445
var (

filesystem/squashfs/squashfs.go

+26
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ func Read(file util.File, size, start, blocksize int64) (*FileSystem, error) {
229229
return fs, nil
230230
}
231231

232+
// interface guard
233+
var _ filesystem.FileSystem = (*FileSystem)(nil)
234+
232235
// Type returns the type code for the filesystem. Always returns filesystem.TypeFat32
233236
func (fs *FileSystem) Type() filesystem.Type {
234237
return filesystem.TypeSquashfs
@@ -276,6 +279,22 @@ func (fs *FileSystem) Mkdir(p string) error {
276279
return err
277280
}
278281

282+
// creates a filesystem node (file, device special file, or named pipe) named pathname,
283+
// with attributes specified by mode and dev
284+
func (fs *FileSystem) Mknod(path string, mode uint32, dev int) error {
285+
return filesystem.ErrNotSupported
286+
}
287+
288+
// creates a new link (also known as a hard link) to an existing file.
289+
func (fs *FileSystem) Link(oldpath string, newpath string) error {
290+
return filesystem.ErrNotSupported
291+
}
292+
293+
// creates a symbolic link named linkpath which contains the string target.
294+
func (fs *FileSystem) Symlink(oldpath string, newpath string) error {
295+
return filesystem.ErrNotSupported
296+
}
297+
279298
// ReadDir return the contents of a given directory in a given filesystem.
280299
//
281300
// Returns a slice of os.FileInfo with all of the entries in the directory.
@@ -379,6 +398,13 @@ func (fs *FileSystem) OpenFile(p string, flag int) (filesystem.File, error) {
379398
return f, nil
380399
}
381400

401+
func (fs *FileSystem) Remove(p string) error {
402+
if fs.workspace == "" {
403+
return filesystem.ErrNotSupported
404+
}
405+
return os.Remove(path.Join(fs.workspace, p))
406+
}
407+
382408
// readDirectory - read directory entry on squashfs only (not workspace)
383409
func (fs *FileSystem) readDirectory(p string) ([]*directoryEntry, error) {
384410
// use the root inode to find the location of the root direectory in the table

0 commit comments

Comments
 (0)