Getting back into it...

This commit is contained in:
Caleb Gardner
2021-02-23 22:26:15 -06:00
parent 3691e1f486
commit 8a91e1a1c1
3 changed files with 63 additions and 16 deletions
+31 -2
View File
@@ -3,6 +3,7 @@ package squashfs
import (
"errors"
"io"
"io/fs"
"log"
"os"
"path"
@@ -29,6 +30,9 @@ type Writer struct {
//Currently Duplicates, Exportable, UncompressedXattr, NoXattr values are ignored
Flags SuperblockFlags
allowErrors bool
//variables used when actually writing.
superblock superblock
}
//NewWriter creates a new with the default options (Gzip compression and allow errors)
@@ -54,6 +58,7 @@ func NewWriterWithOptions(compressionType int, allowErrors bool) (*Writer, error
compressionType: compressionType,
allowErrors: allowErrors,
BlockSize: uint32(1048576),
Flags: DefaultFlags,
}, nil
}
@@ -95,7 +100,8 @@ 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.path = path.Dir(filepath)
holder.name = path.Base(filepath)
holder.reader = file
stat, err := file.Stat()
if err != nil {
@@ -167,12 +173,35 @@ 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 = path.Dir(filepath)
holder.name = path.Base(filepath)
holder.reader = reader
w.structure[holder.path] = append(w.structure[holder.path], &holder)
return nil
}
//AddFolderTo adds a folder at the given path. IF the folder is already present, it sets the folder's permissions.
//If the path points to a non-folder (such as a file or symlink), an error is returned
func (w *Writer) AddFolderTo(folderpath string, permission fs.FileMode) error {
folderpath = path.Clean(folderpath)
tmp := w.holderAt(folderpath)
if tmp != nil {
if !tmp.folder {
return errors.New("Path is not a folder: " + folderpath)
}
tmp.perm = int(permission.Perm())
return nil
}
file := fileHolder{
path: path.Dir(folderpath),
name: path.Base(folderpath),
perm: int(permission | fs.ModePerm),
folder: true,
}
w.structure[file.path] = append(w.structure[file.path], &file)
return nil
}
//Remove tries to remove the file(s) at the given filepath. If wildcards are used, it will remove all files that match.
//Returns true if one or more files are removed.
func (w *Writer) Remove(filepath string) bool {