Skip to content

Commit bec50ce

Browse files
committed
Clean up
1 parent 76d9dbd commit bec50ce

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

internal/file/file_manager_service.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -393,34 +393,42 @@ func (fms *FileManagerService) DetermineFileActions(
393393
continue
394394
}
395395

396+
fileStats, statErr := os.Stat(fileName)
397+
396398
// If file doesn't exist on disk.
397399
// Treat it as adding a new file.
398-
if fileStats, statErr := os.Stat(fileName); errors.Is(statErr, os.ErrNotExist) {
400+
if errors.Is(statErr, os.ErrNotExist) {
399401
slog.DebugContext(ctx, "New untracked file to created", "file_name", fileName)
400402
modifiedFile.Action = model.Add
401403
fileDiff[fileName] = modifiedFile
402404

403405
continue
404-
// If file already exists on disk but is not being tracked in manifest and the file hash is different.
405-
// Treat it as a file update.
406-
} else if statErr == nil {
407-
if fileStats.IsDir() {
408-
return nil, fmt.Errorf(
409-
"unable to create file %s since a directory with the same name already exists on the data plane",
410-
fileName,
411-
)
412-
}
406+
}
413407

414-
metadataOfFileOnDisk, err := files.FileMeta(fileName)
415-
if err != nil {
416-
return nil, fmt.Errorf("unable to get file metadata for %s: %w", fileName, err)
417-
}
408+
// If there is an error other than not existing, return that error.
409+
if statErr != nil {
410+
return nil, fmt.Errorf("unable to stat file %s: %w", fileName, statErr)
411+
}
418412

419-
if metadataOfFileOnDisk.GetHash() != modifiedFile.File.GetFileMeta().GetHash() {
420-
slog.DebugContext(ctx, "Untracked file requires updating", "file_name", fileName)
421-
modifiedFile.Action = model.Update
422-
fileDiff[fileName] = modifiedFile
423-
}
413+
// If there is a directory with the same name, return an error.
414+
if fileStats.IsDir() {
415+
return nil, fmt.Errorf(
416+
"unable to create file %s since a directory with the same name already exists",
417+
fileName,
418+
)
419+
}
420+
421+
// If file already exists on disk but is not being tracked in manifest and the file hash is different.
422+
// Treat it as a file update.
423+
metadataOfFileOnDisk, err := files.FileMeta(fileName)
424+
if err != nil {
425+
return nil, fmt.Errorf("unable to get file metadata for %s: %w", fileName, err)
426+
}
427+
428+
if metadataOfFileOnDisk.GetHash() != modifiedFile.File.GetFileMeta().GetHash() {
429+
slog.DebugContext(ctx, "Untracked file requires updating", "file_name", fileName)
430+
modifiedFile.Action = model.Update
431+
fileDiff[fileName] = modifiedFile
424432
}
425433
}
426434

internal/file/file_manager_service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
819819
expectedCache: map[string]*model.FileCache(nil),
820820
expectedContent: make(map[string][]byte),
821821
expectedError: fmt.Errorf(
822-
"unable to create file %s since a directory with the same name already exists on the data plane",
822+
"unable to create file %s since a directory with the same name already exists",
823823
tempDir,
824824
),
825825
},

0 commit comments

Comments
 (0)