More setup for Files.

This commit is contained in:
Caleb Gardner
2020-11-25 13:57:38 -06:00
parent 9beca864c3
commit 77222f55f5
2 changed files with 45 additions and 14 deletions
+2 -2
View File
@@ -21,9 +21,9 @@ Thanks also to [distri's squashfs library](https://github.com/distr1/distri/tree
# Not Working (Yet). Not necessarily in order.
* Rename repo so it's easier to import
* Provide an easy interface to find and list files and their properties
* Maybe squashfs.File
* Make device, socket, symlink, and all extended types of inode work properly. (I need to find an archive that uses it first.)
* Extracting files
* from inodes.
* from file info.
@@ -41,4 +41,4 @@ Thanks also to [distri's squashfs library](https://github.com/distr1/distri/tree
# Where I'm at
* v0.1 is the first working version!
* Working on the File interface that should make it easier to deal with squashfs files. I'm also trying to make them capable for when I get squashing working.
+43 -12
View File
@@ -1,24 +1,55 @@
package squashfs
import (
"errors"
"io"
"github.com/CalebQ42/squashfs/internal/directory"
"github.com/CalebQ42/squashfs/internal/inode"
)
//File represents a file within a squashfs. File can be either a file or folder.
var (
//ErrNotDirectory is returned when you're trying to do directory things with a non-directory
ErrNotDirectory = errors.New("File is not a directory")
)
//File is the main way to interact with files within squashfs, or when putting files into a squashfs.
//File can be either a file or folder. When reading from a squashfs, it reads from the datablocks.
//When writing, this holds the information on WHERE the file will be placed inside the archive.
type File struct {
Name string
Parent *File
Reader *io.Reader
path string
size uint32
r *Reader
in *inode.Inode
Name string //The name of the file or folder.
Parent *File //The parent directory. If it's the root directory, will be nil
Reader io.Reader //Underlying reader. When writing, will probably be an os.File. When reading will probably be a FileReader
path string //When writing, you can set where a file goes in the archive with this. (not yet tho)
size int //The size of the file. -1 if a directory
r *Reader //The squashfs.Reader where this file is contained.
in *inode.Inode //Underlyting inode when reading.
filType int //The file's type, using inode types.
}
func (r *Reader) newFileFromEntry(en *directory.Entry) (f *File, err error) {
f.Name = en.Name
f.in, err = r.getInodeFromEntry(en)
func (f *File) GetChildren() ([]*File, error) {
if !f.IsDir() {
return nil, ErrNotDirectory
}
//TODO
return nil, nil
}
//IsDir returns if the file is a directory.
func (f *File) IsDir() bool {
return f.filType == inode.BasicDirectoryType || f.filType == inode.ExtDirType
}
//
func (f *File) Close() {
//nil the reader to free up resources (in theory). Might switch reader to be a readcloser to make it easier.
f.Reader = nil
}
//Read from the file. Doesn't do anything fancy, just pases it to the underlying io.Reader. If a directory, return io.EOF
func (f *File) Read(p []byte) (int, error) {
if f.IsDir() {
return 0, io.EOF
}
//Check if reader is nill and create a new one if needed.
return f.Reader.Read(p)
}