mirror of
https://github.com/gilbertchen/duplicacy
synced 2025-12-10 13:23:17 +00:00
75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
// Copyright (c) Acrosync LLC. All rights reserved.
|
|
// Licensed under the Fair Source License 0.9 (https://fair.io/)
|
|
// User Limitation: 5 users
|
|
|
|
package duplicacy
|
|
|
|
import (
|
|
"os"
|
|
)
|
|
|
|
// FileReader wraps a number of files and turns them into a series of readers.
|
|
type FileReader struct {
|
|
top string
|
|
files [] *Entry
|
|
|
|
CurrentFile *os.File
|
|
CurrentIndex int
|
|
CurrentEntry *Entry
|
|
|
|
SkippedFiles [] string
|
|
}
|
|
|
|
// CreateFileReader creates a file reader.
|
|
func CreateFileReader(top string, files[] *Entry) (*FileReader) {
|
|
|
|
reader := &FileReader {
|
|
top: top,
|
|
files: files,
|
|
CurrentIndex: -1,
|
|
}
|
|
|
|
reader.NextFile()
|
|
|
|
return reader
|
|
}
|
|
|
|
// NextFile switchs to the next file in the file reader.
|
|
func (reader *FileReader) NextFile() bool{
|
|
|
|
if reader.CurrentFile != nil {
|
|
reader.CurrentFile.Close()
|
|
}
|
|
|
|
reader.CurrentIndex++
|
|
for reader.CurrentIndex < len(reader.files) {
|
|
|
|
reader.CurrentEntry = reader.files[reader.CurrentIndex]
|
|
if !reader.CurrentEntry.IsFile() || reader.CurrentEntry.Size == 0 {
|
|
reader.CurrentIndex++
|
|
continue
|
|
}
|
|
|
|
var err error
|
|
|
|
fullPath := joinPath(reader.top, reader.CurrentEntry.Path)
|
|
reader.CurrentFile, err = os.OpenFile(fullPath, os.O_RDONLY, 0)
|
|
if err != nil {
|
|
LOG_WARN("OPEN_FAILURE", "Failed to open file for reading: %v", err)
|
|
reader.CurrentEntry.Size = 0
|
|
reader.SkippedFiles = append(reader.SkippedFiles, reader.CurrentEntry.Path)
|
|
reader.CurrentIndex++
|
|
continue
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
reader.CurrentFile = nil
|
|
return false
|
|
}
|
|
|
|
|
|
|
|
|