Skip to content

Commit 67375d2

Browse files
committed
use atomicfile to create the file
1 parent c73b178 commit 67375d2

File tree

4 files changed

+32
-293
lines changed

4 files changed

+32
-293
lines changed

account_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ import (
2929
"go.uber.org/zap"
3030
)
3131

32-
// testingMemoryStorage is an in-memory storage implementation with known contents *and* fixed iteration order for List.
33-
type testingMemoryStorage struct {
34-
contents []testingMemoryStorageItem
32+
// memoryStorage is an in-memory storage implementation with known contents *and* fixed iteration order for List.
33+
type memoryStorage struct {
34+
contents []memoryStorageItem
3535
}
3636

37-
type testingMemoryStorageItem struct {
37+
type memoryStorageItem struct {
3838
key string
3939
data []byte
4040
}
4141

42-
func (m *testingMemoryStorage) lookup(_ context.Context, key string) *testingMemoryStorageItem {
42+
func (m *memoryStorage) lookup(_ context.Context, key string) *memoryStorageItem {
4343
for _, item := range m.contents {
4444
if item.key == key {
4545
return &item
4646
}
4747
}
4848
return nil
4949
}
50-
func (m *testingMemoryStorage) Delete(ctx context.Context, key string) error {
50+
func (m *memoryStorage) Delete(ctx context.Context, key string) error {
5151
for i, item := range m.contents {
5252
if item.key == key {
5353
m.contents = append(m.contents[:i], m.contents[i+1:]...)
@@ -56,14 +56,14 @@ func (m *testingMemoryStorage) Delete(ctx context.Context, key string) error {
5656
}
5757
return fs.ErrNotExist
5858
}
59-
func (m *testingMemoryStorage) Store(ctx context.Context, key string, value []byte) error {
60-
m.contents = append(m.contents, testingMemoryStorageItem{key: key, data: value})
59+
func (m *memoryStorage) Store(ctx context.Context, key string, value []byte) error {
60+
m.contents = append(m.contents, memoryStorageItem{key: key, data: value})
6161
return nil
6262
}
63-
func (m *testingMemoryStorage) Exists(ctx context.Context, key string) bool {
63+
func (m *memoryStorage) Exists(ctx context.Context, key string) bool {
6464
return m.lookup(ctx, key) != nil
6565
}
66-
func (m *testingMemoryStorage) List(ctx context.Context, path string, recursive bool) ([]string, error) {
66+
func (m *memoryStorage) List(ctx context.Context, path string, recursive bool) ([]string, error) {
6767
if recursive {
6868
panic("unimplemented")
6969
}
@@ -88,22 +88,22 @@ nextitem:
8888
}
8989
return result, nil
9090
}
91-
func (m *testingMemoryStorage) Load(ctx context.Context, key string) ([]byte, error) {
91+
func (m *memoryStorage) Load(ctx context.Context, key string) ([]byte, error) {
9292
if item := m.lookup(ctx, key); item != nil {
9393
return item.data, nil
9494
}
9595
return nil, fs.ErrNotExist
9696
}
97-
func (m *testingMemoryStorage) Stat(ctx context.Context, key string) (KeyInfo, error) {
97+
func (m *memoryStorage) Stat(ctx context.Context, key string) (KeyInfo, error) {
9898
if item := m.lookup(ctx, key); item != nil {
9999
return KeyInfo{Key: key, Size: int64(len(item.data))}, nil
100100
}
101101
return KeyInfo{}, fs.ErrNotExist
102102
}
103-
func (m *testingMemoryStorage) Lock(ctx context.Context, name string) error { panic("unimplemented") }
104-
func (m *testingMemoryStorage) Unlock(ctx context.Context, name string) error { panic("unimplemented") }
103+
func (m *memoryStorage) Lock(ctx context.Context, name string) error { panic("unimplemented") }
104+
func (m *memoryStorage) Unlock(ctx context.Context, name string) error { panic("unimplemented") }
105105

106-
var _ Storage = (*testingMemoryStorage)(nil)
106+
var _ Storage = (*memoryStorage)(nil)
107107

108108
type recordingStorage struct {
109109
Storage
@@ -293,7 +293,7 @@ func TestGetAccountAlreadyExistsSkipsBroken(t *testing.T) {
293293
am := &ACMEIssuer{CA: dummyCA, Logger: zap.NewNop(), mu: new(sync.Mutex)}
294294
testConfig := &Config{
295295
Issuers: []Issuer{am},
296-
Storage: &testingMemoryStorage{},
296+
Storage: &memoryStorage{},
297297
Logger: defaultTestLogger,
298298
certCache: new(Cache),
299299
}
@@ -342,7 +342,7 @@ func TestGetAccountWithEmailAlreadyExists(t *testing.T) {
342342
am := &ACMEIssuer{CA: dummyCA, Logger: zap.NewNop(), mu: new(sync.Mutex)}
343343
testConfig := &Config{
344344
Issuers: []Issuer{am},
345-
Storage: &recordingStorage{Storage: &testingMemoryStorage{}},
345+
Storage: &recordingStorage{Storage: &memoryStorage{}},
346346
Logger: defaultTestLogger,
347347
certCache: new(Cache),
348348
}

filestorage.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func fileLockIsStale(meta lockMeta) bool {
289289
// identified by filename. A successfully created
290290
// lockfile should be removed with removeLockfile.
291291
func createLockfile(filename string) error {
292-
err := atomicallyCreateFile(filename, true)
292+
err := atomicallyCreateFile(filename)
293293
if err != nil {
294294
return err
295295
}
@@ -376,29 +376,26 @@ func updateLockfileFreshness(filename string) (bool, error) {
376376

377377
// atomicallyCreateFile atomically creates the file
378378
// identified by filename if it doesn't already exist.
379-
func atomicallyCreateFile(filename string, writeLockInfo bool) error {
379+
func atomicallyCreateFile(filename string) error {
380380
// no need to check this error, we only really care about the file creation error
381381
_ = os.MkdirAll(filepath.Dir(filename), 0700)
382-
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0644)
382+
fp, err := atomicfile.New(filename, 0o600)
383383
if err != nil {
384+
// cancel the write if file creation fails and error out
384385
return err
385386
}
386-
defer f.Close()
387-
if writeLockInfo {
388-
now := time.Now()
389-
meta := lockMeta{
390-
Created: now,
391-
Updated: now,
392-
}
393-
if err := json.NewEncoder(f).Encode(meta); err != nil {
394-
return err
395-
}
396-
// see https://github.com/caddyserver/caddy/issues/3954
397-
if err := f.Sync(); err != nil {
398-
return err
399-
}
387+
now := time.Now()
388+
meta := lockMeta{
389+
Created: now,
390+
Updated: now,
400391
}
401-
return nil
392+
if err := json.NewEncoder(fp).Encode(meta); err != nil {
393+
// cancel the write if json encoding fails and error out
394+
fp.Cancel()
395+
return err
396+
}
397+
// close, thereby flushing the write
398+
return fp.Close()
402399
}
403400

404401
// homeDir returns the best guess of the current user's home

memorystorage.go

Lines changed: 0 additions & 186 deletions
This file was deleted.

0 commit comments

Comments
 (0)