123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package file
- import (
- "errors"
- "io/ioutil"
- "os"
- "path"
- "path/filepath"
- )
- func Basename(fPath string) string {
- return filepath.Base(fPath)
- }
- func Dirname(fPath string) string {
- return filepath.Dir(fPath)
- }
- func Filesize(fPath string) int64 {
- fInfo, err := os.Stat(fPath)
- if err != nil && os.IsNotExist(err) {
- return 0
- }
- fm := fInfo.Mode()
- if fm.IsDir() {
- return 0
- }
- return fInfo.Size()
- }
- func PathInfo(fPath string) (map[string]interface{}, error) {
- fInfoMap := map[string]interface{}{}
- fInfo, err := os.Stat(fPath)
- if err != nil && os.IsNotExist(err) {
- return fInfoMap, err
- }
- fm := fInfo.Mode()
- if fm.IsDir() {
- return fInfoMap, errors.New("fPath is dirname")
- }
- fInfoMap["dirname"] = filepath.Dir(fPath)
- fInfoMap["filename"] = fInfo.Name()
- fInfoMap["size"] = fInfo.Size()
- fInfoMap["extension"] = path.Ext(fPath)
-
- return fInfoMap, nil
- }
- func FileExists(fName string) bool {
- _, err := os.Stat(fName)
- if err != nil && os.IsNotExist(err) {
- return false
- }
- return true
- }
- func IsDir(fName string) bool {
- fInfo, err := os.Stat(fName)
- if err != nil {
- return false
- }
- fm := fInfo.Mode()
- return fm.IsDir()
- }
- func IsFile(fName string) bool {
- fInfo, err := os.Stat(fName)
- if err != nil && os.IsNotExist(err) {
- return false
- }
- fm := fInfo.Mode()
- return !fm.IsDir()
- }
- func FileGetContents(fName string) (string, error) {
- data, err := ioutil.ReadFile(fName)
- return string(data), err
- }
- func FilePutContents(filename string, data string, mode os.FileMode) error {
- return ioutil.WriteFile(filename, []byte(data), mode)
- }
- func Chmod(filename string, mode os.FileMode) bool {
- return os.Chmod(filename, mode) == nil
- }
- func Chown(filename string, uid, gid int) bool {
- return os.Chown(filename, uid, gid) == nil
- }
|