From 79af2145140d0bacedd87da22f5b1160c34a7bb6 Mon Sep 17 00:00:00 2001 From: Noel Cower Date: Tue, 21 Apr 2020 20:30:47 -0700 Subject: [PATCH] Add ref=LINK header option This allows turning a file into a hard link. --- mtar.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/mtar.go b/mtar.go index 2bc5069..4afba0e 100644 --- a/mtar.go +++ b/mtar.go @@ -65,6 +65,8 @@ // Force file to become a dir entry. Implies norec. // link=LINK // Force file to become a symlink pointing to LINK. +// ref=LINK +// Force file to become a hard link pointing to LINK. // nouser // Strip user information from the file. // uid=UID | owner=USERNAME @@ -199,6 +201,8 @@ available (all option names are case-sensitive): Force file to become a dir entry. Implies norec. link=LINK Force file to become a symlink pointing to LINK. + ref=LINK + Force file to become a hard link pointing to LINK. uid=UID | owner=USERNAME Set the owner's uid and/or username for the file entry. gid=GID | group=GROUPNAME @@ -555,8 +559,9 @@ type FileOpts struct { group *user.Group // exclusive: - dir bool - link string + dir bool + link string + linkType byte mode int64 @@ -598,12 +603,27 @@ func (fo *FileOpts) parse(opts string) error { fo.dir = true fo.noRecursive = true case strings.HasPrefix(f, "link="): + if fo.link != "" { + return errors.New("link already assigned to file") + } if fo.dir { return errors.New("may not set link with dir") } if fo.link = f[len("link="):]; fo.link == "" { return errors.New("may not set an empty link name") } + fo.linkType = tar.TypeSymlink + case strings.HasPrefix(f, "ref="): + if fo.link != "" { + return errors.New("link already assigned to file") + } + if fo.dir { + return errors.New("may not set link with dir") + } + if fo.link = f[len("ref="):]; fo.link == "" { + return errors.New("may not set an empty link name") + } + fo.linkType = tar.TypeLink case f == "nouser": fo.nouser = true case strings.HasPrefix(f, "uid="): @@ -765,7 +785,8 @@ func (f *FileOpts) setHeaderFields(hdr *tar.Header) { } } else if f.link != "" { hdr.Linkname = f.link - hdr.Typeflag = tar.TypeSymlink + hdr.Typeflag = f.linkType + hdr.Size = 0 } if !f.mtime.IsZero() { -- 2.45.2