lib/model: Don't fail on temporary chmod (fixes #8355, ref #8235) (#8356)

This commit is contained in:
Simon Frei 2022-05-22 13:52:40 +02:00 committed by GitHub
parent 5495d0f8ab
commit e3078cc531
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -116,13 +116,15 @@ func inWritableDir(fn func(string) error, targetFs fs.Filesystem, path string, i
}
const permBits = fs.ModePerm | fs.ModeSetuid | fs.ModeSetgid | fs.ModeSticky
var parentErr error
if mode := info.Mode() & permBits; mode&0200 == 0 {
// A non-writeable directory (for this user; we assume that's the
// relevant part). Temporarily change the mode so we can delete the
// file or directory inside it.
if err := targetFs.Chmod(dir, mode|0700); err != nil {
return err
}
parentErr = targetFs.Chmod(dir, mode|0700)
if parentErr != nil {
l.Debugf("Failed to make parent directory writable: %v", parentErr)
} else {
// Chmod succeeded, we should change the permissions back on the way
// out. If we fail we log the error as we have irrevocably messed up
// at this point. :( (The operation we were called to wrap has
@ -138,6 +140,11 @@ func inWritableDir(fn func(string) error, targetFs fs.Filesystem, path string, i
}
}()
}
return fn(path)
}
err = fn(path)
if fs.IsPermission(err) && parentErr != nil {
err = fmt.Errorf("error after failing to make parent directory writable: %w", err)
}
return err
}