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

Merged
merged 8 commits into from
Nov 17, 2024
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) entriesToBytes(bytesPerCluster int) ([]byte, error) {
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 @@ func (d *Directory) createEntry(name string, cluster uint32, dir bool) (*directo
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)
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)
lfn := ""
if isLFN {
lfn = oldFileName
}

var newEntries []*directoryEntry
var isReplaced = false
for _, entry := range d.entries {
if entry.filenameLong == newFileName {
continue // skip adding already existing file, will be overwritten
}
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
130 changes: 121 additions & 9 deletions filesystem/fat32/fat32.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,18 +637,130 @@ func (fs *FileSystem) OpenFile(p string, flag int) (filesystem.File, error) {
}, nil
}

// Rename renames (moves) oldpath to newpath. If newpath already exists and is not a directory, Rename replaces it.
//
//nolint:revive // parameters will be used eventually
func (fs *FileSystem) Rename(oldpath, newpath string) error {
return filesystem.ErrNotImplemented
}

// removes the named file or (empty) directory.
//
//nolint:revive // parameters will be used eventually
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the method got implemented, lint exception is not needed anymore here. Please remove it.

func (fs *FileSystem) Remove(pathname string) error {
return filesystem.ErrNotImplemented
// get the path
dir := path.Dir(pathname)
filename := path.Base(pathname)
// if the dir == filename, then it is just /
if dir == filename {
return fmt.Errorf("cannot remove directory %s as file", pathname)
}
// 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 {
content, err := fs.ReadDir(pathname)
if err != nil {
return fmt.Errorf("error while checking if file to delete is empty: %+v", err)
}
// '.' & '..' are always present in directory
if len(content) > 2 {
return fmt.Errorf("cannot remove non-empty directory %s", pathname)
}
}
// 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", pathname)
}
err = parentDir.removeEntry(filename)
if err != nil {
return fmt.Errorf("failed to remove file %s: %v", pathname, 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", pathname, err)
}

return nil
}

// Rename renames (moves) oldpath to newpath. If newpath already exists and is not a directory, Rename replaces it.
func (fs *FileSystem) Rename(oldpath, newpath string) error {
// get the path
dir := path.Dir(oldpath)
filename := path.Base(oldpath)

newDir := path.Dir(newpath)
newname := path.Base(newpath)
if dir != newDir {
return errors.New("can only rename files within the same directory")
}

// if the dir == filename, then it is just /
if dir == filename {
return fmt.Errorf("cannot rename directory %s as file", oldpath)
}
// 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
}
// 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", oldpath)
}
err = parentDir.renameEntry(filename, newname)
if err != nil {
return fmt.Errorf("failed to rename file %s: %v", oldpath, 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", oldpath, err)
}

return nil
}

// Label get the label of the filesystem from the secial file in the root directory.
Expand Down Expand Up @@ -801,7 +913,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: %w", err)
Expand Down
Loading