Documentation
¶
Index ¶
- Constants
- Variables
- func Append(dst string, src string) error
- func AppendAll(filename string, data []byte) error
- func AppendAllFrom(filename string, r io.Reader) error
- func AppendAllIfNotExist(path string) (*os.File, error)
- func AppendFileAll(filename string, data []byte, dirperm, fileperm os.FileMode) error
- func AppendFileAllFrom(filename string, r io.Reader, dirperm, fileperm os.FileMode) error
- func AppendTruncate(dst string, src string) error
- func AppendTruncateAll(dst string, src string) error
- func ChtimesNow(name string) error
- func Copy(dst string, src string) error
- func CopyAll(dst string, src string) error
- func CopyAppendAll(dst string, src string) error
- func CopyFile(dst string, src string, flag int, perm os.FileMode) error
- func CopyFileAll(dst string, src string, flag int, dirperm, fileperm os.FileMode) error
- func CopyRename(dst string, src string) error
- func CopyRenameAll(dst string, src string) error
- func CopyRenameFile(dst string, src string, flag int, perm os.FileMode) error
- func CopyRenameFileAll(dst string, src string, flag int, dirperm, fileperm os.FileMode) error
- func CopyTruncate(dst string, src string) error
- func CopyTruncateAll(dst string, src string) error
- func CopyTruncateFile(dst string, src string, flag int, perm os.FileMode, size int64) error
- func CopyTruncateFileAll(dst string, src string, flag int, dirperm, fileperm os.FileMode, size int64) error
- func CreateAll(path string) (*os.File, error)
- func CreateAllIfNotExist(path string) (*os.File, error)
- func DiskUsage(path string) (total int64, free int64, avail int64, inodes int64, inodesFree int64, ...)
- func GetAbsBinDir() (dir string, err error)
- func LockAll(path string) (*os.File, error)
- func MD5CacheKey(s string) string
- func Make(name string) error
- func MakeAll(name string) error
- func MaxSeq(pattern string) (prefix string, seq int, suffix string)
- func MaxSeqFunc(pattern string, handler func(name string) bool) (prefix string, seq int, suffix string)
- func MkdirTempAll(dir, pattern string) (string, error)
- func MkdirTempDirAll(dir, pattern string, dirperm os.FileMode) (string, error)
- func NextFile(pattern string, seq int) (f *os.File, seqUsed int, err error)
- func OpenAll(path string) (*os.File, error)
- func OpenFileAll(path string, flag int, dirperm, fileperm os.FileMode) (*os.File, error)
- func PathExists(path string) (bool, error)
- func ReLink(oldname, newname string) error
- func ReSymlink(oldname, newname string) error
- func ReadDirN(name string, n int) ([]os.DirEntry, error)
- func RemoveIfExist(name string) error
- func RenameAll(oldpath, newpath string) error
- func RenameFileAll(oldpath, newpath string, dirperm os.FileMode) error
- func SameFile(fi1, fi2 string) bool
- func TempAll(dir, pattern string) (f *os.File, err error)
- func TempFileAll(dir, pattern string, dirperm os.FileMode) (f *os.File, err error)
- func TouchAll(path string) (*os.File, error)
- func UnlinkOldestFiles(pattern string, quora DiskQuota) error
- func UnlinkOldestFilesFunc(pattern string, quora DiskQuota, f func(name string) bool) error
- func WriteAll(filename string, data []byte) error
- func WriteAllFrom(filename string, r io.Reader) error
- func WriteFileAll(filename string, data []byte, dirperm, fileperm os.FileMode) error
- func WriteFileAllFrom(filename string, r io.Reader, dirperm, fileperm os.FileMode) error
- func WriteRenameAll(filename string, data []byte) error
- func WriteRenameAllFrom(filename string, r io.Reader) error
- func WriteRenameFileAll(filename string, data []byte, dirperm os.FileMode) error
- func WriteRenameFileAllFrom(filename string, r io.Reader, dirperm os.FileMode) error
- type CacheFile
- func (o *CacheFile) ApplyOptions(options ...CacheFileOption) *CacheFile
- func (f *CacheFile) BucketKey(name string) string
- func (f *CacheFile) Get(name string) (cacheFilePath, cacheMetaPath string, hit bool, err error)
- func (f *CacheFile) Put(name string, r io.Reader) (cacheFilePath string, refreshed bool, err error)
- type CacheFileOption
- func WithCacheFile(v CacheFile) CacheFileOption
- func WithCacheFileBucketKeyFunc(v func(key string) string) CacheFileOption
- func WithCacheFileBucketRootDir(v string) CacheFileOption
- func WithCacheFileCacheExpiredAfter(v time.Duration) CacheFileOption
- func WithCacheFileCacheMetaExt(v string) CacheFileOption
- type CacheFileOptionFunc
- type DiskQuota
- type EmptyCacheFileOption
- type FileInfos
- type FileModeTimeDescSlice
- type FileModeTimeSlice
- type RotateFile
- type RotateMode
- type WalkFileInfo
- type WalkFileInfos
Examples ¶
Constants ¶
const ( DefaultPermissionFile os.FileMode = 0666 DefaultPermissionDirectory os.FileMode = 0755 DefaultFlagCreateIfNotExist = os.O_RDWR | os.O_CREATE DefaultFlagCreateTruncate = os.O_RDWR | os.O_CREATE | os.O_TRUNC DefaultFlagCreate = DefaultFlagCreateTruncate DefaultFlagCreateAppend = os.O_RDWR | os.O_CREATE | os.O_APPEND DefaultFlagLock = os.O_RDWR | os.O_CREATE | os.O_EXCL )
Variables ¶
Functions ¶
func Append ¶
Append creates or appends the dst file or dir, filled with content from src file. If the dst file already exists, it is truncated. If the dst file does not exist, it is created with mode 0666 (before umask). If the dst dir does not exist, it is created with mode 0755 (before umask). parent dirs will not be created, otherwise, use AppendAll instead.
func AppendAll ¶
AppendAll appends data to a file named by filename. If the file does not exist, AppendAll creates it with mode 0666 (before umask) If the dir does not exist, AppendAll creates it with 0755 (before umask) (before umask); otherwise AppendAll appends it before writing, without changing permissions.
func AppendAllFrom ¶
AppendAllFrom appends data to a file named by filename from r until EOF or error. If the file does not exist, AppendAllFrom creates it with mode 0666 (before umask) If the dir does not exist, AppendAllFrom creates it with 0755 (before umask) (before umask); otherwise AppendAllFrom appends it before writing, without changing permissions.
func AppendAllIfNotExist ¶
AppendAllIfNotExist appends the named file or dir. If the file does not exist, it is created with mode 0666 (before umask). If the dir does not exist, it is created with mode 0755 (before umask). If path is already a directory, CreateAllIfNotExist does nothing and returns nil.
func AppendFileAll ¶
AppendFileAll is the generalized open call; most users will use AppendAll instead. It appends data to a file named by filename. If the file does not exist, AppendFileAll creates it with permissions fileperm (before umask) If the dir does not exist, AppendFileAll creates it with permissions dirperm (before umask) otherwise AppendFileAll appends it before writing, without changing permissions.
func AppendFileAllFrom ¶
AppendFileAllFrom is the generalized open call; most users will use AppendFileFrom instead. It appends data to a file named by filename from r until EOF or error. If the file does not exist, AppendFileAllFrom creates it with permissions fileperm (before umask) If the dir does not exist, AppendFileAllFrom creates it with permissions dirperm (before umask) otherwise AppendFileAllFrom appends it before writing, without changing permissions.
func AppendTruncate ¶
AppendTruncate truncates the original src file in place after appending or creating a copy dst, instead of moving the src file to dir and optionally creating a new one src.
func AppendTruncateAll ¶
AppendTruncateAll truncates the original src file in place after appending or creating a copy dst, instead of moving the src file to dir and optionally creating a new one src. parent dirs will be created with dirperm if not exist.
func ChtimesNow ¶
ChtimesNow changes the access and modification times of the named file with Now, similar to the Unix utime() or utimes() functions.
The underlying filesystem may truncate or round the values to a less precise time unit. If there is an error, it will be of type *PathError.
func Copy ¶
Copy creates or truncates the dst file or dir, filled with content from src file. If the dst file already exists, it is truncated. If the dst file does not exist, it is created with mode 0666 (before umask). If the dst dir does not exist, it is created with mode 0755 (before umask). parent dirs will not be created, otherwise, use CopyAll instead.
func CopyAll ¶
CopyAll creates or truncates the dst file or dir, filled with content from src file. If the dst file already exists, it is truncated. If the dst file does not exist, it is created with mode 0666 (before umask). If the dst dir does not exist, it is created with mode 0755 (before umask).
func CopyAppendAll ¶
CopyAppendAll creates or appends the dst file or dir, filled with content from src file. If the dst file already exists, it is truncated. If the dst file does not exist, it is created with mode 0666 (before umask). If the dst dir does not exist, it is created with mode 0755 (before umask).
func CopyFile ¶
CopyFile is the generalized open call; most users will use Copy or Append instead. It opens the named file or directory with specified flag (O_RDONLY etc.). CopyFile copies from src to dst. parent dirs will not be created, otherwise, use CopyFileAll instead.
func CopyFileAll ¶
CopyFileAll is the generalized open call; most users will use CopyAll or AppendAll instead. It opens the named file or directory with specified flag (O_RDONLY etc.). If the dst file does not exist, and the O_CREATE flag is passed, it is created with mode fileperm (before umask). If the dst directory does not exist, it is created with mode dirperm (before umask). If successful, methods on the returned File can be used for I/O. If there is an error, it will be of type *PathError.
func CopyRename ¶
CopyRename makes a copy of the src file, but don't change the original src at all. This option can be used, for instance, to make a snapshot of the current log file, or when some other utility needs to truncate or parse the file.
func CopyRenameAll ¶
CopyRenameAll makes a copy of the src file, but don't change the original src at all. This option can be used, for instance, to make a snapshot of the current log file, or when some other utility needs to truncate or parse the file. parent dirs will be created with dirperm if not exist.
func CopyRenameFile ¶
CopyRenameFile is the generalized open call; most users will use CopyRename instead. It opens the named file or directory with specified flag (O_RDONLY etc.). CopyTruncateFile copies from src to dst and truncates src. parent dirs will not be created, otherwise, use CopyRenameFileAll instead.
func CopyRenameFileAll ¶
CopyRenameFileAll is the generalized open call; most users will use CopyRenameAll instead. It makes a copy of the src file, but don't change the original src at all. CopyRenameFileAll renames from src to dst and creates src if not exist. parent dirs will be created with dirperm if not exist. CopyRenameFileAll = RenameFileAll(src->dst) + OpenFile(src)
func CopyTruncate ¶
CopyTruncate truncates the original src file in place after creating a copy dst, instead of moving the src file to dir and optionally creating a new one src. It can be used when some program can‐ not be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost.
func CopyTruncateAll ¶
CopyTruncateAll truncates the original src file in place after creating a copy dst, instead of moving the src file to dir and optionally creating a new one src. It can be used when some program can‐ not be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. parent dirs will be created with dirperm if not exist.
func CopyTruncateFile ¶
CopyTruncateFile is the generalized open call; most users will use CopyTruncate or AppendTruncate instead. It opens the named file or directory with specified flag (O_RDONLY etc.). CopyTruncateFile copies from src to dst and truncates src. parent dirs will not be created, otherwise, use CopyTruncateFileAll instead. CopyTruncateFile = CopyFile(src->dst) + Truncate(src)
func CopyTruncateFileAll ¶
func CopyTruncateFileAll(dst string, src string, flag int, dirperm, fileperm os.FileMode, size int64) error
CopyTruncateFileAll is the generalized open call; most users will use CopyTruncateAll or AppendTruncateAll instead. It opens the named file or directory with specified flag (O_RDONLY etc.). CopyTruncateFileAll copies from src to dst and truncates src. parent dirs will be created with dirperm if not exist.
func CreateAll ¶
CreateAll creates or truncates the named file or dir. If the file already exists, it is truncated. If the file does not exist, it is created with mode 0666 (before umask). If the dir does not exist, it is created with mode 0755 (before umask).
func CreateAllIfNotExist ¶
CreateAllIfNotExist creates the named file or dir. If the file does not exist, it is created with mode 0666 (before umask). If the dir does not exist, it is created with mode 0755 (before umask). If path is already a directory, CreateAllIfNotExist does nothing and returns nil.
func DiskUsage ¶
func DiskUsage(path string) (total int64, free int64, avail int64, inodes int64, inodesFree int64, err error)
DiskUsage returns total and free bytes available in a directory, e.g. `/`.
Example ¶
package main import ( "fmt" os_ "github.com/searKing/golang/go/os" ) func main() { total, free, avail, inodes, inodesFree, err := os_.DiskUsage("/tmp") if err != nil { return } fmt.Printf("total :%d B, free: %d B, avail: %d B, inodes: %d, inodesFree: %d", total, free, avail, inodes, inodesFree) // total :499963174912 B, free: 57534603264 B, avail: 57534603264 B, inodes: 566386444, inodesFree: 561861360 }
Output:
func GetAbsBinDir ¶
func LockAll ¶
LockAll creates the named file or dir. If the file already exists, error returned. If the file does not exist, it is created with mode 0666 (before umask). If the dir does not exist, it is created with mode 0755 (before umask).
func MD5CacheKey ¶ added in v1.2.101
func Make ¶
Make creates a directory named path and returns nil, or else returns an error. If the dir does not exist, it is created with mode 0755 (before umask).
func MakeAll ¶
MakeAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. If the dir does not exist, it is created with mode 0755 (before umask).
func MaxSeqFunc ¶ added in v1.2.100
func MaxSeqFunc(pattern string, handler func(name string) bool) (prefix string, seq int, suffix string)
MaxSeqFunc return hit seq or max seq else set by NextFile split pattern by the last wildcard "*" All errors that arise visiting files and directories are filtered by fn: see the filepath.WalkFunc documentation for details.
func MkdirTempAll ¶ added in v1.2.102
MkdirTempAll creates a new temporary directory in the directory dir and returns the pathname of the new directory. The new directory's name is generated by adding a random string to the end of pattern. If pattern includes a "*", the random string replaces the last "*" instead. If dir is the empty string, MkdirTemp uses the default directory for temporary files, as returned by TempDir. Multiple programs or goroutines calling MkdirTemp simultaneously will not choose the same directory. It is the caller's responsibility to remove the directory when it is no longer needed. If the dir does not exist, TempAll creates it with 0755 (before umask)
func MkdirTempDirAll ¶ added in v1.2.102
MkdirTempDirAll is the generalized open call; most users will use MkdirTempAll instead. If the directory does not exist, it is created with mode dirperm (before umask).
func NextFile ¶
NextFile creates a new file, opens the file for reading and writing, and returns the resulting *os.File. The filename is generated by taking pattern and adding a seq increased string to the end. If pattern includes a "*", the random string replaces the last "*". Multiple programs calling NextFile simultaneously will not choose the same file. The caller can use f.Name() to find the pathname of the file. It is the caller's responsibility to remove the file when no longer needed.
func OpenAll ¶
OpenAll opens the named file or dir for reading. If successful, methods on the returned file or dir can be used for reading; the associated file descriptor has mode O_RDONLY. If there is an error, it will be of type *PathError.
func OpenFileAll ¶
OpenFileAll is the generalized open call; most users will use OpenAll or CreateAll instead. It opens the named file or directory with specified flag (O_RDONLY etc.). If the file does not exist, and the O_CREATE flag is passed, it is created with mode fileperm (before umask). If the directory does not exist, it is created with mode dirperm (before umask). If successful, methods on the returned File can be used for I/O. If there is an error, it will be of type *PathError.
func PathExists ¶
func ReLink ¶
ReLink creates or replaces newname as a hard link to the oldname file. If there is an error, it will be of type *LinkError.
func ReSymlink ¶
ReSymlink creates or replace newname as a symbolic link to oldname. If there is an error, it will be of type *LinkError.
func ReadDirN ¶ added in v1.2.100
ReadDirN reads the named directory, returning a slice of its directory entries sorted by filename. If an error occurs reading the directory, ReadDirN returns a slice of entries it was able to read before the error, along with the error.
If n > 0, ReadDirN returns at most n DirEntry records. In this case, if ReadDirN returns an empty slice, it will return an error explaining why. At the end of a directory, the error is io.EOF.
If n <= 0, ReadDirN returns all the DirEntry records remaining in the directory. When it succeeds, it returns a nil error (not io.EOF). To read all dirs, see os.ReadDir, or set n as -1.
Example ¶
package main import ( "fmt" "log" os_ "github.com/searKing/golang/go/os" ) func main() { files, err := os_.ReadDirN(".", 1) if err != nil { log.Fatal(err) } for _, file := range files { fmt.Println(file.Name()) } }
Output:
func RemoveIfExist ¶
RemoveIfExist removes the named file or (empty) directory. If the path does not exist, RemoveIfExist returns nil (no error). If there is an error, it will be of type *PathError.
func RenameAll ¶
RenameAll renames (moves) oldpath to newpath. If newpath already exists and is not a directory, Rename replaces it. OS-specific restrictions may apply when oldpath and newpath are in different directories. If there is an error, it will be of type *LinkError. If the dir does not exist, it is created with mode 0755 (before umask).
func RenameFileAll ¶
RenameFileAll is the generalized open call; most users will use RenameAll instead. It renames (moves) oldpath to newpath.
func SameFile ¶
SameFile reports whether fi1 and fi2 describe the same file. Overload os.SameFile by file path
func TempAll ¶
TempAll creates a new temporary file in the directory dir, opens the file for reading and writing, and returns the resulting *os.File. If the file does not exist, TempAll creates it with mode 0600 (before umask) If the dir does not exist, TempAll creates it with 0755 (before umask) otherwise TempAll truncates it before writing, without changing permissions.
func TempFileAll ¶
TempFileAll is the generalized open call; most users will use TempAll instead. If the directory does not exist, it is created with mode dirperm (before umask).
func TouchAll ¶
TouchAll creates the named file or dir. If the file already exists, it is touched to now. If the file does not exist, it is created with mode 0666 (before umask). If the dir does not exist, it is created with mode 0755 (before umask).
func UnlinkOldestFiles ¶
UnlinkOldestFiles unlink old files if need
func UnlinkOldestFilesFunc ¶ added in v1.2.22
UnlinkOldestFilesFunc unlink old files satisfying f(c) if need
func WriteAll ¶
WriteAll writes data to a file named by filename. If the file does not exist, WriteAll creates it with mode 0666 (before umask) If the dir does not exist, WriteAll creates it with 0755 (before umask) otherwise WriteAll truncates it before writing, without changing permissions.
func WriteAllFrom ¶
WriteAllFrom writes data to a file named by filename from r until EOF or error. If the file does not exist, WriteAll creates it with mode 0666 (before umask) If the dir does not exist, WriteAll creates it with 0755 (before umask) otherwise WriteAll truncates it before writing, without changing permissions.
func WriteFileAll ¶
WriteFileAll is the generalized open call; most users will use WriteAll instead. It writes data to a file named by filename. If the file does not exist, WriteFileAll creates it with permissions fileperm (before umask) If the dir does not exist, WriteFileAll creates it with permissions dirperm (before umask) otherwise WriteFileAll truncates it before writing, without changing permissions.
func WriteFileAllFrom ¶
WriteFileAllFrom is the generalized open call; most users will use WriteAllFrom instead. It writes data to a file named by filename from r until EOF or error. If the file does not exist, WriteFileAllFrom creates it with permissions fileperm (before umask) If the dir does not exist, WriteFileAllFrom creates it with permissions dirperm (before umask) otherwise WriteFileAllFrom truncates it before writing, without changing permissions.
func WriteRenameAll ¶
WriteRenameAll writes data to a temp file and rename to the new file named by filename. If the file does not exist, WriteRenameAll creates it with mode 0666 (before umask) If the dir does not exist, WriteRenameAll creates it with 0755 (before umask) otherwise WriteRenameAll truncates it before writing, without changing permissions.
func WriteRenameAllFrom ¶
WriteRenameAllFrom writes data to a temp file from r until EOF or error, and rename to the new file named by filename. WriteRenameAllFrom is safer than WriteAllFrom as before Write finished, nobody can find the unfinished file. If the file does not exist, WriteRenameAllFrom creates it with mode 0666 (before umask) If the dir does not exist, WriteRenameAllFrom creates it with 0755 (before umask) otherwise WriteRenameAllFrom truncates it before writing, without changing permissions.
func WriteRenameFileAll ¶
WriteRenameFileAll is the generalized open call; most users will use WriteRenameAll instead. WriteRenameFileAll is safer than WriteFileAll as before Write finished, nobody can find the unfinished file. It writes data to a temp file and rename to the new file named by filename. If the file does not exist, WriteRenameFileAll creates it with permissions fileperm If the dir does not exist, WriteRenameFileAll creates it with permissions dirperm (before umask); otherwise WriteRenameFileAll truncates it before writing, without changing permissions.
func WriteRenameFileAllFrom ¶
WriteRenameFileAllFrom is the generalized open call; most users will use WriteRenameAllFrom instead. WriteRenameFileAllFrom is safer than WriteRenameAllFrom as before Write finished, nobody can find the unfinished file. It writes data to a temp file and rename to the new file named by filename. If the file does not exist, WriteRenameFileAllFrom creates it with permissions fileperm If the dir does not exist, WriteRenameFileAllFrom creates it with permissions dirperm (before umask); otherwise WriteRenameFileAllFrom truncates it before writing, without changing permissions.
Types ¶
type CacheFile ¶ added in v1.2.101
type CacheFile struct { BucketRootDir string // cache root dir // generate bucket key from key(file path) // bucket key should not contain any of the magic characters recognized by [filepath.Match] // otherwise, bucket key will be escaped by MD5CacheKey // see: https://github.com/golang/go/issues/13516 BucketKeyFunc func(key string) string CacheMetaExt string // the file name extension used by path. ".cache" if empty CacheExpiredAfter time.Duration // Cache file expiration time, lazy expire cache files base on cache URL modification time }
CacheFile is a package cache(Eventual consistency), backed by a file system directory tree.
It is safe for multiple processes on a single machine to use the same cache directory in a local file system simultaneously. They will coordinate using operating system file locks and may duplicate effort but will not corrupt the cache.
However, it is NOT safe for multiple processes on different machines to share a cache directory (for example, if the directory were stored in a network file system). File locking is notoriously unreliable in network file systems and may not suffice to protect the cache.
func NewCacheFile ¶ added in v1.2.101
func NewCacheFile(opts ...CacheFileOption) *CacheFile
Example ¶
package main import ( "fmt" "strconv" "strings" "time" os_ "github.com/searKing/golang/go/os" ) func main() { file := os_.NewCacheFile(os_.WithCacheFileBucketRootDir("log"), os_.WithCacheFileCacheExpiredAfter(10*time.Millisecond), os_.WithCacheFileBucketKeyFunc(func(url string) string { return "always conflict key" })) for i := 0; i < 10000; i++ { time.Sleep(1 * time.Millisecond) _, _, err := file.Put(fmt.Sprintf("cache%d", i), strings.NewReader(strconv.Itoa(i))) if err != nil { fmt.Printf("%d, err: %v\n", i, err) } } }
Output:
func (*CacheFile) ApplyOptions ¶ added in v1.2.101
func (o *CacheFile) ApplyOptions(options ...CacheFileOption) *CacheFile
ApplyOptions call apply() for all options one by one
type CacheFileOption ¶ added in v1.2.101
type CacheFileOption interface {
// contains filtered or unexported methods
}
A CacheFileOption sets options.
func WithCacheFile ¶ added in v1.2.101
func WithCacheFile(v CacheFile) CacheFileOption
WithCacheFile sets CacheFile.
func WithCacheFileBucketKeyFunc ¶ added in v1.2.101
func WithCacheFileBucketKeyFunc(v func(key string) string) CacheFileOption
WithCacheFileBucketKeyFunc sets BucketKeyFunc in CacheFile. generate bucket key from key(file path) bucket key should not contain any of the magic characters recognized by filepath.Match otherwise, bucket key will be escaped by MD5CacheKey see: https://github.com/golang/go/issues/13516
func WithCacheFileBucketRootDir ¶ added in v1.2.101
func WithCacheFileBucketRootDir(v string) CacheFileOption
WithCacheFileBucketRootDir sets BucketRootDir in CacheFile. cache root dir
func WithCacheFileCacheExpiredAfter ¶ added in v1.2.101
func WithCacheFileCacheExpiredAfter(v time.Duration) CacheFileOption
WithCacheFileCacheExpiredAfter sets CacheExpiredAfter in CacheFile. Cache file expiration time, lazy expire cache files base on cache URL modification time
func WithCacheFileCacheMetaExt ¶ added in v1.2.101
func WithCacheFileCacheMetaExt(v string) CacheFileOption
WithCacheFileCacheMetaExt sets CacheMetaExt in CacheFile. the file name extension used by path. ".cache" if empty
type CacheFileOptionFunc ¶ added in v1.2.101
type CacheFileOptionFunc func(*CacheFile)
CacheFileOptionFunc wraps a function that modifies CacheFile into an implementation of the CacheFileOption interface.
type DiskQuota ¶ added in v1.2.22
type DiskQuota struct { MaxAge time.Duration // max age of files MaxCount int // max count of files MaxUsedProportion float32 // max used proportion of files MaxIUsedProportion float32 // max used proportion of inodes }
func (DiskQuota) ExceedBytes ¶ added in v1.2.22
func (DiskQuota) ExceedInodes ¶ added in v1.2.22
type EmptyCacheFileOption ¶ added in v1.2.101
type EmptyCacheFileOption struct{}
EmptyCacheFileOption does not alter the configuration. It can be embedded in another structure to build custom options.
This API is EXPERIMENTAL.
type FileModeTimeDescSlice ¶
type FileModeTimeDescSlice []string
FileModeTimeDescSlice sorts filenames by mode time in decrease order
func (FileModeTimeDescSlice) Len ¶
func (s FileModeTimeDescSlice) Len() int
func (FileModeTimeDescSlice) Less ¶
func (s FileModeTimeDescSlice) Less(i, j int) bool
func (FileModeTimeDescSlice) Swap ¶
func (s FileModeTimeDescSlice) Swap(i, j int)
type FileModeTimeSlice ¶
type FileModeTimeSlice []string
FileModeTimeSlice sorts filenames by mode time in increase order
func (FileModeTimeSlice) Len ¶
func (s FileModeTimeSlice) Len() int
func (FileModeTimeSlice) Less ¶
func (s FileModeTimeSlice) Less(i, j int) bool
func (FileModeTimeSlice) Swap ¶
func (s FileModeTimeSlice) Swap(i, j int)
type RotateFile ¶
type RotateFile struct { RotateMode RotateMode FilePathPrefix string // FilePath = FilePathPrefix + now.Format(filePathRotateLayout) FilePathRotateLayout string // Time layout to format rotate file RotateFileGlob string // file glob to clean // sets the symbolic link name that gets linked to the current file name being used. FileLinkPath string // Rotate files are rotated until RotateInterval expired before being removed // take effects if only RotateInterval is bigger than 0. RotateInterval time.Duration // Rotate files are rotated if they grow bigger then size bytes. // take effects if only RotateSize is bigger than 0. RotateSize int64 // max age of a log file before it gets purged from the file system. // Remove rotated logs older than duration. The age is only checked if the file is // to be rotated. // take effects if only MaxAge is bigger than 0. MaxAge time.Duration // Rotate files are rotated MaxCount times before being removed // take effects if only MaxCount is bigger than 0. MaxCount int // Force File Rotate when start up ForceNewFileOnStartup bool // PreRotateHandler called before file rotate // name means file path rotated PreRotateHandler func(name string) // PostRotateHandler called after file rotate // name means file path rotated PostRotateHandler func(name string) // contains filtered or unexported fields }
RotateFile logrotate reads everything about the log files it should be handling from the series of con‐ figuration files specified on the command line. Each configuration file can set global options (local definitions override global ones, and later definitions override earlier ones) and specify rotatefiles to rotate. A simple configuration file looks like this:
func NewRotateFile ¶
func NewRotateFile(layout string) *RotateFile
Example ¶
package main import ( "fmt" "time" os_ "github.com/searKing/golang/go/os" ) func main() { file := os_.NewRotateFile("log/test.2006-01-02-15-04-05.log") defer file.Close() file.MaxCount = 5 file.RotateInterval = 5 * time.Second file.MaxAge = time.Hour file.FileLinkPath = "log/s.log" for i := 0; i < 10000; i++ { time.Sleep(1 * time.Millisecond) file.WriteString(time.Now().String()) if err := file.Rotate(false); err != nil { fmt.Printf("%d, err: %v\n", i, err) } } }
Output:
func NewRotateFileWithStrftime ¶
func NewRotateFileWithStrftime(strftimeLayout string) *RotateFile
Example ¶
package main import ( "fmt" "time" os_ "github.com/searKing/golang/go/os" ) func main() { file := os_.NewRotateFileWithStrftime("log/test.%Y-%m-%d-%H-%M-%S.log") file.MaxCount = 5 file.RotateInterval = 5 * time.Second file.MaxAge = time.Hour file.FileLinkPath = "log/s.log" for i := 0; i < 10000; i++ { time.Sleep(1 * time.Millisecond) file.WriteString(time.Now().String()) if err := file.Rotate(false); err != nil { fmt.Printf("%d, err: %v\n", i, err) } } }
Output:
func (*RotateFile) Close ¶
func (f *RotateFile) Close() error
Close satisfies the io.Closer interface. You must call this method if you performed any writes to the object.
func (*RotateFile) Rotate ¶
func (f *RotateFile) Rotate(forceRotate bool) error
Rotate forcefully rotates the file. If the generated file name clash because file already exists, a numeric suffix of the form ".1", ".2", ".3" and so forth are appended to the end of the log file
This method can be used in conjunction with a signal handler so to emulate servers that generate new log files when they receive a SIGHUP
func (*RotateFile) WriteAt ¶
func (f *RotateFile) WriteAt(b []byte, off int64) (n int, err error)
WriteAt writes len(b) bytes to the File starting at byte offset off. It returns the number of bytes written and an error, if any. WriteAt returns a non-nil error when n != len(b).
If file was opened with the O_APPEND flag, WriteAt returns an error.
func (*RotateFile) WriteString ¶
func (f *RotateFile) WriteString(s string) (n int, err error)
WriteString is like Write, but writes the contents of string s rather than a slice of bytes.
type RotateMode ¶
type RotateMode int
RotateMode represents a way to rotate file. see details: https://man7.org/linux/man-pages/man8/logrotate.8.html
const ( // RotateModeNew create new rotate file directly // Immediately after rotation (before the postrotate script is run) the log file is created (with // the same name as the log file just rotated). // see option: `create` in https://man7.org/linux/man-pages/man8/logrotate.8.html RotateModeNew RotateMode = iota // RotateModeCopyRename Make a copy of the log file, but don't change the original at all. This option can be // used, for instance, to make a snapshot of the current log file, or when some other // utility needs to truncate or parse the file. When this option is used, the create // option will have no effect, as the old log file stays in place. // see option: `copy` in https://man7.org/linux/man-pages/man8/logrotate.8.html RotateModeCopyRename RotateMode = iota // RotateModeCopyTruncate Truncate the original log file in place after creating a copy, instead of moving the // old log file and optionally creating a new one. It can be used when some program can‐ // not be told to close its rotatefile and thus might continue writing (appending) to the // previous log file forever. Note that there is a very small time slice between copying // the file and truncating it, so some logging data might be lost. When this option is // used, the create option will have no effect, as the old log file stays in place. // see option: `copytruncate` in https://man7.org/linux/man-pages/man8/logrotate.8.html RotateModeCopyTruncate RotateMode = iota )
type WalkFileInfo ¶
WalkFileInfo is a wrapper for sort of filepath.WalkFunc
type WalkFileInfos ¶
type WalkFileInfos []WalkFileInfo
func (WalkFileInfos) Len ¶
func (w WalkFileInfos) Len() int
func (WalkFileInfos) Less ¶
func (w WalkFileInfos) Less(i, j int) bool
func (WalkFileInfos) Swap ¶
func (w WalkFileInfos) Swap(i, j int)