github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/config-common.go (about) 1 // Copyright (c) 2015-2021 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "bytes" 22 "context" 23 "errors" 24 "io" 25 "net/http" 26 27 "github.com/minio/minio/internal/hash" 28 ) 29 30 var errConfigNotFound = errors.New("config file not found") 31 32 func readConfigWithMetadata(ctx context.Context, store objectIO, configFile string, opts ObjectOptions) ([]byte, ObjectInfo, error) { 33 r, err := store.GetObjectNInfo(ctx, minioMetaBucket, configFile, nil, http.Header{}, opts) 34 if err != nil { 35 if isErrObjectNotFound(err) { 36 return nil, ObjectInfo{}, errConfigNotFound 37 } 38 39 return nil, ObjectInfo{}, err 40 } 41 defer r.Close() 42 43 buf, err := io.ReadAll(r) 44 if err != nil { 45 return nil, ObjectInfo{}, err 46 } 47 if len(buf) == 0 { 48 return nil, ObjectInfo{}, errConfigNotFound 49 } 50 return buf, r.ObjInfo, nil 51 } 52 53 func readConfig(ctx context.Context, store objectIO, configFile string) ([]byte, error) { 54 buf, _, err := readConfigWithMetadata(ctx, store, configFile, ObjectOptions{}) 55 return buf, err 56 } 57 58 type objectDeleter interface { 59 DeleteObject(ctx context.Context, bucket, object string, opts ObjectOptions) (ObjectInfo, error) 60 } 61 62 func deleteConfig(ctx context.Context, objAPI objectDeleter, configFile string) error { 63 _, err := objAPI.DeleteObject(ctx, minioMetaBucket, configFile, ObjectOptions{ 64 DeletePrefix: true, 65 DeletePrefixObject: true, // use prefix delete on exact object (this is an optimization to avoid fan-out calls) 66 }) 67 if err != nil && isErrObjectNotFound(err) { 68 return errConfigNotFound 69 } 70 return err 71 } 72 73 func saveConfigWithOpts(ctx context.Context, store objectIO, configFile string, data []byte, opts ObjectOptions) error { 74 hashReader, err := hash.NewReader(ctx, bytes.NewReader(data), int64(len(data)), "", getSHA256Hash(data), int64(len(data))) 75 if err != nil { 76 return err 77 } 78 79 _, err = store.PutObject(ctx, minioMetaBucket, configFile, NewPutObjReader(hashReader), opts) 80 return err 81 } 82 83 func saveConfig(ctx context.Context, store objectIO, configFile string, data []byte) error { 84 return saveConfigWithOpts(ctx, store, configFile, data, ObjectOptions{MaxParity: true}) 85 } 86 87 func checkConfig(ctx context.Context, objAPI ObjectLayer, configFile string) error { 88 if _, err := objAPI.GetObjectInfo(ctx, minioMetaBucket, configFile, ObjectOptions{}); err != nil { 89 // Treat object not found as config not found. 90 if isErrObjectNotFound(err) { 91 return errConfigNotFound 92 } 93 94 return err 95 } 96 return nil 97 }