Skip to content

Commit

Permalink
filelocker: Emit 404 if directory for lock does not exist (#1149)
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut authored Jun 27, 2024
1 parent 048417a commit ce54f1e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pkg/filelocker/filelocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package filelocker

import (
"context"
"errors"
"io/fs"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -104,6 +106,17 @@ func (lock fileUploadLock) Lock(ctx context.Context, requestRelease func()) erro
continue
}
}
// If the upload ID uses a folder structure (e.g. projectA/upload1), the directory
// (e.g. projectA) might not exist, if no such upload exists already. In those cases,
// we just return ErrNotFound because no such upload exists anyways.
// TODO: This assumes that filelocker is used mostly with filestore, which is likely
// true, but does not have to be. If another storage backend is used, we cannot make
// any assumption about the folder structure. As an alternative, we should consider
// normalizing the upload ID to remove the folder structure as well an turn projectA/upload1
// into projectA-upload1.
if errors.Is(err, fs.ErrNotExist) {
return handler.ErrNotFound
}
if err != lockfile.ErrBusy {
// If we get something different than ErrBusy, bubble the error up.
return err
Expand Down
22 changes: 21 additions & 1 deletion pkg/filelocker/filelocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestFileLocker_Timeout(t *testing.T) {
assertEmptyDirectory(dir, a)
}

func TestMemoryLocker_RequestUnlock(t *testing.T) {
func TestFileLocker_RequestUnlock(t *testing.T) {
a := assert.New(t)

dir, err := os.MkdirTemp("", "tusd-file-locker")
Expand Down Expand Up @@ -95,6 +95,26 @@ func TestMemoryLocker_RequestUnlock(t *testing.T) {
assertEmptyDirectory(dir, a)
}

func TestFileLocker_DirectoryNotFound(t *testing.T) {
a := assert.New(t)

dir, err := os.MkdirTemp("", "tusd-file-locker")
a.NoError(err)

locker := New(dir)

// The upload ID uses a directory structure, but the corresponding directories do
// not exist. Since there can also exist no info file in this folder, we expect
// the locker to return ErrNotFound
lock, err := locker.NewLock("nested/folder/structure/upload")
a.Nil(err)

err = lock.Lock(context.Background(), func() {
panic("must not be called")
})
a.Equal(handler.ErrNotFound, err)
}

func assertEmptyDirectory(dir string, a *assert.Assertions) {
file, err := os.Open(dir)
a.NoError(err)
Expand Down

0 comments on commit ce54f1e

Please sign in to comment.