github.com/assistd/afero@v1.1.1/ioutil.go (about) 1 // Copyright ©2015 The Go Authors 2 // Copyright ©2015 Steve Francia <spf@spf13.com> 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 package afero 17 18 import ( 19 "bytes" 20 "io" 21 "os" 22 "path/filepath" 23 "sort" 24 "strconv" 25 "sync" 26 "time" 27 ) 28 29 // byName implements sort.Interface. 30 type byName []os.FileInfo 31 32 func (f byName) Len() int { return len(f) } 33 func (f byName) Less(i, j int) bool { return f[i].Name() < f[j].Name() } 34 func (f byName) Swap(i, j int) { f[i], f[j] = f[j], f[i] } 35 36 // ReadDir reads the directory named by dirname and returns 37 // a list of sorted directory entries. 38 func (a Afero) ReadDir(dirname string) ([]os.FileInfo, error) { 39 return ReadDir(a.Fs, dirname) 40 } 41 42 func ReadDir(fs Fs, dirname string) ([]os.FileInfo, error) { 43 f, err := fs.Open(dirname) 44 if err != nil { 45 return nil, err 46 } 47 list, err := f.Readdir(-1) 48 f.Close() 49 if err != nil { 50 return nil, err 51 } 52 sort.Sort(byName(list)) 53 return list, nil 54 } 55 56 // ReadFile reads the file named by filename and returns the contents. 57 // A successful call returns err == nil, not err == EOF. Because ReadFile 58 // reads the whole file, it does not treat an EOF from Read as an error 59 // to be reported. 60 func (a Afero) ReadFile(filename string) ([]byte, error) { 61 return ReadFile(a.Fs, filename) 62 } 63 64 func ReadFile(fs Fs, filename string) ([]byte, error) { 65 f, err := fs.Open(filename) 66 if err != nil { 67 return nil, err 68 } 69 defer f.Close() 70 // It's a good but not certain bet that FileInfo will tell us exactly how much to 71 // read, so let's try it but be prepared for the answer to be wrong. 72 var n int64 73 74 if fi, err := f.Stat(); err == nil { 75 // Don't preallocate a huge buffer, just in case. 76 if size := fi.Size(); size < 1e9 { 77 n = size 78 } 79 } 80 // As initial capacity for readAll, use n + a little extra in case Size is zero, 81 // and to avoid another allocation after Read has filled the buffer. The readAll 82 // call will read into its allocated internal buffer cheaply. If the size was 83 // wrong, we'll either waste some space off the end or reallocate as needed, but 84 // in the overwhelmingly common case we'll get it just right. 85 return readAll(f, n+bytes.MinRead) 86 } 87 88 // readAll reads from r until an error or EOF and returns the data it read 89 // from the internal buffer allocated with a specified capacity. 90 func readAll(r io.Reader, capacity int64) (b []byte, err error) { 91 buf := bytes.NewBuffer(make([]byte, 0, capacity)) 92 // If the buffer overflows, we will get bytes.ErrTooLarge. 93 // Return that as an error. Any other panic remains. 94 defer func() { 95 e := recover() 96 if e == nil { 97 return 98 } 99 if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge { 100 err = panicErr 101 } else { 102 panic(e) 103 } 104 }() 105 _, err = buf.ReadFrom(r) 106 return buf.Bytes(), err 107 } 108 109 // ReadAll reads from r until an error or EOF and returns the data it read. 110 // A successful call returns err == nil, not err == EOF. Because ReadAll is 111 // defined to read from src until EOF, it does not treat an EOF from Read 112 // as an error to be reported. 113 func ReadAll(r io.Reader) ([]byte, error) { 114 return readAll(r, bytes.MinRead) 115 } 116 117 // WriteFile writes data to a file named by filename. 118 // If the file does not exist, WriteFile creates it with permissions perm; 119 // otherwise WriteFile truncates it before writing. 120 func (a Afero) WriteFile(filename string, data []byte, perm os.FileMode) error { 121 return WriteFile(a.Fs, filename, data, perm) 122 } 123 124 func WriteFile(fs Fs, filename string, data []byte, perm os.FileMode) error { 125 f, err := fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) 126 if err != nil { 127 return err 128 } 129 n, err := f.Write(data) 130 if err == nil && n < len(data) { 131 err = io.ErrShortWrite 132 } 133 if err1 := f.Close(); err == nil { 134 err = err1 135 } 136 return err 137 } 138 139 // Random number state. 140 // We generate random temporary file names so that there's a good 141 // chance the file doesn't exist yet - keeps the number of tries in 142 // TempFile to a minimum. 143 var rand uint32 144 var randmu sync.Mutex 145 146 func reseed() uint32 { 147 return uint32(time.Now().UnixNano() + int64(os.Getpid())) 148 } 149 150 func nextSuffix() string { 151 randmu.Lock() 152 r := rand 153 if r == 0 { 154 r = reseed() 155 } 156 r = r*1664525 + 1013904223 // constants from Numerical Recipes 157 rand = r 158 randmu.Unlock() 159 return strconv.Itoa(int(1e9 + r%1e9))[1:] 160 } 161 162 // TempFile creates a new temporary file in the directory dir 163 // with a name beginning with prefix, opens the file for reading 164 // and writing, and returns the resulting *File. 165 // If dir is the empty string, TempFile uses the default directory 166 // for temporary files (see os.TempDir). 167 // Multiple programs calling TempFile simultaneously 168 // will not choose the same file. The caller can use f.Name() 169 // to find the pathname of the file. It is the caller's responsibility 170 // to remove the file when no longer needed. 171 func (a Afero) TempFile(dir, prefix string) (f File, err error) { 172 return TempFile(a.Fs, dir, prefix) 173 } 174 175 func TempFile(fs Fs, dir, prefix string) (f File, err error) { 176 if dir == "" { 177 dir = os.TempDir() 178 } 179 180 nconflict := 0 181 for i := 0; i < 10000; i++ { 182 name := filepath.Join(dir, prefix+nextSuffix()) 183 f, err = fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) 184 if os.IsExist(err) { 185 if nconflict++; nconflict > 10 { 186 randmu.Lock() 187 rand = reseed() 188 randmu.Unlock() 189 } 190 continue 191 } 192 break 193 } 194 return 195 } 196 197 // TempDir creates a new temporary directory in the directory dir 198 // with a name beginning with prefix and returns the path of the 199 // new directory. If dir is the empty string, TempDir uses the 200 // default directory for temporary files (see os.TempDir). 201 // Multiple programs calling TempDir simultaneously 202 // will not choose the same directory. It is the caller's responsibility 203 // to remove the directory when no longer needed. 204 func (a Afero) TempDir(dir, prefix string) (name string, err error) { 205 return TempDir(a.Fs, dir, prefix) 206 } 207 func TempDir(fs Fs, dir, prefix string) (name string, err error) { 208 if dir == "" { 209 dir = os.TempDir() 210 } 211 212 nconflict := 0 213 for i := 0; i < 10000; i++ { 214 try := filepath.Join(dir, prefix+nextSuffix()) 215 err = fs.Mkdir(try, 0700) 216 if os.IsExist(err) { 217 if nconflict++; nconflict > 10 { 218 randmu.Lock() 219 rand = reseed() 220 randmu.Unlock() 221 } 222 continue 223 } 224 if err == nil { 225 name = try 226 } 227 break 228 } 229 return 230 }