Skip to content

Commit 30857ef

Browse files
authored
Merge pull request containerd#4308 from apostasie/2025-06-fs
internal/filesystem consolidation, part 2
2 parents 7ad308c + f3dc0ee commit 30857ef

File tree

35 files changed

+402
-72
lines changed

35 files changed

+402
-72
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ linters:
3131
- revive
3232
# Gocritic
3333
- gocritic
34+
- forbidigo
3435

3536
# 3. We used to use these, but have now removed them
3637

@@ -41,6 +42,12 @@ linters:
4142
# - nakedret
4243

4344
settings:
45+
forbidigo:
46+
forbid:
47+
# FIXME: there are still calls to os.WriteFile in tests under `cmd`
48+
- pattern: ^os\.WriteFile.*$
49+
pkg: github.com/containerd/nerdctl/v2/pkg
50+
msg: os.WriteFile is neither atomic nor durable - use nerdctl filesystem.WriteFile instead
4451
staticcheck:
4552
checks:
4653
# Below is the default set

docs/dev/auditing_dockerfile.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ On a warm cache, it is still over 150MB and 30+ seconds.
255255
In and of itself, this is hard to reduce, as we need these...
256256

257257
Actions:
258-
- [ ] we could cache the module download location to reduce round-trips on modules that are shared accross
258+
- [ ] we could cache the module download location to reduce round-trips on modules that are shared across
259259
different projects
260260
- [ ] we are likely installing nerdctl modules six times - (once per architecture during the build phase, then once per
261261
ubuntu version and architecture during the tests runs (this is not even accounted for in the audit above)) - it should

docs/dev/store.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ containers can be named the same), etc.
2323
However, storing data on the filesystem in a reliable way comes with challenges:
2424
- incomplete writes may happen (because of a system restart, or an application crash), leaving important structured files
2525
in a broken state
26-
- concurrent writes, or reading while writing would obviously be a problem as well, be it accross goroutines, or between
26+
- concurrent writes, or reading while writing would obviously be a problem as well, be it across goroutines, or between
2727
concurrent executions of the nerdctl binary, or embedded in a third-party application that does concurrently access resources
2828

2929
The `pkg/store` package does provide a "storage" abstraction that takes care of these issues, generally providing

docs/dir.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Data volume
6565

6666
Can be overridden with `nerdctl --cni-netconfpath=<NETCONFPATH>` flag and environment variable `$NETCONFPATH`.
6767

68-
At the top-level of <NETCONFPATH>, network (files) are shared accross all namespaces.
68+
At the top-level of <NETCONFPATH>, network (files) are shared across all namespaces.
6969
Sub-folders inside <NETCONFPATH> are only available to the namespace bearing the same name,
7070
and its networks definitions are private.
7171

pkg/buildkitutil/buildkitutil_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"testing"
3030

3131
"gotest.tools/v3/assert"
32+
33+
"github.com/containerd/nerdctl/v2/pkg/internal/filesystem"
3234
)
3335

