github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/ioutil/os_wrap.go (about) 1 // Copyright 2016 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package ioutil 6 7 import ( 8 "os" 9 10 "github.com/pkg/errors" 11 ) 12 13 // OpenFile wraps OpenFile from "os". 14 func OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) { 15 f, err := os.OpenFile(name, flag, perm) 16 if err != nil { 17 return nil, errors.Wrapf(err, "failed to open %q", name) 18 } 19 20 return f, nil 21 } 22 23 // Lstat wraps Lstat from "os". 24 func Lstat(name string) (os.FileInfo, error) { 25 info, err := os.Lstat(name) 26 if err != nil { 27 return nil, errors.Wrapf(err, "failed to lstat %q", name) 28 } 29 30 return info, nil 31 } 32 33 // Mkdir wraps MkdirAll from "os". 34 func Mkdir(path string, perm os.FileMode) error { 35 err := os.MkdirAll(path, perm) 36 if err != nil { 37 return errors.Wrapf(err, "failed to mkdir %q", path) 38 } 39 40 return nil 41 } 42 43 // MkdirAll wraps MkdirAll from "os". 44 func MkdirAll(path string, perm os.FileMode) error { 45 twoAttempts := false 46 err := os.MkdirAll(path, perm) 47 // KBFS-3245: Simple workaround for test flake where a directory 48 // seems to disappear out from under us. 49 if os.IsNotExist(err) { 50 twoAttempts = true 51 err = os.MkdirAll(path, perm) 52 } 53 if err != nil { 54 return errors.Wrapf(err, 55 "failed to mkdir (all) %q, twoAttempts=%t", path, twoAttempts) 56 } 57 58 return nil 59 } 60 61 // Stat wraps Stat from "os". 62 func Stat(name string) (os.FileInfo, error) { 63 info, err := os.Stat(name) 64 if err != nil { 65 return nil, errors.Wrapf(err, "failed to stat %q", name) 66 } 67 68 return info, nil 69 } 70 71 // Remove wraps Remove from "os". 72 func Remove(name string) error { 73 err := os.Remove(name) 74 if err != nil { 75 return errors.Wrapf(err, "failed to remove %q", name) 76 } 77 78 return nil 79 } 80 81 // RemoveAll wraps RemoveAll from "os". 82 func RemoveAll(name string) error { 83 err := os.RemoveAll(name) 84 if err != nil { 85 return errors.Wrapf(err, "failed to remove (all) %q", name) 86 } 87 88 return nil 89 } 90 91 // Rename wraps Rename from "os". 92 func Rename(oldpath, newpath string) error { 93 err := os.Rename(oldpath, newpath) 94 if err != nil { 95 return errors.Wrapf( 96 err, "failed to rename %q to %q", oldpath, newpath) 97 } 98 99 return nil 100 }