~nilium/mtar

79af2145140d0bacedd87da22f5b1160c34a7bb6 — Noel Cower 4 years ago 5781f88
Add ref=LINK header option

This allows turning a file into a hard link.
1 files changed, 24 insertions(+), 3 deletions(-)

M mtar.go
M mtar.go => mtar.go +24 -3
@@ 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() {