fix: best effort save permissions always (#3050)

This commit is contained in:
Harshavardhana
2020-01-17 14:02:12 -08:00
committed by kannappanr
parent 0596acea95
commit 28aae43c8c
4 changed files with 68 additions and 71 deletions
+43 -40
View File
@@ -233,6 +233,35 @@ func isStreamFile(objectPath string) bool {
return false
}
func preserveAttributes(fd *os.File, attr map[string]string) *probe.Error {
mode, e := strconv.ParseUint(attr["mode"], 10, 32)
if e != nil {
return probe.NewError(e)
}
// Attempt to change the file mode.
if e := fd.Chmod(os.FileMode(mode)); e != nil {
return probe.NewError(e)
}
uid, e := strconv.Atoi(attr["uid"])
if e != nil {
return probe.NewError(e)
}
gid, e := strconv.Atoi(attr["gid"])
if e != nil {
return probe.NewError(e)
}
// Attempt to change the owner.
if e := fd.Chown(uid, gid); e != nil {
return probe.NewError(e)
}
return nil
}
/// Object operations.
func (f *fsClient) put(reader io.Reader, size int64, metadata map[string][]string, progress io.Reader) (int64, *probe.Error) {
@@ -270,6 +299,17 @@ func (f *fsClient) put(reader io.Reader, size int64, metadata map[string][]strin
return 0, err.Trace(f.PathURL.Path)
}
attr := make(map[string]string)
if len(metadata["mc-attrs"]) != 0 {
attr, e = parseAttribute(metadata["mc-attrs"][0])
if e != nil {
return 0, probe.NewError(e)
}
if err := preserveAttributes(partFile, attr); err != nil {
return 0, err.Trace(objectPath)
}
}
// Get stat to get the current size.
partSt, e := os.Stat(objectPartPath)
if e != nil {
@@ -345,41 +385,7 @@ func (f *fsClient) put(reader io.Reader, size int64, metadata map[string][]strin
return totalWritten, err.Trace(objectPartPath, objectPath)
}
if len(metadata["mc-attrs"]) != 0 {
attr, e := parseAttribute(metadata["mc-attrs"][0])
if e != nil {
return totalWritten, probe.NewError(e)
}
mode, e := strconv.ParseUint(attr["mode"], 10, 32)
if e != nil {
return totalWritten, probe.NewError(e)
}
// Change the mode of file
if e := os.Chmod(objectPath, os.FileMode(mode)); e != nil {
if !os.IsPermission(e) {
return totalWritten, probe.NewError(e)
}
}
uid, e := strconv.Atoi(attr["uid"])
if e != nil {
return totalWritten, probe.NewError(e)
}
gid, e := strconv.Atoi(attr["gid"])
if e != nil {
return totalWritten, probe.NewError(e)
}
// Change the owner
if e := os.Chown(objectPath, uid, gid); e != nil {
if !os.IsPermission(e) {
return totalWritten, probe.NewError(e)
}
}
if len(attr) != 0 {
atime, e := strconv.ParseInt(attr["atime"], 10, 64)
if e != nil {
return totalWritten, probe.NewError(e)
@@ -390,13 +396,10 @@ func (f *fsClient) put(reader io.Reader, size int64, metadata map[string][]strin
return totalWritten, probe.NewError(e)
}
// Change the access, modify and change time
// Attempt to change the access, modify and change time
if e := os.Chtimes(objectPath, time.Unix(atime, 0), time.Unix(ctime, 0)); e != nil {
if !os.IsPermission(e) {
return totalWritten, probe.NewError(e)
}
return totalWritten, probe.NewError(e)
}
}
}
return totalWritten, nil
+9 -11
View File
@@ -1,7 +1,7 @@
// +build darwin
/*
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
* MinIO Cloud Storage, (C) 2019-2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,7 +29,6 @@ import (
// GetFileSystemAttrs return the file system attribute as string; containing mode,
// uid, gid, uname, Gname, atime, mtime, ctime and md5
func GetFileSystemAttrs(file string) (string, error) {
st := syscall.Stat_t{}
err := syscall.Stat(file, &st)
if err != nil {
@@ -44,12 +43,11 @@ func GetFileSystemAttrs(file string) (string, error) {
fileAttr.WriteString("/gid:")
fileAttr.WriteString(strconv.Itoa(int(st.Gid)))
fileAttr.WriteString("/gname:")
g, err := user.LookupGroupId(strconv.FormatUint(uint64(st.Gid), 10))
if err != nil {
return "", err
if err == nil {
fileAttr.WriteString("/gname:")
fileAttr.WriteString(g.Name)
}
fileAttr.WriteString(g.Name)
fileAttr.WriteString("/mode:")
fileAttr.WriteString(strconv.Itoa(int(st.Mode)))
@@ -58,12 +56,12 @@ func GetFileSystemAttrs(file string) (string, error) {
fileAttr.WriteString("/uid:")
fileAttr.WriteString(strconv.Itoa(int(st.Uid)))
fileAttr.WriteString("/uname:")
i, err := user.LookupId(strconv.FormatUint(uint64(st.Uid), 10))
if err != nil {
return "", err
u, err := user.LookupId(strconv.FormatUint(uint64(st.Uid), 10))
if err == nil {
fileAttr.WriteString("/uname:")
fileAttr.WriteString(u.Username)
}
fileAttr.WriteString(i.Username)
return fileAttr.String(), nil
}
+8 -10
View File
@@ -1,7 +1,7 @@
// +build linux
/*
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
* MinIO Cloud Storage, (C) 2019-2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,12 +44,11 @@ func GetFileSystemAttrs(file string) (string, error) {
fileAttr.WriteString("/gid:")
fileAttr.WriteString(strconv.Itoa(int(st.Gid)))
fileAttr.WriteString("/gname:")
g, err := user.LookupGroupId(strconv.FormatUint(uint64(st.Gid), 10))
if err != nil {
return "", err
if err == nil {
fileAttr.WriteString("/gname:")
fileAttr.WriteString(g.Name)
}
fileAttr.WriteString(g.Name)
fileAttr.WriteString("/mode:")
fileAttr.WriteString(strconv.Itoa(int(st.Mode)))
@@ -58,12 +57,11 @@ func GetFileSystemAttrs(file string) (string, error) {
fileAttr.WriteString("/uid:")
fileAttr.WriteString(strconv.Itoa(int(st.Uid)))
fileAttr.WriteString("/uname:")
i, err := user.LookupId(strconv.FormatUint(uint64(st.Uid), 10))
if err != nil {
return "", err
u, err := user.LookupId(strconv.FormatUint(uint64(st.Uid), 10))
if err == nil {
fileAttr.WriteString("/uname:")
fileAttr.WriteString(u.Username)
}
fileAttr.WriteString(i.Username)
return fileAttr.String(), nil
}
+8 -10
View File
@@ -1,7 +1,7 @@
// +build openbsd
/*
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
* MinIO Cloud Storage, (C) 2019-2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,12 +43,11 @@ func GetFileSystemAttrs(file string) (string, error) {
fileAttr.WriteString("/gid:")
fileAttr.WriteString(strconv.Itoa(int(st.Gid)))
fileAttr.WriteString("/gname:")
g, err := user.LookupGroupId(strconv.FormatUint(uint64(st.Gid), 10))
if err != nil {
return "", err
if err == nil {
fileAttr.WriteString("/gname:")
fileAttr.WriteString(g.Name)
}
fileAttr.WriteString(g.Name)
fileAttr.WriteString("/mode:")
fileAttr.WriteString(strconv.Itoa(int(st.Mode)))
@@ -57,12 +56,11 @@ func GetFileSystemAttrs(file string) (string, error) {
fileAttr.WriteString("/uid:")
fileAttr.WriteString(strconv.Itoa(int(st.Uid)))
fileAttr.WriteString("/uname:")
i, err := user.LookupId(strconv.FormatUint(uint64(st.Uid), 10))
if err != nil {
return "", err
u, err := user.LookupId(strconv.FormatUint(uint64(st.Uid), 10))
if err == nil {
fileAttr.WriteString("/uname:")
fileAttr.WriteString(u.Username)
}
fileAttr.WriteString(i.Username)
return fileAttr.String(), nil
}