mirror of
https://github.com/stashapp/stash.git
synced 2025-12-18 04:44:37 +03:00
Handle modified files where the case of the filename changed on case-insensitive filesystems (#6327)
* Find existing files with case insensitivity if filesystem is case insensitive * Handle case change in folders * Optimise to only test file system case sensitivity if the first query found nothing This limits the overhead to new paths, and adds an extra query for new paths to windows installs
This commit is contained in:
@@ -625,9 +625,9 @@ func (qb *FileStore) find(ctx context.Context, id models.FileID) (models.File, e
|
||||
}
|
||||
|
||||
// FindByPath returns the first file that matches the given path. Wildcard characters are supported.
|
||||
func (qb *FileStore) FindByPath(ctx context.Context, p string) (models.File, error) {
|
||||
func (qb *FileStore) FindByPath(ctx context.Context, p string, caseSensitive bool) (models.File, error) {
|
||||
|
||||
ret, err := qb.FindAllByPath(ctx, p)
|
||||
ret, err := qb.FindAllByPath(ctx, p, caseSensitive)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -642,7 +642,7 @@ func (qb *FileStore) FindByPath(ctx context.Context, p string) (models.File, err
|
||||
|
||||
// FindAllByPath returns all the files that match the given path.
|
||||
// Wildcard characters are supported.
|
||||
func (qb *FileStore) FindAllByPath(ctx context.Context, p string) ([]models.File, error) {
|
||||
func (qb *FileStore) FindAllByPath(ctx context.Context, p string, caseSensitive bool) ([]models.File, error) {
|
||||
// separate basename from path
|
||||
basename := filepath.Base(p)
|
||||
dirName := filepath.Dir(p)
|
||||
@@ -657,7 +657,7 @@ func (qb *FileStore) FindAllByPath(ctx context.Context, p string) ([]models.File
|
||||
// like uses case-insensitive matching. Only use like if wildcards are used
|
||||
q := qb.selectDataset().Prepared(true)
|
||||
|
||||
if strings.Contains(basename, "%") || strings.Contains(dirName, "%") {
|
||||
if strings.Contains(basename, "%") || strings.Contains(dirName, "%") || !caseSensitive {
|
||||
q = q.Where(
|
||||
folderTable.Col("path").Like(dirName),
|
||||
table.Col("basename").Like(basename),
|
||||
|
||||
@@ -551,7 +551,7 @@ func Test_FileStore_FindByPath(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
runWithRollbackTxn(t, tt.name, func(t *testing.T, ctx context.Context) {
|
||||
assert := assert.New(t)
|
||||
got, err := qb.FindByPath(ctx, tt.path)
|
||||
got, err := qb.FindByPath(ctx, tt.path, true)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FileStore.FindByPath() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
|
||||
@@ -292,8 +292,16 @@ func (qb *FolderStore) FindMany(ctx context.Context, ids []models.FolderID) ([]*
|
||||
return folders, nil
|
||||
}
|
||||
|
||||
func (qb *FolderStore) FindByPath(ctx context.Context, p string) (*models.Folder, error) {
|
||||
q := qb.selectDataset().Prepared(true).Where(qb.table().Col("path").Eq(p))
|
||||
func (qb *FolderStore) FindByPath(ctx context.Context, p string, caseSensitive bool) (*models.Folder, error) {
|
||||
// use like for case insensitive search
|
||||
var criterion exp.BooleanExpression
|
||||
if caseSensitive {
|
||||
criterion = qb.table().Col("path").Eq(p)
|
||||
} else {
|
||||
criterion = qb.table().Col("path").ILike(p)
|
||||
}
|
||||
|
||||
q := qb.selectDataset().Prepared(true).Where(criterion)
|
||||
|
||||
ret, err := qb.get(ctx, q)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
|
||||
@@ -89,7 +89,7 @@ func Test_FolderStore_Create(t *testing.T) {
|
||||
assert.Equal(copy, s)
|
||||
|
||||
// ensure can find the folder
|
||||
found, err := qb.FindByPath(ctx, path)
|
||||
found, err := qb.FindByPath(ctx, path, true)
|
||||
if err != nil {
|
||||
t.Errorf("FolderStore.Find() error = %v", err)
|
||||
}
|
||||
@@ -180,7 +180,7 @@ func Test_FolderStore_Update(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
s, err := qb.FindByPath(ctx, path)
|
||||
s, err := qb.FindByPath(ctx, path, true)
|
||||
if err != nil {
|
||||
t.Errorf("FolderStore.Find() error = %v", err)
|
||||
}
|
||||
@@ -228,7 +228,7 @@ func Test_FolderStore_FindByPath(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
runWithRollbackTxn(t, tt.name, func(t *testing.T, ctx context.Context) {
|
||||
got, err := qb.FindByPath(ctx, tt.path)
|
||||
got, err := qb.FindByPath(ctx, tt.path, true)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FolderStore.FindByPath() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user