files

package
v1.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package files 文件操作

包含了文件状态的快速判断, 大小计算 文件/文件夹的创建 移动 拷贝 删除 内置的Rsync命令调用 一个Serial串流文件流写入类帮助连续写入

Package files

Package files

Package files

Package files

Package files

Package files

Package files

Index

Constants

View Source
const (
	Mask             = 0022
	ReadOnlyMode     = 0444
	WriteOnlyMode    = 0222
	UsrReadOnlyMode  = 0400
	UsrWriteOnlyMode = 0200
	GReadOnlyMode    = 0440
	GWriteOnlyMode   = 0220
	RWMode           = 0666
	UsrRWMode        = 0600
	GRWMode          = 0660
	XMode            = 0777
	UsrXMode         = 0700
	GXMode           = 0770

	FileMode  = 0640
	XFileMode = 0700
	DirMode   = 0750
	TmpMode   = 0660
)

常用的文件权限mode Usr uid G gid

View Source
const (
	KB = 1 << 10
	MB = KB << 10
	GB = MB << 10
	TB = GB << 10
)
View Source
const (
	RsyncCMD = "rsync -q -p -backup %s %s"
	TarCMD   = "tar -zcf %s.tar.gz %s"
)

Variables

View Source
var (
	ErrFileOverDir = errors.New(
		"fsync: trying to overwrite a non-empty directory with a file")
)
View Source
var TmpDir = os.TempDir()

Functions

func AutoName added in v1.0.9

func AutoName(dst string) string

AutoName 自动创建时间戳目录

func CalcSize added in v1.1.2

func CalcSize(s int64) string

CalcSize 计算一个文件大小得可读性值

func ClearFile

func ClearFile(f string) error

ClearFile 清空文件内容

func Copy

func Copy(src, dst string) error

Copy 复制文件到某路径

func CreateFile

func CreateFile(f string, perm os.FileMode, dirPerm os.FileMode) (*os.File, error)

CreateFile 创建文件如果不存在

func GetContent

func GetContent(f string) []byte

GetContent 读取文件内容 错误时返回空

func GetFileMode

func GetFileMode(f string) os.FileMode

GetFileMode 获取文件类型

func GetModTime

func GetModTime(f string) time.Time

GetModTime 获取文件修改时间 文件不存在时返回当前时间

func GetSize

func GetSize(f string) int64

GetSize 获取文件大小

func GetSizeReadable

func GetSizeReadable(f string) string

GetSizeReadable 可读性更好的fileSize

func IsExist

func IsExist(path string) bool

IsExist 文件或文件夹是否存在

func IsFile

func IsFile(path string) bool

IsFile 是否为文件

func IsFolder

func IsFolder(path string) bool

IsFolder 是否为文件夹

func IsZip

func IsZip(f string) bool

func MkDir

func MkDir(p string, perm os.FileMode) error

MkDir 按照子目录创建目录

func Name

func Name(f string) string

Name 获取文件的base名称

func RmDir

func RmDir(p string) error

RmDir 删除目录

func RmFile

func RmFile(f string) error

func Rsync added in v1.0.9

func Rsync(src, dst string) error

同步copy src源目录 dst 目标目录 不存在自动创建

func RsyncAndTar added in v1.0.9

func RsyncAndTar(src, dst string, postClear bool) error

RsyncAndTar 自动备份并压缩 eg: RsyncAndTar("/test", "/tmp/back", false) 会自动压缩目录到/tmp/back-timestamp.tar.gz

func SetFileMode

func SetFileMode(f string, perm os.FileMode) error

SetFileMode chmod修改文件权限

func SetFileOwn

func SetFileOwn(f string, uid int, gid int) error

SetFileOwn chown修改属主 user id group id

func Sync

func Sync(dst, src string) error

Sync copies files and directories inside src into dst.

func SyncTo

func SyncTo(to string, srcs ...string) error

SyncTo syncs srcs files and directories into to directory.

func Tar added in v1.0.9

func Tar(file, src string) error

Tar 会进行目录切换 需要保证目录src存在

func TmpFile

func TmpFile(f string) (*os.File, error)

TmpFile 创建/tmp下的临时文件

func UnZip

func UnZip(f string, target string) error

UnZip 解压文件

func Zip

func Zip(f string, target string) error

Zip 压缩文件或目录到指定路径 例如: Zip(/tmp/1.txt, /tmp/1.zip) target不存在zip后缀时默认添加

Types

type FileInfo

type FileInfo interface {
	Name() string
	IsDir() bool
}

FileInfo contains the shared methods between os.FileInfo and fs.DirEntry.

type Serial added in v1.0.9

type Serial struct {
	// contains filtered or unexported fields
}

func NewSerial added in v1.0.9

func NewSerial(name string) *Serial

NewSerial 新建一个串行的流 无name时 最终会默认增加一个时间戳

func (*Serial) ExportSerial added in v1.0.9

func (s *Serial) ExportSerial(w ...io.Writer) (int, error)

ExportSerial 导出流到指定writer 默认为os.stdout

func (*Serial) GetSerial added in v1.0.9

func (s *Serial) GetSerial() string

GetSerial 获取内容

func (*Serial) ToFileSerial added in v1.0.9

func (s *Serial) ToFileSerial() error

ToFileSerial 存储到文件 默认存储为s.name.txt || timestamp.txt

func (*Serial) WriteSerial added in v1.0.9

func (s *Serial) WriteSerial(str string)

WriteSerial 写入可读性强的string

func (*Serial) WriteSerialRaw added in v1.0.9

func (s *Serial) WriteSerialRaw(b []byte)

WriteSerialRaw 写入bytes

type Syncer

type Syncer struct {
	// Set this to true to delete files in the destination that don't exist
	// in the source.
	Delete bool
	// To allow certain files to remain in the destination, implement this function.
	// Return true to skip file, false to delete.
	// Note that src may be either os.FileInfo or fs.DirEntry depending on the file system.
	DeleteFilter func(f FileInfo) bool
	// By default, modification times are synced. This can be turned off by
	// setting this to true.
	NoTimes bool
	// NoChmod disables permission mode syncing.
	NoChmod bool
	// Implement this function to skip Chmod syncing for only certain files
	// or directories. Return true to skip Chmod.
	ChmodFilter func(dst, src os.FileInfo) bool

	SrcFs  afero.Fs
	DestFs afero.Fs

	// exclude
	Exclude     []string
	ExcludeFunc func(f FileInfo) bool
}

Type Syncer provides functions for syncing files.

func NewSyncer

func NewSyncer() *Syncer

NewSyncer creates a new instance of Syncer with default options.

func (*Syncer) Sync

func (s *Syncer) Sync(dst, src string) error

Sync copies files and directories inside src into dst.

func (*Syncer) SyncTo

func (s *Syncer) SyncTo(to string, srcs ...string) error

SyncTo syncs srcs files or directories into to directory.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL