storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/config-common.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cmd 18 19 import ( 20 "bytes" 21 "context" 22 "errors" 23 "io/ioutil" 24 "net/http" 25 26 "storj.io/minio/pkg/hash" 27 ) 28 29 var errConfigNotFound = errors.New("config file not found") 30 31 func readConfig(ctx context.Context, objAPI ObjectLayer, configFile string) ([]byte, error) { 32 // Read entire content by setting size to -1 33 r, err := objAPI.GetObjectNInfo(ctx, minioMetaBucket, configFile, nil, http.Header{}, readLock, ObjectOptions{}) 34 if err != nil { 35 // Treat object not found as config not found. 36 if isErrObjectNotFound(err) { 37 return nil, errConfigNotFound 38 } 39 40 return nil, err 41 } 42 defer r.Close() 43 44 buf, err := ioutil.ReadAll(r) 45 if err != nil { 46 return nil, err 47 } 48 if len(buf) == 0 { 49 return nil, errConfigNotFound 50 } 51 return buf, nil 52 } 53 54 type objectDeleter interface { 55 DeleteObject(ctx context.Context, bucket, object string, opts ObjectOptions) (ObjectInfo, error) 56 } 57 58 func deleteConfig(ctx context.Context, objAPI objectDeleter, configFile string) error { 59 _, err := objAPI.DeleteObject(ctx, minioMetaBucket, configFile, ObjectOptions{}) 60 if err != nil && isErrObjectNotFound(err) { 61 return errConfigNotFound 62 } 63 return err 64 } 65 66 func saveConfig(ctx context.Context, objAPI ObjectLayer, configFile string, data []byte) error { 67 hashReader, err := hash.NewReader(bytes.NewReader(data), int64(len(data)), "", getSHA256Hash(data), int64(len(data))) 68 if err != nil { 69 return err 70 } 71 72 _, err = objAPI.PutObject(ctx, minioMetaBucket, configFile, NewPutObjReader(hashReader), ObjectOptions{MaxParity: true}) 73 return err 74 } 75 76 func checkConfig(ctx context.Context, objAPI ObjectLayer, configFile string) error { 77 if _, err := objAPI.GetObjectInfo(ctx, minioMetaBucket, configFile, ObjectOptions{}); err != nil { 78 // Treat object not found as config not found. 79 if isErrObjectNotFound(err) { 80 return errConfigNotFound 81 } 82 83 return err 84 } 85 return nil 86 }