-
Notifications
You must be signed in to change notification settings - Fork 261
feat(envd): add composite file upload API #2043
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 all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
bdd31f3
feat(envd): Add multi-part upload API endpoints
mishushakov 6d532d0
chore: auto-commit generated changes
github-actions[bot] 2e24715
fix(envd): fix multipart upload race condition, sort order, and atomi…
mishushakov 5e50a05
chore: auto-commit generated changes
github-actions[bot] dbce132
fix(envd): fix lint errors in multipart upload handlers
mishushakov d4cd3f4
fix(envd): fix TOCTOU race in multipart upload PUT vs Complete/Delete
mishushakov 33297f9
fix(envd): remove truncated part file on write failure
mishushakov 491abc0
fix(envd): include uploadId in tmpPath to avoid collision
mishushakov 98ef413
fix(envd): return 500 on abort cleanup failure instead of silent 204
mishushakov d20534a
fix(envd): fix temp file assertion to use actual path with uploadId
mishushakov bf77c08
fix(envd): re-register upload session on Complete failure to allow retry
mishushakov 1541da9
renamed meta to session
mishushakov eb1ccc5
replaced multi-part implementation with composite upload
mishushakov f5b2c9c
chore: auto-commit generated changes
github-actions[bot] 85d70b1
fix(envd): skip compose preservation test when running as root
mishushakov 3902310
fix(envd): reject compose when source path equals destination
mishushakov 44ea33d
fix(envd): validate source paths are regular files in compose
mishushakov 6000709
feat(envd): return EntryInfo from compose endpoint
mishushakov 7527f34
chore: auto-commit generated changes
github-actions[bot] d9a9d93
chore(envd): rename multipart files to compose and bump version to 0.5.5
mishushakov ed73d5b
Merge branch 'main' into mishushakov/riyadh-region
mishushakov 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,214 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "os/user" | ||
| "path/filepath" | ||
| "syscall" | ||
|
|
||
| "github.com/google/uuid" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/envd/internal/execcontext" | ||
| "github.com/e2b-dev/infra/packages/envd/internal/logs" | ||
| "github.com/e2b-dev/infra/packages/envd/internal/permissions" | ||
| ) | ||
|
|
||
| func (a *API) PostFilesCompose(w http.ResponseWriter, r *http.Request) { | ||
| defer r.Body.Close() | ||
|
|
||
| operationID := logs.AssignOperationID() | ||
|
|
||
| var req ComposeRequest | ||
| if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
| jsonError(w, http.StatusBadRequest, fmt.Errorf("invalid request body: %w", err)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if len(req.SourcePaths) == 0 { | ||
| jsonError(w, http.StatusBadRequest, fmt.Errorf("source_paths must not be empty")) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if req.Destination == "" { | ||
| jsonError(w, http.StatusBadRequest, fmt.Errorf("destination is required")) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| username, err := execcontext.ResolveDefaultUsername(req.Username, a.defaults.User) | ||
| if err != nil { | ||
| a.logger.Error().Err(err).Str(string(logs.OperationIDKey), operationID).Msg("no user specified") | ||
| jsonError(w, http.StatusBadRequest, err) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| u, err := user.Lookup(username) | ||
| if err != nil { | ||
| errMsg := fmt.Errorf("error looking up user '%s': %w", username, err) | ||
| a.logger.Error().Err(errMsg).Str(string(logs.OperationIDKey), operationID).Msg("user lookup failed") | ||
| jsonError(w, http.StatusUnauthorized, errMsg) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| uid, gid, err := permissions.GetUserIdInts(u) | ||
| if err != nil { | ||
| errMsg := fmt.Errorf("error getting user ids: %w", err) | ||
| a.logger.Error().Err(errMsg).Str(string(logs.OperationIDKey), operationID).Msg("failed to get user ids") | ||
| jsonError(w, http.StatusInternalServerError, errMsg) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| destPath, err := permissions.ExpandAndResolve(req.Destination, u, a.defaults.Workdir) | ||
| if err != nil { | ||
| errMsg := fmt.Errorf("error resolving destination path: %w", err) | ||
| a.logger.Error().Err(errMsg).Str(string(logs.OperationIDKey), operationID).Msg("path resolution failed") | ||
| jsonError(w, http.StatusBadRequest, errMsg) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| resolvedSources := make([]string, len(req.SourcePaths)) | ||
| for i, src := range req.SourcePaths { | ||
| resolved, err := permissions.ExpandAndResolve(src, u, a.defaults.Workdir) | ||
| if err != nil { | ||
| jsonError(w, http.StatusBadRequest, fmt.Errorf("error resolving source path %q: %w", src, err)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if resolved == destPath { | ||
| jsonError(w, http.StatusBadRequest, fmt.Errorf("source path %q cannot be the same as destination", src)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| info, err := os.Stat(resolved) | ||
|
||
| if err != nil { | ||
| jsonError(w, http.StatusNotFound, fmt.Errorf("source file not found: %s", src)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if !info.Mode().IsRegular() { | ||
| jsonError(w, http.StatusBadRequest, fmt.Errorf("source path is not a regular file: %s", src)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| resolvedSources[i] = resolved | ||
| } | ||
|
|
||
| err = permissions.EnsureDirs(filepath.Dir(destPath), uid, gid) | ||
| if err != nil { | ||
| jsonError(w, http.StatusInternalServerError, fmt.Errorf("error ensuring directories: %w", err)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| // Write to a temporary file and rename on success to avoid destroying | ||
| // any pre-existing file at destPath if assembly fails midway. | ||
| tmpPath := destPath + ".e2b-compose." + uuid.New().String() + ".tmp" | ||
|
|
||
| destFile, err := os.OpenFile(tmpPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o666) | ||
|
||
| if err != nil { | ||
| if errors.Is(err, syscall.ENOSPC) { | ||
| jsonError(w, http.StatusInsufficientStorage, fmt.Errorf("not enough disk space available")) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| jsonError(w, http.StatusInternalServerError, fmt.Errorf("error creating destination file: %w", err)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| err = os.Chown(tmpPath, uid, gid) | ||
|
||
| if err != nil { | ||
| destFile.Close() | ||
| os.Remove(tmpPath) | ||
|
||
| jsonError(w, http.StatusInternalServerError, fmt.Errorf("error changing file ownership: %w", err)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| var totalSize int64 | ||
|
|
||
| for _, srcPath := range resolvedSources { | ||
| srcFile, err := os.Open(srcPath) | ||
|
||
| if err != nil { | ||
| destFile.Close() | ||
| os.Remove(tmpPath) | ||
|
||
| jsonError(w, http.StatusInternalServerError, fmt.Errorf("error opening source file %s: %w", srcPath, err)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| // ReadFrom uses copy_file_range on Linux for zero-copy transfers | ||
| // between regular files — data moves kernel-side without touching | ||
| // userspace buffers. | ||
| n, err := destFile.ReadFrom(srcFile) | ||
| srcFile.Close() | ||
|
|
||
| if err != nil { | ||
| destFile.Close() | ||
| os.Remove(tmpPath) | ||
|
||
|
|
||
| if errors.Is(err, syscall.ENOSPC) { | ||
| jsonError(w, http.StatusInsufficientStorage, fmt.Errorf("not enough disk space available")) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| jsonError(w, http.StatusInternalServerError, fmt.Errorf("error composing source %s: %w", srcPath, err)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| totalSize += n | ||
| } | ||
|
|
||
| if err := destFile.Close(); err != nil { | ||
| os.Remove(tmpPath) | ||
|
||
| jsonError(w, http.StatusInternalServerError, fmt.Errorf("error closing destination file: %w", err)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if err := os.Rename(tmpPath, destPath); err != nil { | ||
|
||
| os.Remove(tmpPath) | ||
|
||
| jsonError(w, http.StatusInternalServerError, fmt.Errorf("error finalizing compose: %w", err)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| for _, srcPath := range resolvedSources { | ||
| os.Remove(srcPath) | ||
|
||
| } | ||
mishushakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| a.logger.Info(). | ||
| Str(string(logs.OperationIDKey), operationID). | ||
| Str("path", destPath). | ||
| Int("sources", len(resolvedSources)). | ||
| Int64("size", totalSize). | ||
| Msg("File compose completed") | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(http.StatusOK) | ||
|
|
||
| if err := json.NewEncoder(w).Encode(EntryInfo{ | ||
| Path: destPath, | ||
| Name: filepath.Base(destPath), | ||
| Type: File, | ||
| }); err != nil { | ||
| a.logger.Error().Err(err).Str(string(logs.OperationIDKey), operationID).Msg("failed to encode compose response") | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should probably be
compose.go