diff --git a/storage/filesystem.go b/storage/filesystem.go index 0d9f09d..fc614e4 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() @@ -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 }