3436
func TestBuildKitFile(t *testing.T) {
@@ -55,7 +57,7 @@ func TestBuildKitFile(t *testing.T) {
5557
{
5658
name: "only Dockerfile is present",
5759
prepare: func(t *testing.T) error {
58-
return os.WriteFile(filepath.Join(tmp, DefaultDockerfileName), []byte{}, 0644)
60+
return filesystem.WriteFile(filepath.Join(tmp, DefaultDockerfileName), []byte{}, 0644)
5961
},
6062
args: args{".", ""},
6163
wantAbsDir: tmp,
@@ -65,7 +67,7 @@ func TestBuildKitFile(t *testing.T) {
6567
{
6668
name: "only Containerfile is present",
6769
prepare: func(t *testing.T) error {
68-
return os.WriteFile(filepath.Join(tmp, "Containerfile"), []byte{}, 0644)
70+
return filesystem.WriteFile(filepath.Join(tmp, "Containerfile"), []byte{}, 0644)
6971
},
7072
args: args{".", ""},
7173
wantAbsDir: tmp,
@@ -75,11 +77,11 @@ func TestBuildKitFile(t *testing.T) {
7577
{
7678
name: "both Dockerfile and Containerfile are present",
7779
prepare: func(t *testing.T) error {
78-
var err = os.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte{}, 0644)
80+
var err = filesystem.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte{}, 0644)
7981
if err != nil {
8082
return err
8183
}
82-
return os.WriteFile(filepath.Join(tmp, "Containerfile"), []byte{}, 0644)
84+
return filesystem.WriteFile(filepath.Join(tmp, "Containerfile"), []byte{}, 0644)
8385
},
8486
args: args{".", ""},
8587
wantAbsDir: tmp,
@@ -89,11 +91,11 @@ func TestBuildKitFile(t *testing.T) {
8991
{
9092
name: "Dockerfile and Containerfile have different contents",
9193
prepare: func(t *testing.T) error {
92-
var err = os.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte{'d'}, 0644)
94+
var err = filesystem.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte{'d'}, 0644)
9395
if err != nil {
9496
return err
9597
}
96-
return os.WriteFile(filepath.Join(tmp, "Containerfile"), []byte{'c'}, 0644)
98+
return filesystem.WriteFile(filepath.Join(tmp, "Containerfile"), []byte{'c'}, 0644)
9799
},
98100
args: args{".", ""},
99101
wantAbsDir: tmp,
@@ -103,7 +105,7 @@ func TestBuildKitFile(t *testing.T) {
103105
{
104106
name: "Custom file is specfied",
105107
prepare: func(t *testing.T) error {
106-
return os.WriteFile(filepath.Join(tmp, "CustomFile"), []byte{}, 0644)
108+
return filesystem.WriteFile(filepath.Join(tmp, "CustomFile"), []byte{}, 0644)
107109
},
108110
args: args{".", "CustomFile"},
109111
wantAbsDir: tmp,
@@ -113,7 +115,7 @@ func TestBuildKitFile(t *testing.T) {
113115
{
114116
name: "Absolute path is specified along with custom file",
115117
prepare: func(t *testing.T) error {
116-
return os.WriteFile(filepath.Join(tmp, "CustomFile"), []byte{}, 0644)
118+
return filesystem.WriteFile(filepath.Join(tmp, "CustomFile"), []byte{}, 0644)
117119
},
118120
args: args{tmp, "CustomFile"},
119121
wantAbsDir: tmp,
@@ -123,7 +125,7 @@ func TestBuildKitFile(t *testing.T) {
123125
{
124126
name: "Absolute path is specified along with Docker file",
125127
prepare: func(t *testing.T) error {
126-
return os.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte{}, 0644)
128+
return filesystem.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte{}, 0644)
127129
},
128130
args: args{tmp, "."},
129131
wantAbsDir: tmp,
@@ -133,7 +135,7 @@ func TestBuildKitFile(t *testing.T) {
133135
{
134136
name: "Absolute path is specified with Container file in the path",
135137
prepare: func(t *testing.T) error {
136-
return os.WriteFile(filepath.Join(tmp, ContainerfileName), []byte{}, 0644)
138+
return filesystem.WriteFile(filepath.Join(tmp, ContainerfileName), []byte{}, 0644)
137139
},
138140
args: args{tmp, "."},
139141
wantAbsDir: tmp,

pkg/cmd/builder/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/containerd/nerdctl/v2/pkg/buildkitutil"
4242
"github.com/containerd/nerdctl/v2/pkg/clientutil"
4343
"github.com/containerd/nerdctl/v2/pkg/containerutil"
44+
"github.com/containerd/nerdctl/v2/pkg/internal/filesystem"
4445
"github.com/containerd/nerdctl/v2/pkg/platformutil"
4546
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
4647
"github.com/containerd/nerdctl/v2/pkg/strutil"
@@ -110,7 +111,7 @@ func Build(ctx context.Context, client *containerd.Client, options types.Builder
110111
if err != nil {
111112
return err
112113
}
113-
if err := os.WriteFile(options.IidFile, []byte(id), 0644); err != nil {
114+
if err := filesystem.WriteFile(options.IidFile, []byte(id), 0644); err != nil {
114115
return err
115116
}
116117
}

pkg/cmd/compose/compose.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/containerd/nerdctl/v2/pkg/composer"
3535
"github.com/containerd/nerdctl/v2/pkg/composer/serviceparser"
3636
"github.com/containerd/nerdctl/v2/pkg/imgutil"
37+
"github.com/containerd/nerdctl/v2/pkg/internal/filesystem"
3738
"github.com/containerd/nerdctl/v2/pkg/ipfs"
3839
"github.com/containerd/nerdctl/v2/pkg/netutil"
3940
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
@@ -136,7 +137,7 @@ func New(client *containerd.Client, globalOptions types.GlobalCommandOptions, op
136137
return err
137138
}
138139
defer os.RemoveAll(dir)
139-
if err := os.WriteFile(filepath.Join(dir, "api"), []byte(ipfsAddress), 0600); err != nil {
140+
if err := filesystem.WriteFile(filepath.Join(dir, "api"), []byte(ipfsAddress), 0600); err != nil {
140141
return err
141142
}
142143
ipfsPath = dir

pkg/cmd/container/create.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
"github.com/containerd/nerdctl/v2/pkg/imgutil"
5252
"github.com/containerd/nerdctl/v2/pkg/imgutil/load"
5353
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
54+
"github.com/containerd/nerdctl/v2/pkg/internal/filesystem"
5455
"github.com/containerd/nerdctl/v2/pkg/ipcutil"
5556
"github.com/containerd/nerdctl/v2/pkg/labels"
5657
"github.com/containerd/nerdctl/v2/pkg/logging"
@@ -951,7 +952,7 @@ func generateLogConfig(dataStore string, id string, logDriver string, logOpt []s
951952
}
952953

953954
logConfigFilePath := logging.LogConfigFilePath(dataStore, ns, id)
954-
if err = os.WriteFile(logConfigFilePath, logConfigB, 0600); err != nil {
955+
if err = filesystem.WriteFile(logConfigFilePath, logConfigB, 0600); err != nil {
955956
return logConfig, err
956957
}
957958

pkg/cmd/image/pull.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/containerd/nerdctl/v2/pkg/api/types"
2828
"github.com/containerd/nerdctl/v2/pkg/imgutil"
29+
"github.com/containerd/nerdctl/v2/pkg/internal/filesystem"
2930
"github.com/containerd/nerdctl/v2/pkg/ipfs"
3031
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
3132
"github.com/containerd/nerdctl/v2/pkg/signutil"
@@ -62,7 +63,7 @@ func EnsureImage(ctx context.Context, client *containerd.Client, rawRef string,
6263
return nil, err
6364
}
6465
defer os.RemoveAll(dir)
65-
if err := os.WriteFile(filepath.Join(dir, "api"), []byte(options.IPFSAddress), 0600); err != nil {
66+
if err := filesystem.WriteFile(filepath.Join(dir, "api"), []byte(options.IPFSAddress), 0600); err != nil {
6667
return nil, err
6768
}
6869
ipfsPath = dir

pkg/cmd/image/push.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
nerdconverter "github.com/containerd/nerdctl/v2/pkg/imgutil/converter"
4747
"github.com/containerd/nerdctl/v2/pkg/imgutil/dockerconfigresolver"
4848
"github.com/containerd/nerdctl/v2/pkg/imgutil/push"
49+
"github.com/containerd/nerdctl/v2/pkg/internal/filesystem"
4950
"github.com/containerd/nerdctl/v2/pkg/ipfs"
5051
"github.com/containerd/nerdctl/v2/pkg/platformutil"
5152
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
@@ -85,7 +86,7 @@ func Push(ctx context.Context, client *containerd.Client, rawRef string, options
8586
return err
8687
}
8788
defer os.RemoveAll(dir)
88-
if err := os.WriteFile(filepath.Join(dir, "api"), []byte(options.IpfsAddress), 0600); err != nil {
89+
if err := filesystem.WriteFile(filepath.Join(dir, "api"), []byte(options.IpfsAddress), 0600); err != nil {
8990
return err
9091
}
9192
ipfsPath = dir

0 commit comments

Comments
 (0)