bitbucket.org/ai69/amoy@v0.2.3/file.go (about) 1 package amoy 2 3 import ( 4 "bufio" 5 "io/ioutil" 6 "log" 7 "os" 8 9 gu "golang.org/x/tools/godoc/util" 10 ) 11 12 // IsTextFile loads and checks if the given path is a text file. 13 func IsTextFile(path string) bool { 14 buf, err := ioutil.ReadFile(path) 15 if err != nil { 16 log.Fatalf("fail to read file: %v", err) 17 } 18 return gu.IsText(buf) 19 } 20 21 // TrimUTF8BOM removes the leading UTF-8 byte order mark from bytes. 22 func TrimUTF8BOM(b []byte) []byte { 23 if len(b) >= 3 && b[0] == 0xef && b[1] == 0xbb && b[2] == 0xbf { 24 return b[3:] 25 } 26 return b 27 } 28 29 var ( 30 filePerm os.FileMode = 0644 31 createFileFlag = os.O_RDWR | os.O_CREATE | os.O_TRUNC 32 appendFileFlag = os.O_APPEND | os.O_CREATE | os.O_WRONLY 33 ) 34 35 // ReadFileBytes reads the whole named file and returns the contents. 36 // It's a sugar actually, simply calls os.ReadFile like ioutil.ReadFile does since Go 1.16. 37 func ReadFileBytes(path string) ([]byte, error) { 38 return os.ReadFile(path) 39 } 40 41 // ReadFileString reads the whole named file and returns the contents as a string. 42 func ReadFileString(path string) (string, error) { 43 b, err := os.ReadFile(path) 44 if err != nil { 45 return EmptyStr, err 46 } 47 return string(b), nil 48 } 49 50 // WriteFileBytes writes the given data into a file. 51 func WriteFileBytes(path string, data []byte) error { 52 return openFileWriteBytes(path, createFileFlag, data) 53 } 54 55 // AppendFileBytes writes the given data to the end of a file. 56 func AppendFileBytes(path string, data []byte) error { 57 return openFileWriteBytes(path, appendFileFlag, data) 58 } 59 60 func openFileWriteBytes(path string, flag int, data []byte) error { 61 file, err := os.OpenFile(path, flag, filePerm) 62 if err != nil { 63 return err 64 } 65 defer file.Close() 66 67 w := bufio.NewWriter(file) 68 defer w.Flush() 69 _, err = w.Write(data) 70 return err 71 } 72 73 // WriteFileString writes the given content string into a file. 74 func WriteFileString(path string, content string) error { 75 return openFileWriteString(path, createFileFlag, content) 76 } 77 78 // AppendFileString appends the given content string to the end of a file. 79 func AppendFileString(path string, content string) error { 80 return openFileWriteString(path, appendFileFlag, content) 81 } 82 83 func openFileWriteString(path string, flag int, content string) error { 84 file, err := os.OpenFile(path, flag, filePerm) 85 if err != nil { 86 return err 87 } 88 defer file.Close() 89 90 w := bufio.NewWriter(file) 91 defer w.Flush() 92 _, err = w.WriteString(content) 93 return err 94 } 95 96 // IsSamePath returns true if two paths describe the same file. 97 func IsSamePath(p1, p2 string) (bool, error) { 98 // compare string directly 99 if p1 == p2 { 100 return true, nil 101 } 102 // get file stats 103 fi1, err := os.Stat(p1) 104 if err != nil { 105 return false, err 106 } 107 fi2, err := os.Stat(p2) 108 if err != nil { 109 return false, err 110 } 111 // compare with file stats 112 return os.SameFile(fi1, fi2), nil 113 }