mirror of
https://git.sr.ht/~sircmpwn/tokidoki
synced 2025-12-12 06:07:22 +01:00
storage/filesystem: atomically check for IfNoneMatch
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.
This commit is contained in:
parent
a3bfd56bf9
commit
228384530e
1 changed files with 16 additions and 11 deletions
|
|
@ -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()
|
||||
|
|
@ -602,13 +605,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()
|
||||
|
|
@ -620,5 +626,4 @@ func (b *filesystemBackend) PutCalendarObject(ctx context.Context, objPath strin
|
|||
}
|
||||
|
||||
return objPath, nil
|
||||
return "", nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue