github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/ioutil/read_file.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 ioutil 19 20 import ( 21 "io" 22 "io/fs" 23 "os" 24 25 "github.com/minio/minio/internal/disk" 26 ) 27 28 var ( 29 // OpenFileDirectIO allows overriding default function. 30 OpenFileDirectIO = disk.OpenFileDirectIO 31 // OsOpen allows overriding default function. 32 OsOpen = os.Open 33 // OsOpenFile allows overriding default function. 34 OsOpenFile = os.OpenFile 35 ) 36 37 // ReadFileWithFileInfo reads the named file and returns the contents. 38 // A successful call returns err == nil, not err == EOF. 39 // Because ReadFile reads the whole file, it does not treat an EOF from Read 40 // as an error to be reported. 41 func ReadFileWithFileInfo(name string) ([]byte, fs.FileInfo, error) { 42 f, err := OsOpenFile(name, readMode, 0o666) 43 if err != nil { 44 return nil, nil, err 45 } 46 defer f.Close() 47 48 st, err := f.Stat() 49 if err != nil { 50 return nil, nil, err 51 } 52 53 dst := make([]byte, st.Size()) 54 _, err = io.ReadFull(f, dst) 55 return dst, st, err 56 } 57 58 // ReadFile reads the named file and returns the contents. 59 // A successful call returns err == nil, not err == EOF. 60 // Because ReadFile reads the whole file, it does not treat an EOF from Read 61 // as an error to be reported. 62 // 63 // passes NOATIME flag for reads on Unix systems to avoid atime updates. 64 func ReadFile(name string) ([]byte, error) { 65 // Don't wrap with un-needed buffer. 66 // Don't use os.ReadFile, since it doesn't pass NO_ATIME when present. 67 f, err := OsOpenFile(name, readMode, 0o666) 68 if err != nil { 69 return nil, err 70 } 71 defer f.Close() 72 st, err := f.Stat() 73 if err != nil { 74 return io.ReadAll(f) 75 } 76 dst := make([]byte, st.Size()) 77 _, err = io.ReadFull(f, dst) 78 return dst, err 79 }