Ignore duplicate files when recursive

This commit is contained in:
Terence Boldt 2023-01-22 12:02:09 -05:00
parent 286964d3ee
commit 36c24e60ca
4 changed files with 38 additions and 8 deletions

View File

@ -201,7 +201,7 @@ func put(fileName string, pathName string, fileType int, auxType int, inFileName
defer file.Close()
fileInfo, err := os.Stat(fileName)
err = prodos.WriteFileFromFile(file, pathName, fileType, auxType, fileInfo.ModTime(), inFileName)
err = prodos.WriteFileFromFile(file, pathName, fileType, auxType, fileInfo.ModTime(), inFileName, false)
if err != nil {
fmt.Printf("Failed to write file %s", err)
}

View File

@ -248,6 +248,7 @@ func getFreeFileEntryInDirectory(readerWriter ReaderWriterAt, directory string)
fileEntry := parseFileEntry(buffer[entryOffset:entryOffset+0x28], blockNumber, entryOffset)
if fileEntry.StorageType == StorageDeleted {
fileEntry = FileEntry{}
fileEntry.DirectoryBlock = blockNumber
fileEntry.DirectoryOffset = entryOffset
fileEntry.HeaderPointer = directoryHeader.StartingBlock

View File

@ -52,7 +52,7 @@ func WriteFile(readerWriter ReaderWriterAt, path string, fileType int, auxType i
existingFileEntry, _ := GetFileEntry(readerWriter, path)
if existingFileEntry.StorageType != StorageDeleted {
DeleteFile(readerWriter, path)
return errors.New(("file already exists"))
}
// get list of blocks to write file to
@ -126,6 +126,10 @@ func incrementFileCount(readerWriter ReaderWriterAt, fileEntry FileEntry) error
// DeleteFile deletes a file from a ProDOS volume
func DeleteFile(readerWriter ReaderWriterAt, path string) error {
fileEntry, err := GetFileEntry(readerWriter, path)
// DumpFileEntry(fileEntry)
// oldDirectoryBlock, _ := ReadBlock(readerWriter, fileEntry.DirectoryBlock)
// DumpBlock(oldDirectoryBlock)
if err != nil {
return errors.New("file not found")
}
@ -169,6 +173,12 @@ func DeleteFile(readerWriter ReaderWriterAt, path string) error {
return nil
}
// FileExists return true if the file exists
func FileExists(reader io.ReaderAt, path string) (bool, error) {
fileEntry, _ := GetFileEntry(reader, path)
return fileEntry.StorageType != StorageDeleted, nil
}
// GetDirectoryAndFileNameFromPath gets the directory and filename from a path
func GetDirectoryAndFileNameFromPath(path string) (string, string) {
path = strings.ToUpper(path)

View File

@ -22,7 +22,8 @@ func AddFilesFromHostDirectory(
readerWriter ReaderWriterAt,
directory string,
path string,
recursive bool) error {
recursive bool,
) error {
path, err := makeFullPath(path, readerWriter)
@ -42,7 +43,7 @@ func AddFilesFromHostDirectory(
}
if file.Name()[0] != '.' && !file.IsDir() && info.Size() > 0 && info.Size() <= 0x1000000 {
err = WriteFileFromFile(readerWriter, path, 0, 0, info.ModTime(), filepath.Join(directory, file.Name()))
err = WriteFileFromFile(readerWriter, path, 0, 0, info.ModTime(), filepath.Join(directory, file.Name()), true)
if err != nil {
return err
}
@ -56,8 +57,14 @@ func AddFilesFromHostDirectory(
newFullPath := strings.ToUpper(path + newPath)
newHostDirectory := filepath.Join(directory, file.Name())
CreateDirectory(readerWriter, newFullPath)
AddFilesFromHostDirectory(readerWriter, newHostDirectory, newFullPath+"/", recursive)
err = CreateDirectory(readerWriter, newFullPath)
if err != nil {
return err
}
err = AddFilesFromHostDirectory(readerWriter, newHostDirectory, newFullPath+"/", recursive)
if err != nil {
return err
}
}
}
@ -71,7 +78,9 @@ func WriteFileFromFile(
fileType int,
auxType int,
modifiedTime time.Time,
inFileName string) error {
inFileName string,
ignoreDuplicates bool,
) error {
inFile, err := os.ReadFile(inFileName)
if err != nil {
@ -117,7 +126,17 @@ func WriteFileFromFile(
pathName = strings.Join(paths, "")
}
fmt.Printf("Source: %s Destination: %s\n", inFileName, pathName)
// skip if file already exists and ignoring duplicates
if ignoreDuplicates {
exists, err := FileExists(readerWriter, pathName)
if err != nil {
return err
}
if exists {
return nil
}
}
return WriteFile(readerWriter, pathName, fileType, auxType, time.Now(), modifiedTime, inFile)
}