Skip to content

Commit

Permalink
cr: move DeleteAllFiles to be part of the backend interface
Browse files Browse the repository at this point in the history
  • Loading branch information
drehelis committed Jan 4, 2025
1 parent 259c53f commit 9e09a18
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
25 changes: 5 additions & 20 deletions fakestorage/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,31 +363,16 @@ func (s *Server) reseedServer(r *http.Request) jsonResponse {
}

func (s *Server) deleteAllFiles(r *http.Request) jsonResponse {
storageRoot := s.options.StorageRoot
storageType := "filesystem"

if storageRoot != "" {
if err := os.RemoveAll(storageRoot); err != nil {
return jsonResponse{
status: http.StatusInternalServerError,
errorMessage: err.Error(),
}
if err := s.backend.DeleteAllFiles(); err != nil {
return jsonResponse{
status: http.StatusInternalServerError,
errorMessage: err.Error(),
}

if err := os.MkdirAll(filepath.Join(storageRoot), 0o700); err != nil {
return jsonResponse{
status: http.StatusInternalServerError,
errorMessage: err.Error(),
}
}
} else {
storageType = "memory"
s.backend, _ = backend.NewStorageMemory(nil)
}

return jsonResponse{
status: http.StatusOK,
data: map[string]string{"message": fmt.Sprintf("All files deleted successfully from %s", storageType)},
data: map[string]string{"message": "All files deleted successfully"},
}
}

Expand Down
9 changes: 9 additions & 0 deletions internal/backend/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,12 @@ func (s *storageFS) ComposeObject(bucketName string, objectNames []string, desti

return result, nil
}

func (s *storageFS) DeleteAllFiles() error {
s.mtx.Lock()
defer s.mtx.Unlock()
if err := os.RemoveAll(s.rootDir); err != nil {
return err
}
return os.MkdirAll(s.rootDir, 0o700)
}
7 changes: 7 additions & 0 deletions internal/backend/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,10 @@ func (s *storageMemory) ComposeObject(bucketName string, objectNames []string, d

return result, nil
}

func (s *storageMemory) DeleteAllFiles() error {
s.mtx.Lock()
defer s.mtx.Unlock()
s.buckets = make(map[string]bucketInMemory)
return nil
}
1 change: 1 addition & 0 deletions internal/backend/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Storage interface {
PatchObject(bucketName, objectName string, attrsToUpdate ObjectAttrs) (StreamingObject, error)
UpdateObject(bucketName, objectName string, attrsToUpdate ObjectAttrs) (StreamingObject, error)
ComposeObject(bucketName string, objectNames []string, destinationName string, metadata map[string]string, contentType string) (StreamingObject, error)
DeleteAllFiles() error
}

type Error string
Expand Down

0 comments on commit 9e09a18

Please sign in to comment.