GitLabgitlab-org
cli
main
1//go:build !windows
3package fsx
5import (
6 "encoding/json"
7 "os"
8 "path/filepath"
10 "github.com/google/renameio/v2"
11)
13// WriteOwnerOnly writes data to path atomically and forces mode 0o600.
14//
15// On POSIX the write is a temp-file-plus-rename via renameio, so a reader
16// concurrent with the write either sees the full old file or the full new
17// file — never a half-written mix. If the process dies mid-write, the target
18// is untouched and only an orphaned temp file may be left behind.
19//
20// renameio.WriteFile defaults to WithExistingPermissions(), which preserves
21// the existing mode when the target already exists. That is the wrong policy
22// for token-bearing files (see the package-level doc comment for the full
23// rationale), so we follow the write with an explicit Chmod to guarantee
24// 0o600 in both the create and the overwrite paths.
25//
26// Callers use this for any file that may embed an inline authentication
27// token — for example, .npmrc, .yarnrc.yml, or a patched Pipfile — and for
28// the token-bearing backups those files produce.
29func WriteOwnerOnly(path string, data []byte) error {
30 if err := renameio.WriteFile(path, data, 0o600); err != nil {
31 return err
32 }
33 return os.Chmod(path, 0o600)
34}
36// WriteExecutable writes data to path atomically and forces mode 0o700.
37//
38// Unlike WriteOwnerOnly, the executable mode is set on the temp file via
39// renameio.WithStaticPermissions before the atomic rename, not by a Chmod
40// call on path afterward: a reader racing the write — for example, a shim
41// invocation racing a concurrent install — must never observe the file at
42// its final path before it is executable, which a write-then-Chmod sequence
43// cannot guarantee since the file is briefly visible in between at whatever
44// mode the write left it in.
45//
46// Callers use this for shim scripts that Docker or another tool invokes
47// directly. Never use it for a file that may hold a secret; see
48// WriteOwnerOnly for those.
49func WriteExecutable(path string, data []byte) error {
50 t, err := renameio.NewPendingFile(path, renameio.WithStaticPermissions(0o700))
51 if err != nil {
52 return err
53 }
54 defer func() { _ = t.Cleanup() }()
56 if _, err := t.Write(data); err != nil {
57 return err
58 }
60 return t.CloseAtomicallyReplace()
61}
63// WriteJSONFile creates path's parent directory (0o755), marshals v with
64// two-space indent for user-editability, appends a trailing newline
65// (POSIX text-file convention; keeps `git diff` clean when the file is
66// hand-edited later), and writes the result to path with mode 0o644.
67// The write is atomic via renameio. Use it for non-secret configuration
68// and log files; token-bearing files must use WriteOwnerOnly instead.
69func WriteJSONFile(path string, v any) error {
70 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
71 return err
72 }
73 raw, err := json.MarshalIndent(v, "", " ") //nolint:forbidigo // writing config/log to disk, not stdout
74 if err != nil {
75 return err
76 }
77 return renameio.WriteFile(path, append(raw, '\n'), 0o644)
78}
Diagnostics6 git calls · 237 ms
CallStatusSizeTime0237 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB213 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=204 git.response.bytes=59849 git.response.cache=EXPIRED git.response.duration_ms=213 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchtree:0200202 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=202 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=da30e344667699ee3f5c5b32590d1fa98350e85d git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=202 git.pkt.payload.max=175 git.sideband.band1.bytes=175 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=175 git.pack.trailer=fb90a53cb363fe86844dccc988be2a05632002e8Open pack map
fetch2001.4 KiB · hit19 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=19 git.response.bytes=1427 git.response.cache=HIT git.response.duration_ms=19 git.fetch.wants=475401e07d7631fcd16ab1fe2cd97f3e18801d60 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1427 git.pkt.payload.max=1400 git.sideband.band1.bytes=1400 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e24efa6e090ecd
- Request
- bb8bb4d9…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format