~sircmpwn/tokidoki-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH] storage/filesystem: atomically check for IfNoneMatch

Details
Message ID
<20220603063346.157522-1-contact@emersion.fr>
DKIM signature
missing
Download raw message
Patch: +16 -11
Using a separate os.Stat() call may result in a race where another
request handler running concurrently creates the file in-between
the os.Stat() call and the os.Create() call.

Use O_EXCL to avoid this situation.
---
 storage/filesystem.go | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/storage/filesystem.go b/storage/filesystem.go
index ce9f00bf468a..79ce1f218e23 100644
--- a/storage/filesystem.go
+++ b/storage/filesystem.go
@@ -425,13 +425,16 @@ func (b *filesystemBackend) PutAddressObject(ctx context.Context, objPath string
		return "", err
	}

	// TODO handle IfMatch
	if _, err := os.Stat(localPath); !os.IsNotExist(err) && opts.IfNoneMatch {
		return "", carddav.NewPreconditionError(carddav.PreconditionNoUIDConflict)
	flags := os.O_RDWR | os.O_CREATE | os.O_TRUNC
	if opts.IfNoneMatch {
		flags |= os.O_EXCL
	}

	f, err := os.Create(localPath)
	if err != nil {
	// TODO handle IfMatch
	f, err := os.OpenFile(localPath, flags, 0666)
	if os.IsExist(err) {
		return "", carddav.NewPreconditionError(carddav.PreconditionNoUIDConflict)
	} else if err != nil {
		return "", err
	}
	defer f.Close()
@@ -604,13 +607,16 @@ func (b *filesystemBackend) PutCalendarObject(ctx context.Context, objPath strin
		return "", err
	}

	// TODO handle IfMatch
	if _, err := os.Stat(localPath); !os.IsNotExist(err) && opts.IfNoneMatch {
		return "", caldav.NewPreconditionError(caldav.PreconditionNoUIDConflict)
	flags := os.O_RDWR | os.O_CREATE | os.O_TRUNC
	if opts.IfNoneMatch {
		flags |= os.O_EXCL
	}

	f, err := os.Create(localPath)
	if err != nil {
	// TODO handle IfMatch
	f, err := os.OpenFile(localPath, flags, 0666)
	if os.IsExist(err) {
		return "", caldav.NewPreconditionError(caldav.PreconditionNoUIDConflict)
	} else if err != nil {
		return "", err
	}
	defer f.Close()
@@ -622,5 +628,4 @@ func (b *filesystemBackend) PutCalendarObject(ctx context.Context, objPath strin
	}

	return objPath, nil
	return "", nil
}

base-commit: 04a422523a008a655e3b4eac3ae7b524b3afc38b
-- 
2.36.1
Details
Message ID
<196d6444-3db5-23af-f168-302b98e2ac17@bitfehler.net>
In-Reply-To
<20220603063346.157522-1-contact@emersion.fr> (view parent)
DKIM signature
missing
Download raw message
Applied, thanks!
Reply to thread Export thread (mbox)