Skip to content

Commit

Permalink
FMWK-556-restore-folder-validation
Browse files Browse the repository at this point in the history
- added validator
- added nested dirs
  • Loading branch information
filkeith committed Oct 6, 2024
1 parent 15313fa commit 9fe0f09
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 18 deletions.
15 changes: 12 additions & 3 deletions io/aws/s3/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,18 @@ func (r *Reader) checkRestoreDirectory(ctx context.Context) error {
if p.Key == nil || isDirectory(r.prefix, *p.Key) && !r.withNestedDir {
continue
}
// If we found anything, then folder is not empty.
if p.Key != nil {
return nil

switch {
case r.validator != nil:
// If we found a valid file, return.
if err = r.validator.Run(*p.Key); err == nil {
return nil
}
default:
// If we found anything, then folder is not empty.
if p.Key != nil {
return nil
}
}
}

Expand Down
15 changes: 12 additions & 3 deletions io/azure/blob/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,18 @@ func (r *Reader) checkRestoreDirectory(ctx context.Context) error {
if isDirectory(r.prefix, *blob.Name) && !r.withNestedDir {
continue
}
// If we found anything, then folder is not empty.
if blob.Name != nil {
return nil

switch {
case r.validator != nil:
// If we found a valid file, return.
if err = r.validator.Run(*blob.Name); err == nil {
return nil
}
default:
// If we found anything, then folder is not empty.
if blob.Name != nil {
return nil
}
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions io/gcp/storage/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,18 @@ func (r *Reader) checkRestoreDirectory(ctx context.Context) error {
if isDirectory(r.prefix, objAttrs.Name) && !r.withNestedDir {
continue
}
// If we found anything, then folder is not empty.
if objAttrs.Name != "" {
return nil

switch {
case r.validator != nil:
// If we found a valid file, return.
if err = r.validator.Run(objAttrs.Name); err == nil {
return nil
}
default:
// If we found anything, then folder is not empty.
if objAttrs.Name != "" {
return nil
}
}
}

Expand Down
1 change: 1 addition & 0 deletions io/gcp/storage/reader_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func (s *GCPSuite) TestReader_StreamFilesEmpty() {
testBucketName,
WithDir(testReadFolderEmpty),
WithValidator(validatorMock{}),
WithNestedDir(),
)
s.Require().NoError(err)

Expand Down
36 changes: 29 additions & 7 deletions io/local/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (r *Reader) StreamFiles(
defer close(readersCh)
// If it is a folder, open and return.
if r.isDir {
err := r.checkRestoreDirectory()
err := r.checkRestoreDirectory(r.path)
if err != nil {
errorsCh <- err
return
Expand Down Expand Up @@ -201,9 +201,7 @@ func (r *Reader) streamFile(

// checkRestoreDirectory checks that the restore directory exists,
// is a readable directory, and contains backup files of the correct format.
func (r *Reader) checkRestoreDirectory() error {
dir := r.path

func (r *Reader) checkRestoreDirectory(dir string) error {
dirInfo, err := os.Stat(dir)
if err != nil {
// Handle the error
Expand All @@ -220,9 +218,33 @@ func (r *Reader) checkRestoreDirectory() error {
return fmt.Errorf("failed to read path %s: %w", dir, err)
}

// Check if the directory is empty
if len(fileInfo) == 0 {
return fmt.Errorf("%s is empty", dir)
switch {
case r.validator != nil:
for _, file := range fileInfo {
if file.IsDir() {
// Iterate over nested dirs recursively.
if r.withNestedDir {
nestedDir := filepath.Join(r.path, file.Name())
// If the nested folder is ok, then return nil.
err = r.checkRestoreDirectory(nestedDir)
if err != nil {
return err
}
}

continue
}

// If we found a valid file, return.
if err = r.validator.Run(file.Name()); err == nil {
return nil
}
}
default:
// Check if the directory is empty
if len(fileInfo) == 0 {
return fmt.Errorf("%s is empty", dir)
}
}

return nil
Expand Down
5 changes: 3 additions & 2 deletions io/local/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ func (s *checkRestoreDirectoryTestSuite) TestCheckRestoreDirectory_Negative_Empt
return fmt.Errorf("invalid file extension")
})

reader, _ := NewReader(WithValidator(mockValidator), WithDir(dir))
err := reader.checkRestoreDirectory()
reader, err := NewReader(WithValidator(mockValidator), WithDir(dir))
s.NoError(err)
err = reader.checkRestoreDirectory(dir)
s.Error(err)
}

Expand Down

0 comments on commit 9fe0f09

Please sign in to comment.