From 77222f55f575b2ba306abb1a9becea077c4fa16b Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Wed, 25 Nov 2020 13:57:38 -0600 Subject: [PATCH] More setup for Files. --- README.md | 4 ++-- file.go | 55 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index b74f53b..a5549f1 100644 --- a/README.md +++ b/README.md @@ -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! \ No newline at end of file +* 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. \ No newline at end of file diff --git a/file.go b/file.go index 43c7adc..f8f33f7 100644 --- a/file.go +++ b/file.go @@ -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) }