Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

archive/tar: add (*Writer).AddFS #58000

Closed
c9845 opened this issue Jan 25, 2023 · 6 comments
Closed

archive/tar: add (*Writer).AddFS #58000

c9845 opened this issue Jan 25, 2023 · 6 comments

Comments

@c9845
Copy link

c9845 commented Jan 25, 2023

This proposal is similar to #54898, except for archive/tar.

The idea would be to provide an AddFS method that would tar an entire directory tree provided by fs.FS.

Why implement this:

  1. If similar functionality will be implemented for archive/zip, it makes sense to implement it for archive/tar since the use case is similar.
  2. This would prevent the need for developers to manually write a "tar this directory" function which would more likely have bugs than a standard-lib implementation.
  3. Taring a directory is a pretty common task, and since fs.FS exists, we might as well use it.
@c9845 c9845 added the Proposal label Jan 25, 2023
@gopherbot gopherbot added this to the Proposal milestone Jan 25, 2023
@ianlancetaylor
Copy link
Contributor

CC @dsnet

@rsc
Copy link
Contributor

rsc commented Jan 31, 2023

Generally speaking we keep archive/tar and archive/zip with comparable API, so I think it makes sense to accept this at the same time we accept the archive/zip change.

@rsc
Copy link
Contributor

rsc commented Jan 31, 2023

I thought I needed this function in a program I was writing, so I wrote it (I've since changed my mind about needing it in this case). Should be something like this.

func tarAddFS(w *tar.Writer, fsys fs.FS) error {
	return fs.WalkDir(fsys, ".", func(name string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if d.IsDir() {
			return nil
		}
		info, err := d.Info()
		if err != nil {
			return err
		}
		h, err := tar.FileInfoHeader(info, "")
		if err != nil {
			return err
		}
		h.Name = name
		if err := w.WriteHeader(h); err != nil {
			return err
		}
		f, err := fsys.Open(name)
		if err != nil {
			return err
		}
		defer f.Close()
		_, err = io.Copy(w, f)
		return err
	})
}

@rsc
Copy link
Contributor

rsc commented Feb 1, 2023

Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Feb 9, 2023

No change in consensus, so accepted. 🎉
This issue now tracks the work of implementing the proposal.
— rsc for the proposal review group

@rsc rsc changed the title proposal: archive/tar: add (*Writer).AddFS archive/tar: add (*Writer).AddFS Feb 9, 2023
@rsc rsc modified the milestones: Proposal, Backlog Feb 9, 2023
mauri870 added a commit to mauri870/go that referenced this issue Jul 26, 2023
The method AddFS can be used to add the contents of a fs.FS filesystem
to a tar archive. This method walks the directory tree starting at the
root of the filesystem and adds each file to the archive.

Fixes: golang#58000
mauri870 added a commit to mauri870/go that referenced this issue Jul 26, 2023
The method AddFS can be used to add the contents of a fs.FS filesystem
to a tar archive. This method walks the directory tree starting at the
root of the filesystem and adds each file to the archive.

Fixes: golang#58000
@gopherbot
Copy link

Change https://go.dev/cl/513316 mentions this issue: archive/tar: add AddFS method to Writer

mauri870 added a commit to mauri870/go that referenced this issue Aug 10, 2023
The method AddFS can be used to add the contents of a fs.FS filesystem
to a tar archive. This method walks the directory tree starting at the
root of the filesystem and adds each file to the archive.

Fixes: golang#58000
mauri870 added a commit to mauri870/go that referenced this issue Aug 10, 2023
The method AddFS can be used to add the contents of a fs.FS filesystem
to a tar archive. This method walks the directory tree starting at the
root of the filesystem and adds each file to the archive.

Fixes: golang#58000
@dmitshur dmitshur modified the milestones: Backlog, Go1.22 Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Accepted
Development

Successfully merging a pull request may close this issue.

5 participants