fix(archive): keep permissions and support long names when replacing an archive

The temporary archive was chmodded to 0644, so replacing a private 0600
archive made it world-readable (QA-6). A replaced archive now keeps its
permissions; a new one gets 0666 minus the umask, as before the
temp-file change.

The temp name embedded the archive name, so names near NAME_MAX failed
with ENAMETOOLONG (QA-7). It is now a short fixed-length .vcom-tmp-<rand>.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-11-01 03:58:18 +03:00
parent 846ee4d2bc
commit c3606d5873
3 changed files with 54 additions and 4 deletions

View file

@ -9,6 +9,7 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
@ -341,7 +342,9 @@ func CreateArchive(ctx context.Context, sources []string, archivePath string, pr
return fmt.Errorf("unsupported archive format: %s", filepath.Ext(archivePath))
}
tmp, err := os.CreateTemp(filepath.Dir(archivePath), ".vcom-tmp-*-"+filepath.Base(archivePath))
// Short temp name: a long but valid archive name plus a prefix would
// exceed NAME_MAX (QA-7). CreateTemp makes it 0600 while it is written.
tmp, err := os.CreateTemp(filepath.Dir(archivePath), ".vcom-tmp-*")
if err != nil {
return fmt.Errorf("create %s: %w", archivePath, err)
}
@ -351,7 +354,14 @@ func CreateArchive(ctx context.Context, sources []string, archivePath string, pr
os.Remove(tmpPath)
return err
}
if err := os.Chmod(tmpPath, 0o644); err != nil {
// A replaced archive keeps its permissions (a private 0600 archive must
// not become world-readable, QA-6); a new one gets 0666 minus umask,
// as os.Create would.
mode := 0o666 &^ processUmask()
if st, err := os.Stat(archivePath); err == nil {
mode = st.Mode().Perm()
}
if err := os.Chmod(tmpPath, mode); err != nil {
os.Remove(tmpPath)
return err
}
@ -726,3 +736,19 @@ func commonBaseDir(paths []string) string {
}
return base
}
// processUmask reads the umask without changing it (syscall.Umask would
// briefly change it for the whole process). Falls back to 022.
func processUmask() os.FileMode {
data, err := os.ReadFile("/proc/self/status")
if err == nil {
for _, line := range strings.Split(string(data), "\n") {
if v, ok := strings.CutPrefix(line, "Umask:"); ok {
if n, err := strconv.ParseUint(strings.TrimSpace(v), 8, 32); err == nil {
return os.FileMode(n) & 0o777
}
}
}
}
return 0o022
}

View file

@ -132,7 +132,6 @@ func qaListArchive(t *testing.T, p string) []string {
// Overwriting an existing archive after confirmation must not widen its
// permissions: a private 0600 archive became world-readable 0644.
func TestQAArchiveOverwriteKeepsPermissions(t *testing.T) {
t.Skip("BUG QA-6: CreateArchive chmods the new archive to 0644, an existing 0600 archive becomes world-readable (archive.go:354)")
d, out := t.TempDir(), t.TempDir()
qaWrite(t, filepath.Join(d, "secret.txt"), "pin 1234", 0o600)
arc := filepath.Join(out, "secret.zip")
@ -152,7 +151,6 @@ func TestQAArchiveOverwriteKeepsPermissions(t *testing.T) {
// A long but valid file name (NAME_MAX is 255 bytes) can no longer be
// archived: the temp name ".vcom-tmp-<rand>-<name>" exceeds NAME_MAX.
func TestQAArchiveLongName(t *testing.T) {
t.Skip("BUG QA-7: archive temp file name adds ~20 bytes to the target name; names near NAME_MAX fail with ENAMETOOLONG (archive.go:344)")
d, out := t.TempDir(), t.TempDir()
base := strings.Repeat("n", 240)
src := filepath.Join(d, base+".txt")

View file

@ -0,0 +1,26 @@
package vfs
import (
"context"
"os"
"path/filepath"
"testing"
)
// QA-6: a new archive gets 0666 minus umask (as os.Create would), not the
// 0600 of the temp file and not a fixed 0644.
func TestNewArchiveModeFollowsUmask(t *testing.T) {
d, out := t.TempDir(), t.TempDir()
qaWrite(t, filepath.Join(d, "a.txt"), "x", 0o644)
arc := filepath.Join(out, "a.tar")
if err := CreateArchive(context.Background(), []string{filepath.Join(d, "a.txt")}, arc, nil); err != nil {
t.Fatal(err)
}
st, err := os.Stat(arc)
if err != nil {
t.Fatal(err)
}
if want := 0o666 &^ processUmask(); st.Mode().Perm() != want {
t.Fatalf("new archive mode %o, want %o", st.Mode().Perm(), want)
}
}