Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement remove and rename feature for fat32 #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions filesystem/ext4/ext4.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,14 @@
}, nil
}

func (fs *FileSystem) RemoveFile(p string) error {

Check failure on line 807 in filesystem/ext4/ext4.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused-parameter: parameter 'p' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 807 in filesystem/ext4/ext4.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused-parameter: parameter 'p' seems to be unused, consider removing or renaming it as _ (revive)
return fmt.Errorf("not implemented")
}

func (fs *FileSystem) RenameFile(p, newFileName string) error {

Check failure on line 811 in filesystem/ext4/ext4.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused-parameter: parameter 'p' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 811 in filesystem/ext4/ext4.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused-parameter: parameter 'p' seems to be unused, consider removing or renaming it as _ (revive)
return fmt.Errorf("not implemented")
}

// Label read the volume label
func (fs *FileSystem) Label() string {
if fs.superblock == nil {
Expand Down
72 changes: 71 additions & 1 deletion filesystem/fat32/directory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fat32

import (
"fmt"
"time"
)

Expand Down Expand Up @@ -41,7 +42,7 @@
func (d *Directory) createEntry(name string, cluster uint32, dir bool) (*directoryEntry, error) {
// is it a long filename or a short filename?
var isLFN bool
// TODO: convertLfnSfn does not calculate if the short name conflicts and thus shoukld increment the last character
// TODO: convertLfnSfn does not calculate if the short name conflicts and thus should increment the last character
// that should happen here, once we can look in the directory entry
shortName, extension, isLFN, _ := convertLfnSfn(name)
lfn := ""
Expand Down Expand Up @@ -70,6 +71,75 @@
return &entry, nil
}

// removeEntry removes an entry in the given directory
func (d *Directory) removeEntry(name string) error {
// is it a long filename or a short filename?
var isLFN bool
// TODO: convertLfnSfn does not calculate if the short name conflicts and thus should increment the last character
// that should happen here, once we can look in the directory entry
_, _, isLFN, _ = convertLfnSfn(name)

Check failure on line 80 in filesystem/fat32/directory.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

declaration has 3 blank identifiers (dogsled)

Check failure on line 80 in filesystem/fat32/directory.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

declaration has 3 blank identifiers (dogsled)
lfn := ""
if isLFN {
lfn = name
}

removeEntryIndex := -1
for i, entry := range d.entries {
if entry.filenameLong == lfn { // || entry.filenameShort == shortName do not compare SFN, since it is not incremented correctly
removeEntryIndex = i
}
}

if removeEntryIndex == -1 {
return fmt.Errorf("cannot find entry for name %s", name)
}

// remove the entry from the list
d.entries = append(d.entries[:removeEntryIndex], d.entries[removeEntryIndex+1:]...)

return nil
}

// renameEntry renames an entry in the given directory, and returns the handle to it
func (d *Directory) renameEntry(oldFileName, newFileName string) error {
// is it a long filename or a short filename?
var isLFN bool
// TODO: convertLfnSfn does not calculate if the short name conflicts and thus should increment the last character
// that should happen here, once we can look in the directory entry
_, _, isLFN, _ = convertLfnSfn(oldFileName)

Check failure on line 109 in filesystem/fat32/directory.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

declaration has 3 blank identifiers (dogsled)

Check failure on line 109 in filesystem/fat32/directory.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

declaration has 3 blank identifiers (dogsled)
lfn := ""
if isLFN {
lfn = oldFileName
}

var newEntries []*directoryEntry

Check failure on line 115 in filesystem/fat32/directory.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Consider pre-allocating `newEntries` (prealloc)

Check failure on line 115 in filesystem/fat32/directory.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Consider pre-allocating `newEntries` (prealloc)
var isReplaced = false
for _, entry := range d.entries {
if entry.filenameLong == newFileName {
return fmt.Errorf("file with name %s already exists", newFileName)
}
if entry.filenameLong == lfn { // || entry.filenameShort == shortName do not compare SFN, since it is not incremented correctly
shortName, extension, isLFN, _ := convertLfnSfn(newFileName)
if isLFN {
lfn = newFileName
}
entry.filenameLong = lfn
entry.filenameShort = shortName
entry.fileExtension = extension
entry.modifyTime = time.Now()
isReplaced = true
}
newEntries = append(newEntries, entry)
}
if !isReplaced {
return fmt.Errorf("cannot find file entry for %s", oldFileName)
}

d.entries = newEntries

return nil
}

// createVolumeLabel create a volume label entry in the given directory, and return the handle to it
func (d *Directory) createVolumeLabel(name string) (*directoryEntry, error) {
// allocate a slot for the new filename in the existing directory
Expand Down
120 changes: 119 additions & 1 deletion filesystem/fat32/fat32.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,124 @@ func (fs *FileSystem) OpenFile(p string, flag int) (filesystem.File, error) {
}, nil
}

// RemoveFile removes a file from the filesystem
//
// returns an error if the file does not exist or cannot be removed
func (fs *FileSystem) RemoveFile(p string) error {
// get the path
dir := path.Dir(p)
filename := path.Base(p)
// if the dir == filename, then it is just /
if dir == filename {
return fmt.Errorf("cannot remove directory %s as file", p)
}
// get the directory entries
parentDir, entries, err := fs.readDirWithMkdir(dir, false)
if err != nil {
return fmt.Errorf("could not read directory entries for %s", dir)
}
// we now know that the directory exists, see if the file exists
var targetEntry *directoryEntry
for _, e := range entries {
shortName := e.filenameShort
if e.fileExtension != "" {
shortName += "." + e.fileExtension
}
if e.filenameLong != filename && shortName != filename {
continue
}
// cannot do anything with directories
if e.isSubdirectory {
return fmt.Errorf("cannot open directory %s as file", p)
}
// if we got this far, we have found the file
targetEntry = e
}

// see if the file exists
// if the file does not exist, and is not opened for os.O_CREATE, return an error
if targetEntry == nil {
return fmt.Errorf("target file %s does not exist", p)
}
err = parentDir.removeEntry(filename)
if err != nil {
return fmt.Errorf("failed to remove file %s: %v", p, err)
}

// we need to make sure that clusters are removed which may not be used anymore
_, err = fs.allocateSpace(uint64(parentDir.fileSize), parentDir.clusterLocation)
if err != nil {
return fmt.Errorf("failed to allocate clusters: %v", err)
}

// write the directory entries to disk
err = fs.writeDirectoryEntries(parentDir)
if err != nil {
return fmt.Errorf("error writing directory file %s to disk: %v", p, err)
}

return nil
}

// RenameFile removes a file from the filesystem
//
// returns an error if the file does not exist or cannot be renamed
func (fs *FileSystem) RenameFile(p, newFileName string) error {
// get the path
dir := path.Dir(p)
filename := path.Base(p)
// if the dir == filename, then it is just /
if dir == filename {
return fmt.Errorf("cannot rename directory %s as file", p)
}
// get the directory entries
parentDir, entries, err := fs.readDirWithMkdir(dir, false)
if err != nil {
return fmt.Errorf("could not read directory entries for %s", dir)
}
// we now know that the directory exists, see if the file exists
var targetEntry *directoryEntry
for _, e := range entries {
shortName := e.filenameShort
if e.fileExtension != "" {
shortName += "." + e.fileExtension
}
if e.filenameLong != filename && shortName != filename {
continue
}
// cannot do anything with directories
if e.isSubdirectory {
return fmt.Errorf("cannot open directory %s as file", p)
}
// if we got this far, we have found the file
targetEntry = e
}

// see if the file exists
// if the file does not exist, and is not opened for os.O_CREATE, return an error
if targetEntry == nil {
return fmt.Errorf("target file %s does not exist", p)
}
err = parentDir.renameEntry(filename, newFileName)
if err != nil {
return fmt.Errorf("failed to rename file %s: %v", p, err)
}

// we need to make sure that clusters are removed which may not be used anymore
_, err = fs.allocateSpace(uint64(parentDir.fileSize), parentDir.clusterLocation)
if err != nil {
return fmt.Errorf("failed to allocate clusters: %v", err)
}

// write the directory entries to disk
err = fs.writeDirectoryEntries(parentDir)
if err != nil {
return fmt.Errorf("error writing directory file %s to disk: %v", p, err)
}

return nil
}

// 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 Expand Up @@ -748,7 +866,7 @@ func (fs *FileSystem) mkSubdir(parent *Directory, name string) (*directoryEntry,
}

func (fs *FileSystem) writeDirectoryEntries(dir *Directory) error {
// we need to save the entries of theparent
// we need to save the entries of the parent
b, err := dir.entriesToBytes(fs.bytesPerCluster)
if err != nil {
return fmt.Errorf("could not create a valid byte stream for a FAT32 Entries: %v", err)
Expand Down
Loading
Loading