Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
letFunny committed Jan 29, 2024
1 parent 13d4b06 commit 0fccd1f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion internal/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func (s *S) testOpenArchiveArch(c *C, release ubuntuRelease, arch string) {
{Path: "/hostname"},
},
},
FSCreator: fsutil.NewCreator(),
Creator: fsutil.NewCreator(),
})
c.Assert(err, IsNil)

Expand Down
9 changes: 6 additions & 3 deletions internal/deb/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ExtractOptions struct {
TargetDir string
Extract map[string][]ExtractInfo
Globbed map[string][]string
FSCreator *fsutil.Creator
Creator *fsutil.Creator
}

type ExtractInfo struct {
Expand All @@ -43,6 +43,9 @@ func checkExtractOptions(options *ExtractOptions) error {
}
}
}
if options.Creator == nil {
options.Creator = fsutil.NewCreator()
}
return nil
}

Expand Down Expand Up @@ -186,7 +189,7 @@ func extractData(dataReader io.Reader, options *ExtractOptions) error {
// Base directory for extracted content. Relevant mainly to preserve
// the metadata, since the extracted content itself will also create
// any missing directories unaccounted for in the options.
err := options.FSCreator.Create(&fsutil.CreateOptions{
err := options.Creator.Create(&fsutil.CreateOptions{
Path: filepath.Join(options.TargetDir, sourcePath),
Mode: tarHeader.FileInfo().Mode(),
MakeParents: true,
Expand Down Expand Up @@ -227,7 +230,7 @@ func extractData(dataReader io.Reader, options *ExtractOptions) error {
if extractInfo.Mode != 0 {
tarHeader.Mode = int64(extractInfo.Mode)
}
err := options.FSCreator.Create(&fsutil.CreateOptions{
err := options.Creator.Create(&fsutil.CreateOptions{
Path: targetPath,
Mode: tarHeader.FileInfo().Mode(),
Data: pathReader,
Expand Down
2 changes: 1 addition & 1 deletion internal/deb/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (s *S) TestExtract(c *C) {
options := test.options
options.Package = "base-files"
options.TargetDir = dir
options.FSCreator = fsutil.NewCreator()
options.Creator = fsutil.NewCreator()

if test.globbed != nil {
options.Globbed = make(map[string][]string)
Expand Down
28 changes: 17 additions & 11 deletions internal/fsutil/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Info struct {
Path string
Mode fs.FileMode
Hash string
Size uint
Size int
Link string
}

Expand All @@ -40,9 +40,15 @@ func NewCreator() *Creator {
}

// Creates a filesystem entry according to the provided options.
func (c Creator) Create(o *CreateOptions) error {
rp := readerProxy{inner: o.Data, h: sha256.New()}
o.Data = &rp
func (c *Creator) Create(options *CreateOptions) error {
rp := &readerProxy{inner: options.Data, h: sha256.New()}
o := &CreateOptions{
Path: options.Path,
Mode: options.Mode,
Data: rp,
Link: options.Link,
MakeParents: options.MakeParents,
}

var err error
if o.MakeParents {
Expand All @@ -64,14 +70,14 @@ func (c Creator) Create(o *CreateOptions) error {
return err
}

fr := Info{
info := Info{
Path: o.Path,
Mode: o.Mode,
Hash: hex.EncodeToString(rp.h.Sum(nil)),
Size: rp.size,
Link: o.Link,
}
c.Created[o.Path] = fr
c.Created[o.Path] = info
return nil
}

Expand Down Expand Up @@ -126,14 +132,14 @@ func createSymlink(o *CreateOptions) error {
type readerProxy struct {
inner io.Reader
h hash.Hash
size uint
size int
}

var _ io.Reader = (*readerProxy)(nil)

func (fr *readerProxy) Read(p []byte) (n int, err error) {
n, err = fr.inner.Read(p)
fr.h.Write(p[:n])
fr.size += uint(n)
func (rp *readerProxy) Read(p []byte) (n int, err error) {
n, err = rp.inner.Read(p)
rp.h.Write(p[:n])
rp.size += n
return n, err
}
10 changes: 5 additions & 5 deletions internal/fsutil/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,22 @@ func (s *S) TestCreate(c *C) {
dir := c.MkDir()
options := test.options
options.Path = filepath.Join(dir, options.Path)
fsCreator := fsutil.NewCreator()
err := fsCreator.Create(&options)
creator := fsutil.NewCreator()
err := creator.Create(&options)
if test.error != "" {
c.Assert(err, ErrorMatches, test.error)
} else {
c.Assert(err, IsNil)
}
c.Assert(testutil.TreeDump(dir), DeepEquals, test.result)
if test.options.MakeParents {
// The fsCreator does not record the parent directories created
// The creator does not record the parent directories created
// implicitly.
for path, info := range treeDumpFSCreator(fsCreator, dir) {
for path, info := range treeDumpFSCreator(creator, dir) {
c.Assert(info, Equals, test.result[path])
}
} else {
c.Assert(treeDumpFSCreator(fsCreator, dir), DeepEquals, test.result)
c.Assert(treeDumpFSCreator(creator, dir), DeepEquals, test.result)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func Run(options *RunOptions) error {
Extract: extract[slice.Package],
TargetDir: targetDir,
Globbed: globbedPaths,
FSCreator: fsCreator,
Creator: fsCreator,
})
reader.Close()
packages[slice.Package] = nil
Expand Down

0 comments on commit 0fccd1f

Please sign in to comment.