mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-13 09:18:15 +08:00
fix(context): only chmod newly created dirs in SaveUploadedFile
Fixes #4622 - SaveUploadedFile no longer attempts to modify permissions on existing directories, which breaks saving to /tmp and other system directories. - Check if directory exists before calling os.MkdirAll - Only call os.Chmod if directory was newly created - Preserves backward compatibility for newly created dirs - Allows saving files to existing system directories This regression was introduced in v1.12.0 when chmod support was added, but was incorrectly applied to all directories. BREAKING CHANGE REVERTED: Restores v1.10.1 behavior for existing dirs Co-authored-by: Nghia Nguyen <nghiack7@users.noreply.github.com>
This commit is contained in:
parent
5f4f964325
commit
c92f4bf410
10
context.go
10
context.go
@ -728,11 +728,17 @@ func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string, perm
|
|||||||
mode = perm[0]
|
mode = perm[0]
|
||||||
}
|
}
|
||||||
dir := filepath.Dir(dst)
|
dir := filepath.Dir(dst)
|
||||||
|
// Check if directory exists to only chmod newly created directories
|
||||||
|
_, statErr := os.Stat(dir)
|
||||||
|
dirExists := !os.IsNotExist(statErr)
|
||||||
if err = os.MkdirAll(dir, mode); err != nil {
|
if err = os.MkdirAll(dir, mode); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = os.Chmod(dir, mode); err != nil {
|
// Only chmod if directory was newly created (fixes #4622)
|
||||||
return err
|
if !dirExists {
|
||||||
|
if err = os.Chmod(dir, mode); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := os.Create(dst)
|
out, err := os.Create(dst)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user