Makes sure folders are created when files are added
Added some comments
This commit is contained in:
@@ -87,7 +87,7 @@ type fileHolder struct {
|
||||
blockSizes []uint32
|
||||
GUID int
|
||||
perm int
|
||||
size uint64
|
||||
size uint64 //when folder, size is of the directory entry(s) in the Directory Table.
|
||||
UID int
|
||||
folder bool
|
||||
symlink bool
|
||||
@@ -145,19 +145,22 @@ func (w *Writer) AddFileTo(filepath string, file *os.File) error {
|
||||
}
|
||||
if holder.symlink {
|
||||
holder.reader = file
|
||||
target, err := os.Readlink(file.Name())
|
||||
var target string
|
||||
target, err = os.Readlink(file.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
holder.symLocation = target
|
||||
} else if holder.folder {
|
||||
subDirNames, err := file.Readdirnames(-1)
|
||||
var subDirNames []string
|
||||
subDirNames, err = file.Readdirnames(-1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dirsAdded := make([]string, 0)
|
||||
for _, subDir := range subDirNames {
|
||||
fil, err := os.Open(file.Name() + subDir)
|
||||
var fil *os.File
|
||||
fil, err = os.Open(file.Name() + subDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -181,6 +184,12 @@ func (w *Writer) AddFileTo(filepath string, file *os.File) error {
|
||||
return errors.New("Unsupported file type " + file.Name())
|
||||
}
|
||||
if _, ok := w.structure[holder.path]; ok {
|
||||
if !w.Contains(holder.path) {
|
||||
err = w.AddFolderTo(holder.path, fs.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
w.folders = append(w.folders, holder.path)
|
||||
sort.Strings(w.folders)
|
||||
}
|
||||
@@ -205,6 +214,12 @@ func (w *Writer) AddReaderTo(filepath string, reader io.Reader, size uint64) err
|
||||
holder.size = size
|
||||
holder.reader = reader
|
||||
if _, ok := w.structure[holder.path]; ok {
|
||||
if !w.Contains(holder.path) {
|
||||
err := w.AddFolderTo(holder.path, fs.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
w.folders = append(w.folders, holder.path)
|
||||
sort.Strings(w.folders)
|
||||
}
|
||||
@@ -227,9 +242,19 @@ func (w *Writer) AddFolderTo(folderpath string, permission fs.FileMode) error {
|
||||
file := fileHolder{
|
||||
path: path.Dir(folderpath),
|
||||
name: path.Base(folderpath),
|
||||
perm: int(permission | fs.ModePerm),
|
||||
perm: int(permission.Perm()),
|
||||
folder: true,
|
||||
}
|
||||
if _, ok := w.structure[file.path]; ok {
|
||||
if !w.Contains(file.path) {
|
||||
err := w.AddFolderTo(file.path, fs.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
w.folders = append(w.folders, file.path)
|
||||
sort.Strings(w.folders)
|
||||
}
|
||||
w.structure[file.path] = append(w.structure[file.path], &file)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -64,6 +64,8 @@ func (w *Writer) addToFragments(fil *fileHolder) {
|
||||
fragSize := fil.blockSizes[len(fil.blockSizes)-1]
|
||||
//only fragment if the final block is less then 80% of a full block or AlwaysFragment
|
||||
if w.Flags.AlwaysFragment || fragSize < uint32(float32(w.BlockSize)*0.8) {
|
||||
//Try to slot the fragment into a fragment that has the perfect size left. If not, just pick the first one.
|
||||
//TODO: possibly make this more efficient, possibly by calculating fragments all at once and seeing which combos match BlockSize perfectly.
|
||||
var possibleFrags []int
|
||||
for i := range w.frags {
|
||||
left := w.frags[i].sizeLeft()
|
||||
|
||||
@@ -3,34 +3,14 @@ package squashfs
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (w *Writer) fixFolders() error {
|
||||
for folder := range w.structure {
|
||||
if folder == "/" || w.Contains(folder) {
|
||||
continue
|
||||
}
|
||||
err := w.AddFolderTo(folder, fs.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//WriteTo attempts to write the archive to the given io.Writer.
|
||||
//Folder that aren't present (such as if you add a file at /folder/file, but not the folder /folder)
|
||||
//are added with full permission (777).
|
||||
//
|
||||
//Not working. Yet.
|
||||
func (w *Writer) WriteTo(write io.Writer) (int64, error) {
|
||||
err := w.fixFolders()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if w.BlockSize > 1048576 {
|
||||
w.BlockSize = 1048576
|
||||
} else if w.BlockSize < 4096 {
|
||||
|
||||
Reference in New Issue
Block a user