mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
improve copy feature for package gfile
This commit is contained in:
parent
09e83e7b8d
commit
acc2a6a353
@ -2,9 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := []int{}
|
||||
fmt.Println(a[0])
|
||||
s := `/Users/john/Workspace/Go/GOPATH/pkg/mod/github.com/nats-io/nats-server/v2@v2.1.4`
|
||||
d := `/Users/john/Workspace/Go/GOPATH/src/github.com/nats-io/nats-server/v2`
|
||||
fmt.Println(gfile.Copy(s, d))
|
||||
}
|
||||
|
@ -25,8 +25,11 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// Default perm for file opening.
|
||||
DefaultPerm = os.FileMode(0666)
|
||||
// DefaultPerm is the default perm for file opening.
|
||||
DefaultPermOpen = os.FileMode(0666)
|
||||
|
||||
// DefaultPermCopy is the default perm for file/folder copy.
|
||||
DefaultPermCopy = os.FileMode(0777)
|
||||
|
||||
// The absolute file path for main package.
|
||||
// It can be only checked and set once.
|
||||
@ -92,7 +95,7 @@ func OpenFile(path string, flag int, perm os.FileMode) (*os.File, error) {
|
||||
// The default <perm> is 0666.
|
||||
// The parameter <flag> is like: O_RDONLY, O_RDWR, O_RDWR|O_CREATE|O_TRUNC, etc.
|
||||
func OpenWithFlag(path string, flag int) (*os.File, error) {
|
||||
f, err := os.OpenFile(path, flag, DefaultPerm)
|
||||
f, err := os.OpenFile(path, flag, DefaultPermOpen)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -235,7 +238,7 @@ func Remove(path string) error {
|
||||
// IsReadable checks whether given <path> is readable.
|
||||
func IsReadable(path string) bool {
|
||||
result := true
|
||||
file, err := os.OpenFile(path, os.O_RDONLY, DefaultPerm)
|
||||
file, err := os.OpenFile(path, os.O_RDONLY, DefaultPermOpen)
|
||||
if err != nil {
|
||||
result = false
|
||||
}
|
||||
@ -259,7 +262,7 @@ func IsWritable(path string) bool {
|
||||
}
|
||||
} else {
|
||||
// 如果是文件,那么判断文件是否可打开
|
||||
file, err := os.OpenFile(path, os.O_WRONLY, DefaultPerm)
|
||||
file, err := os.OpenFile(path, os.O_WRONLY, DefaultPermOpen)
|
||||
if err != nil {
|
||||
result = false
|
||||
}
|
||||
|
@ -67,25 +67,25 @@ func Truncate(path string, size int) error {
|
||||
// PutContents puts string <content> to file of <path>.
|
||||
// It creates file of <path> recursively if it does not exist.
|
||||
func PutContents(path string, content string) error {
|
||||
return putContents(path, []byte(content), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, DefaultPerm)
|
||||
return putContents(path, []byte(content), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, DefaultPermOpen)
|
||||
}
|
||||
|
||||
// PutContentsAppend appends string <content> to file of <path>.
|
||||
// It creates file of <path> recursively if it does not exist.
|
||||
func PutContentsAppend(path string, content string) error {
|
||||
return putContents(path, []byte(content), os.O_WRONLY|os.O_CREATE|os.O_APPEND, DefaultPerm)
|
||||
return putContents(path, []byte(content), os.O_WRONLY|os.O_CREATE|os.O_APPEND, DefaultPermOpen)
|
||||
}
|
||||
|
||||
// PutBytes puts binary <content> to file of <path>.
|
||||
// It creates file of <path> recursively if it does not exist.
|
||||
func PutBytes(path string, content []byte) error {
|
||||
return putContents(path, content, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, DefaultPerm)
|
||||
return putContents(path, content, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, DefaultPermOpen)
|
||||
}
|
||||
|
||||
// PutBytesAppend appends binary <content> to file of <path>.
|
||||
// It creates file of <path> recursively if it does not exist.
|
||||
func PutBytesAppend(path string, content []byte) error {
|
||||
return putContents(path, content, os.O_WRONLY|os.O_CREATE|os.O_APPEND, DefaultPerm)
|
||||
return putContents(path, content, os.O_WRONLY|os.O_CREATE|os.O_APPEND, DefaultPermOpen)
|
||||
}
|
||||
|
||||
// GetNextCharOffset returns the file offset for given <char> starting from <start>.
|
||||
@ -110,7 +110,7 @@ func GetNextCharOffset(reader io.ReaderAt, char byte, start int64) int64 {
|
||||
// GetNextCharOffsetByPath returns the file offset for given <char> starting from <start>.
|
||||
// It opens file of <path> for reading with os.O_RDONLY flag and default perm.
|
||||
func GetNextCharOffsetByPath(path string, char byte, start int64) int64 {
|
||||
if f, err := OpenWithFlagPerm(path, os.O_RDONLY, DefaultPerm); err == nil {
|
||||
if f, err := OpenWithFlagPerm(path, os.O_RDONLY, DefaultPermOpen); err == nil {
|
||||
defer f.Close()
|
||||
return GetNextCharOffset(f, char, start)
|
||||
}
|
||||
@ -134,7 +134,7 @@ func GetBytesTilChar(reader io.ReaderAt, char byte, start int64) ([]byte, int64)
|
||||
//
|
||||
// Note: Returned value contains the character of the last position.
|
||||
func GetBytesTilCharByPath(path string, char byte, start int64) ([]byte, int64) {
|
||||
if f, err := OpenWithFlagPerm(path, os.O_RDONLY, DefaultPerm); err == nil {
|
||||
if f, err := OpenWithFlagPerm(path, os.O_RDONLY, DefaultPermOpen); err == nil {
|
||||
defer f.Close()
|
||||
return GetBytesTilChar(f, char, start)
|
||||
}
|
||||
@ -157,7 +157,7 @@ func GetBytesByTwoOffsets(reader io.ReaderAt, start int64, end int64) []byte {
|
||||
// it returns content range as [start, end).
|
||||
// It opens file of <path> for reading with os.O_RDONLY flag and default perm.
|
||||
func GetBytesByTwoOffsetsByPath(path string, start int64, end int64) []byte {
|
||||
if f, err := OpenWithFlagPerm(path, os.O_RDONLY, DefaultPerm); err == nil {
|
||||
if f, err := OpenWithFlagPerm(path, os.O_RDONLY, DefaultPermOpen); err == nil {
|
||||
defer f.Close()
|
||||
return GetBytesByTwoOffsets(f, start, end)
|
||||
}
|
||||
|
@ -71,11 +71,7 @@ func CopyFile(src, dst string) (err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
si, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = os.Chmod(dst, si.Mode())
|
||||
err = os.Chmod(dst, DefaultPermCopy)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -102,7 +98,7 @@ func CopyDir(src string, dst string) (err error) {
|
||||
return fmt.Errorf("source is not a directory")
|
||||
}
|
||||
if !Exists(dst) {
|
||||
err = os.MkdirAll(dst, si.Mode())
|
||||
err = os.MkdirAll(dst, DefaultPermCopy)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user