mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-08 15:28:05 +03:00
zip creation: avoid stat if not strictly required
Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
@@ -999,7 +999,7 @@ func getFileWriter(conn *BaseConnection, virtualPath string, expectedSize int64)
|
||||
return w, numFiles, truncatedSize, cancelFn, nil
|
||||
}
|
||||
|
||||
func addZipEntry(wr *zipWriterWrapper, conn *BaseConnection, entryPath, baseDir string, recursion int) error {
|
||||
func addZipEntry(wr *zipWriterWrapper, conn *BaseConnection, entryPath, baseDir string, info os.FileInfo, recursion int) error { //nolint:gocyclo
|
||||
if entryPath == wr.Name {
|
||||
// skip the archive itself
|
||||
return nil
|
||||
@@ -1009,10 +1009,13 @@ func addZipEntry(wr *zipWriterWrapper, conn *BaseConnection, entryPath, baseDir
|
||||
return util.ErrRecursionTooDeep
|
||||
}
|
||||
recursion++
|
||||
info, err := conn.DoStat(entryPath, 1, false)
|
||||
if err != nil {
|
||||
eventManagerLog(logger.LevelError, "unable to add zip entry %q, stat error: %v", entryPath, err)
|
||||
return err
|
||||
var err error
|
||||
if info == nil {
|
||||
info, err = conn.DoStat(entryPath, 1, false)
|
||||
if err != nil {
|
||||
eventManagerLog(logger.LevelError, "unable to add zip entry %q, stat error: %v", entryPath, err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
entryName, err := getZipEntryName(entryPath, baseDir)
|
||||
if err != nil {
|
||||
@@ -1050,7 +1053,7 @@ func addZipEntry(wr *zipWriterWrapper, conn *BaseConnection, entryPath, baseDir
|
||||
}
|
||||
for _, info := range contents {
|
||||
fullPath := util.CleanPath(path.Join(entryPath, info.Name()))
|
||||
if err := addZipEntry(wr, conn, fullPath, baseDir, recursion); err != nil {
|
||||
if err := addZipEntry(wr, conn, fullPath, baseDir, info, recursion); err != nil {
|
||||
eventManagerLog(logger.LevelError, "unable to add zip entry: %v", err)
|
||||
return err
|
||||
}
|
||||
@@ -2042,7 +2045,7 @@ func executeCompressFsActionForUser(c dataprovider.EventActionFsCompress, replac
|
||||
}
|
||||
startTime := time.Now()
|
||||
for _, item := range paths {
|
||||
if err := addZipEntry(zipWriter, conn, item, baseDir, 0); err != nil {
|
||||
if err := addZipEntry(zipWriter, conn, item, baseDir, nil, 0); err != nil {
|
||||
closeWriterAndUpdateQuota(writer, conn, name, "", numFiles, truncatedSize, err, operationUpload, startTime) //nolint:errcheck
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1823,7 +1823,7 @@ func TestFilesystemActionErrors(t *testing.T) {
|
||||
Writer: zip.NewWriter(bytes.NewBuffer(nil)),
|
||||
Entries: map[string]bool{},
|
||||
}
|
||||
err = addZipEntry(wr, conn, "/adir/sub/f.dat", "/adir/sub/sub", 0)
|
||||
err = addZipEntry(wr, conn, "/adir/sub/f.dat", "/adir/sub/sub", nil, 0)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, getErrorString(err), "is outside base dir")
|
||||
}
|
||||
@@ -1833,7 +1833,7 @@ func TestFilesystemActionErrors(t *testing.T) {
|
||||
Writer: zip.NewWriter(bytes.NewBuffer(nil)),
|
||||
Entries: map[string]bool{},
|
||||
}
|
||||
err = addZipEntry(wr, conn, "/p1", "/", 2000)
|
||||
err = addZipEntry(wr, conn, "/p1", "/", nil, 2000)
|
||||
assert.ErrorIs(t, err, util.ErrRecursionTooDeep)
|
||||
|
||||
err = dataprovider.DeleteUser(username, "", "", "")
|
||||
|
||||
Reference in New Issue
Block a user