Minor work on the Writer

This commit is contained in:
Caleb Gardner
2021-02-03 14:03:21 -06:00
parent d89153c3e2
commit 07962426b2
2 changed files with 39 additions and 9 deletions
+20 -6
View File
@@ -19,6 +19,7 @@ type Writer struct {
compressor compression.Compressor
structure map[string][]*fileHolder
symlinkTable map[string]string //[oldpath]newpath
folders []string
uidGUIDTable []int
compressionType int
//BlockSize is how large the data blocks are. Can be between 4096 (4KB) and 1048576 (1 MB).
@@ -44,12 +45,15 @@ func NewWriterWithOptions(compressionType int, allowErrors bool) (*Writer, error
return nil, errors.New("Incorrect compression type")
}
if compressionType == 3 {
return nil, errors.New("LZO compression is not (currently) supported")
return nil, errors.New("Lzo compression is not (currently) supported")
}
return &Writer{
structure: map[string][]*fileHolder{
"/": make([]*fileHolder, 0),
},
folders: []string{
"/",
},
symlinkTable: make(map[string]string),
compressionType: compressionType,
allowErrors: allowErrors,
@@ -71,7 +75,7 @@ type fileHolder struct {
symlink bool
}
//AddFile attempts to add an os.File to the archive at it's root.
//AddFile attempts to add an os.File to the archive's root directory.
func (w *Writer) AddFile(file *os.File) error {
return w.AddFileToFolder("/", file)
}
@@ -95,8 +99,7 @@ func (w *Writer) AddFileTo(filepath string, file *os.File) error {
return errors.New("File already exists at " + filepath)
}
var holder fileHolder
holder.path, holder.name = path.Split(filepath)
holder.reader = file
holder.path, holder.name = path.Dir(filepath), path.Base(filepath)
stat, err := file.Stat()
if err != nil {
return err
@@ -118,6 +121,7 @@ func (w *Writer) AddFileTo(filepath string, file *os.File) error {
sort.Ints(w.uidGUIDTable)
}
if holder.symlink {
holder.reader = file
target, err := os.Readlink(file.Name())
if err != nil {
return err
@@ -148,9 +152,15 @@ func (w *Writer) AddFileTo(filepath string, file *os.File) error {
dirsAdded = append(dirsAdded, holder.path+"/"+holder.name)
}
}
} else if !stat.Mode().IsRegular() {
} else if stat.Mode().IsRegular() {
holder.reader = file
} else {
return errors.New("Unsupported file type " + file.Name())
}
if _, ok := w.structure[holder.path]; ok {
w.folders = append(w.folders, holder.path)
sort.Strings(w.folders)
}
w.structure[holder.path] = append(w.structure[holder.path], &holder)
return nil
}
@@ -167,8 +177,12 @@ func (w *Writer) AddReaderTo(filepath string, reader io.Reader) error {
return errors.New("File already exists at " + filepath)
}
var holder fileHolder
holder.path, holder.name = path.Split(filepath)
holder.path, holder.name = path.Dir(filepath), path.Base(filepath)
holder.reader = reader
if _, ok := w.structure[holder.path]; ok {
w.folders = append(w.folders, holder.path)
sort.Strings(w.folders)
}
w.structure[holder.path] = append(w.structure[holder.path], &holder)
return nil
}