-
Notifications
You must be signed in to change notification settings - Fork 894
feat: return better error if file size is too big to upload #7775
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
Changes from 1 commit
47e3480
b34e9d8
0cd070c
7b2844d
ec5604e
3e368a4
cf35412
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ func Tar(w io.Writer, directory string, limit int64) error { | |
) | ||
} | ||
|
||
fileTooBigError := xerrors.Errorf("Archive too big. Must be <= %d bytes", limit) | ||
err = filepath.Walk(directory, func(file string, fileInfo os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
|
@@ -95,6 +96,10 @@ func Tar(w io.Writer, directory string, limit int64) error { | |
if !fileInfo.Mode().IsRegular() { | ||
return nil | ||
} | ||
// Before we even open the file, check if it is going to exceed our limit. | ||
if fileInfo.Size()+totalSize >= limit { | ||
return fileTooBigError | ||
} | ||
data, err := os.Open(file) | ||
if err != nil { | ||
return err | ||
|
@@ -106,7 +111,7 @@ func Tar(w io.Writer, directory string, limit int64) error { | |
} | ||
totalSize += wrote | ||
if limit != 0 && totalSize >= limit { | ||
return xerrors.Errorf("Archive too big. Must be <= %d bytes", limit) | ||
return fileTooBigError | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept this because this is checking the actual bytes written. I can't imagine the
But the |
||
return data.Close() | ||
}) | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
Either the error or this (+ the other) check are wrong? Error says
<=
, this says>=
, who's right?Another observation is that this doesn't account for tar overhead, AFAICT. So we might end up with an archive that's too large anyway at which point we would hit the original issue again (bad message from API)?
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.
Perhaps we can wrap
w
(wc := writeCounter{w}
) before passing it on to tar writer, and count bytes written to it? Then we don't need to trackwrote
here and just checkwc.written
.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.
You are right about the headers. Honestly I didn't look too deep at the code and just assumed the
totalSize
was being tracked correctly 🤦.As for the
<=
vs>=
, I'll change the conditional to>
on the check to be consistent with the api.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.
@mafredri good catch. I am using a limit writer now
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.
Writing a test or two