Skip to content

Commit

Permalink
chore: change LinkTargetNotFoundError impl
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijie-yang committed Nov 19, 2024
1 parent de42bf2 commit a21f7df
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
3 changes: 2 additions & 1 deletion internal/deb/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -277,7 +278,7 @@ func extractData(pkgReader io.ReadSeeker, options *ExtractOptions) error {
err := options.Create(extractInfos, createOptions)
if err != nil {
// Handle the hard link where its counterpart is not extracted
if tarHeader.Typeflag == tar.TypeLink && strings.HasPrefix(err.Error(), "link target does not exist") {
if tarHeader.Typeflag == tar.TypeLink && errors.Is(err, fsutil.LinkTargetNotExistError) {
basePath := sanitizePath(tarHeader.Linkname)
pendingHardlinks[basePath] = append(pendingHardlinks[basePath],
PendingHardlink{
Expand Down
12 changes: 3 additions & 9 deletions internal/fsutil/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@ func createSymlink(o *CreateOptions) error {
return os.Symlink(o.Link, o.Path)
}

var LinkTargetNotExistError = fmt.Errorf("link target does not exist")

func createHardLink(o *CreateOptions) error {
debugf("Creating hard link: %s => %s", o.Path, o.Link)
linkInfo, err := os.Lstat(o.Link)
if err != nil && os.IsNotExist(err) {
return &LinkTargetNotExistError{Link: o.Link}
return LinkTargetNotExistError
} else if err != nil {
return err
}
Expand Down Expand Up @@ -223,11 +225,3 @@ func (rp *writerProxy) Close() error {
rp.entry.Size = rp.size
return rp.inner.Close()
}

type LinkTargetNotExistError struct {
Link string
}

func (e *LinkTargetNotExistError) Error() string {
return fmt.Sprintf("link target does not exist: %s", e.Link)
}
2 changes: 1 addition & 1 deletion internal/fsutil/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ var createTests = []createTest{{
hackopt: func(c *C, targetDir string, options *fsutil.CreateOptions) {
options.Link = filepath.Join(targetDir, options.Link)
},
error: `link target does not exist: \/[^ ]*\/missing-file`,
error: `link target does not exist`,
}, {
summary: "Re-creating a duplicated hard link keeps the original link",
options: fsutil.CreateOptions{
Expand Down

0 comments on commit a21f7df

Please sign in to comment.