-
Notifications
You must be signed in to change notification settings - Fork 347
Simplify delete file|dir to just delete path #2181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
77fbe92
fix implementation
djeebus 1b3374a
remove obsolete methods, common now called 'path'
djeebus c082c1c
rename another handler
djeebus 7b81a53
Merge remote-tracking branch 'origin/main' into simplify-deletes
djeebus b4522b8
fix broken symlink handling
djeebus fec8cfa
parallel tests are hard
djeebus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package volumes | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
|
|
||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/trace" | ||
| "google.golang.org/grpc/codes" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator" | ||
| ) | ||
|
|
||
| func (s *Service) DeletePath(ctx context.Context, request *orchestrator.DeletePathRequest) (r *orchestrator.DeletePathResponse, err error) { | ||
| ctx, span := tracer.Start(ctx, "delete file in volume") | ||
| defer func() { | ||
| setSpanStatus(span, err) | ||
| span.End() | ||
| }() | ||
|
|
||
| fs, path, errResponse := s.getFilesystemAndPath(ctx, request) | ||
| if errResponse != nil { | ||
| return nil, errResponse.Err() | ||
| } | ||
| defer fs.Close() | ||
|
|
||
| if s.isRoot(path) { | ||
| return nil, newAPIError(ctx, | ||
| codes.InvalidArgument, | ||
| http.StatusBadRequest, | ||
| orchestrator.UserErrorCode_CANNOT_DELETE_ROOT, | ||
| "cannot delete root", | ||
| ).Err() | ||
| } | ||
|
|
||
| span.AddEvent("deleting file", trace.WithAttributes( | ||
| attribute.String("path", path), | ||
| )) | ||
|
|
||
| if err = fs.RemoveAll(path); err != nil { | ||
| if os.IsNotExist(err) { | ||
| return nil, newAPIError(ctx, | ||
| codes.NotFound, | ||
| http.StatusNotFound, | ||
| orchestrator.UserErrorCode_PATH_NOT_FOUND, | ||
| "failed to delete: %q not found.", request.GetPath(), | ||
| ).Err() | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("failed to delete path: %w", err) | ||
| } | ||
|
|
||
| return &orchestrator.DeletePathResponse{}, nil | ||
| } | ||
136 changes: 136 additions & 0 deletions
136
packages/orchestrator/internal/volumes/path_delete_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| package volumes | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/grpc/codes" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator" | ||
| ) | ||
|
|
||
| func TestDeletePath(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| s, tmpdir, volumeInfo := setupTestService(t) | ||
|
|
||
| t.Run("delete file", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| filename := "file-to-delete.txt" | ||
| fullPath := filepath.Join(tmpdir, filename) | ||
|
|
||
| err := os.WriteFile(fullPath, []byte("test content"), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = s.DeletePath(t.Context(), &orchestrator.DeletePathRequest{ | ||
| Volume: volumeInfo, | ||
| Path: filename, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = os.Stat(fullPath) | ||
| require.ErrorIs(t, err, os.ErrNotExist) | ||
| }) | ||
|
|
||
| t.Run("delete empty directory", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dirname := "empty-dir-to-delete" | ||
| fullPath := filepath.Join(tmpdir, dirname) | ||
|
|
||
| err := os.Mkdir(fullPath, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = s.DeletePath(t.Context(), &orchestrator.DeletePathRequest{ | ||
| Volume: volumeInfo, | ||
| Path: dirname, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = os.Stat(fullPath) | ||
| require.ErrorIs(t, err, os.ErrNotExist) | ||
| }) | ||
|
|
||
| t.Run("delete non-empty directory recursively", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dirname := "non-empty-dir" | ||
| fullPath := filepath.Join(tmpdir, dirname) | ||
|
|
||
| err := os.Mkdir(fullPath, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| err = os.WriteFile(filepath.Join(fullPath, "child.txt"), []byte("child content"), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| // Create nested subdirectory with file | ||
| nestedDir := filepath.Join(fullPath, "nested") | ||
| err = os.Mkdir(nestedDir, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| err = os.WriteFile(filepath.Join(nestedDir, "nested-child.txt"), []byte("nested content"), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = s.DeletePath(t.Context(), &orchestrator.DeletePathRequest{ | ||
| Volume: volumeInfo, | ||
| Path: dirname, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| // Directory and all contents should be deleted | ||
| _, err = os.Stat(fullPath) | ||
| require.ErrorIs(t, err, os.ErrNotExist) | ||
| }) | ||
|
|
||
| t.Run("delete non-existent path", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| _, err := s.DeletePath(t.Context(), &orchestrator.DeletePathRequest{ | ||
| Volume: volumeInfo, | ||
| Path: "non-existent-path", | ||
| }) | ||
| requireGRPCError(t, err, codes.NotFound, orchestrator.UserErrorCode_PATH_NOT_FOUND) | ||
| }) | ||
|
|
||
| t.Run("delete root fails", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| _, err := s.DeletePath(t.Context(), &orchestrator.DeletePathRequest{ | ||
| Volume: volumeInfo, | ||
| Path: "/", | ||
| }) | ||
| requireGRPCError(t, err, codes.InvalidArgument, orchestrator.UserErrorCode_CANNOT_DELETE_ROOT) | ||
| }) | ||
|
|
||
| t.Run("delete symlink", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| target := "symlink-target.txt" | ||
| link := "symlink-to-delete" | ||
| targetPath := filepath.Join(tmpdir, target) | ||
| linkPath := filepath.Join(tmpdir, link) | ||
|
|
||
| err := os.WriteFile(targetPath, []byte("target content"), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| err = os.Symlink(target, linkPath) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = s.DeletePath(t.Context(), &orchestrator.DeletePathRequest{ | ||
| Volume: volumeInfo, | ||
| Path: link, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| // Symlink should be deleted | ||
| _, err = os.Lstat(linkPath) | ||
| require.ErrorIs(t, err, os.ErrNotExist) | ||
|
|
||
| // Target should still exist | ||
| _, err = os.Stat(targetPath) | ||
| require.NoError(t, err) | ||
| }) | ||
| } |
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.