Skip to content
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

OCFS properly process empty file upload #734

Merged
merged 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/http/services/owncloud/ocdav/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func (s *svc) handlePut(w http.ResponseWriter, r *http.Request, ns string) {

tusc, err := tus.NewClient(dataServerURL, c)
if err != nil {
log.Error().Err(err).Msg("Could not get TUS client")
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -273,10 +274,12 @@ func (s *svc) handlePut(w http.ResponseWriter, r *http.Request, ns string) {
// start the uploading process.
err = uploader.Upload()
if err != nil {
log.Error().Err(err).Msg("Could not start TUS upload")
w.WriteHeader(http.StatusInternalServerError)
return
}

// stat again to check the new file's metadata
sRes, err = client.Stat(ctx, sReq)
if err != nil {
log.Error().Err(err).Msg("error sending grpc stat request")
Expand All @@ -285,6 +288,7 @@ func (s *svc) handlePut(w http.ResponseWriter, r *http.Request, ns string) {
}

if sRes.Status.Code != rpc.Code_CODE_OK {
log.Error().Err(err).Msgf("error status %d when sending grpc stat request", sRes.Status.Code)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
30 changes: 24 additions & 6 deletions pkg/storage/fs/eos/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ func (fs *eosfs) NewUpload(ctx context.Context, info tusd.FileInfo) (upload tusd
fs: fs,
}

if !info.SizeIsDeferred && info.Size == 0 {
log.Debug().Interface("info", info).Msg("eos: finishing upload for empty file")
// no need to create info file and finish directly
err := u.FinishUpload(ctx)
if err != nil {
return nil, err
}
return u, nil
}

// writeInfo creates the file by itself if necessary
err = u.writeInfo()
if err != nil {
Expand Down Expand Up @@ -285,12 +295,16 @@ func (upload *fileUpload) FinishUpload(ctx context.Context) error {
// cleanup in the background, delete might take a while and we don't need to wait for it to finish
go func() {
if err := os.Remove(upload.infoPath); err != nil {
log := appctx.GetLogger(ctx)
log.Err(err).Interface("info", upload.info).Msg("eos: could not delete upload info")
if !os.IsNotExist(err) {
log := appctx.GetLogger(ctx)
log.Err(err).Interface("info", upload.info).Msg("eos: could not delete upload info")
}
}
if err := os.Remove(upload.binPath); err != nil {
log := appctx.GetLogger(ctx)
log.Err(err).Interface("info", upload.info).Msg("eos: could not delete upload binary")
if !os.IsNotExist(err) {
log := appctx.GetLogger(ctx)
log.Err(err).Interface("info", upload.info).Msg("eos: could not delete upload binary")
}
}
}()
}
Expand All @@ -310,10 +324,14 @@ func (fs *eosfs) AsTerminatableUpload(upload tusd.Upload) tusd.TerminatableUploa
// Terminate terminates the upload
func (upload *fileUpload) Terminate(ctx context.Context) error {
if err := os.Remove(upload.infoPath); err != nil {
return err
if !os.IsNotExist(err) {
return err
}
}
if err := os.Remove(upload.binPath); err != nil {
return err
if !os.IsNotExist(err) {
return err
}
}
return nil
}
26 changes: 22 additions & 4 deletions pkg/storage/fs/owncloud/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (fs *ocfs) NewUpload(ctx context.Context, info tusd.FileInfo) (upload tusd.
"LogLevel": log.GetLevel().String(),
}
// Create binary file in the upload folder with no content
log.Debug().Interface("info", info).Msg("ocfs: built storage info")
file, err := os.OpenFile(binPath, os.O_CREATE|os.O_WRONLY, defaultFilePerm)
if err != nil {
return nil, err
Expand All @@ -170,6 +171,17 @@ func (fs *ocfs) NewUpload(ctx context.Context, info tusd.FileInfo) (upload tusd.
binPath: binPath,
infoPath: filepath.Join(fs.c.UploadInfoDir, info.ID+".info"),
fs: fs,
ctx: ctx,
}

if !info.SizeIsDeferred && info.Size == 0 {
log.Debug().Interface("info", info).Msg("ocfs: finishing upload for empty file")
// no need to create info file and finish directly
err := u.FinishUpload(ctx)
if err != nil {
return nil, err
}
return u, nil
}

// writeInfo creates the file by itself if necessary
Expand Down Expand Up @@ -340,10 +352,12 @@ func (upload *fileUpload) FinishUpload(ctx context.Context) error {

// only delete the upload if it was successfully written to eos
if err := os.Remove(upload.infoPath); err != nil {
log.Err(err).Interface("info", upload.info).Msg("ocfs: could not delete upload info")
if !os.IsNotExist(err) {
log.Err(err).Interface("info", upload.info).Msg("ocfs: could not delete upload info")
return err
}
}

// FIXME metadata propagation is left to the storage implementation
return upload.fs.propagate(upload.ctx, np)
}

Expand All @@ -359,10 +373,14 @@ func (fs *ocfs) AsTerminatableUpload(upload tusd.Upload) tusd.TerminatableUpload
// Terminate terminates the upload
func (upload *fileUpload) Terminate(ctx context.Context) error {
if err := os.Remove(upload.infoPath); err != nil {
return err
if !os.IsNotExist(err) {
return err
}
}
if err := os.Remove(upload.binPath); err != nil {
return err
if !os.IsNotExist(err) {
return err
}
}
return nil
}
Expand Down