os

package
v1.1.18 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2021 License: MIT Imports: 16 Imported by: 2

Documentation

Index

Examples

Constants

View Source
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

View Source
var ShutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}

Functions

func Append added in v0.0.114

func Append(dst string, src string) error

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 added in v0.0.113

func AppendAll(dst string, src string) error

AppendAll 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 AppendAllIfNotExist added in v0.0.135

func AppendAllIfNotExist(path string) (*os.File, error)

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 AppendTruncate added in v0.0.135

func AppendTruncate(dst string, src string) error

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 added in v0.0.135

func AppendTruncateAll(dst string, src string) error

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 added in v0.0.23

func ChtimesNow(name string) error

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 added in v0.0.114

func Copy(dst string, src string) error

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 added in v0.0.113

func CopyAll(dst string, src string) error

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 CopyFile added in v0.0.104

func CopyFile(dst string, src string, flag int, perm os.FileMode) error

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 added in v0.0.113

func CopyFileAll(dst string, src string, flag int, dirperm, fileperm os.FileMode) error

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 added in v0.0.135

func CopyRename(dst string, src string) error

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 added in v0.0.135

func CopyRenameAll(dst string, src string) error

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 added in v0.0.135

func CopyRenameFile(dst string, src string, flag int, perm os.FileMode) error

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 added in v0.0.135

func CopyRenameFileAll(dst string, src string, flag int, dirperm, fileperm os.FileMode) error

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 added in v0.0.135

func CopyTruncate(dst string, src string) error

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 added in v0.0.135

func CopyTruncateAll(dst string, src string) error

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 added in v0.0.135

func CopyTruncateFile(dst string, src string, flag int, perm os.FileMode, size int64) error

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 added in v0.0.135

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 added in v0.0.23

func CreateAll(path string) (*os.File, error)

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 added in v0.0.23

func CreateAllIfNotExist(path string) (*os.File, error)

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 GetAbsBinDir

func GetAbsBinDir() (dir string, err error)

func LockAll added in v0.0.135

func LockAll(path string) (*os.File, error)

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 Make added in v0.0.115

func Make(name string) error

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 added in v0.0.115

func MakeAll(name string) error

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 MaxSeq added in v0.0.135

func MaxSeq(pattern string) (prefix string, seq int, suffix string)

MaxSeq return max seq set by NextFile split pattern by the last wildcard "*"

func NextFile added in v0.0.135

func NextFile(pattern string, seq int) (f *os.File, seqUsed int, err error)

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 added in v0.0.113

func OpenAll(path string) (*os.File, error)

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 added in v0.0.113

func OpenFileAll(path string, flag int, dirperm, fileperm os.FileMode) (*os.File, error)

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 PathExists(path string) (bool, error)
func ReLink(oldname, newname string) error

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(oldname, newname string) error

ReSymlink creates or replace newname as a symbolic link to oldname. If there is an error, it will be of type *LinkError.

func RenameAll added in v0.0.135

func RenameAll(oldpath, newpath string) error

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 added in v0.0.135

func RenameFileAll(oldpath, newpath string, dirperm os.FileMode) error

RenameFileAll is the generalized open call; most users will use RenameAll instead. It renames (moves) oldpath to newpath.

func SameFile added in v0.0.104

func SameFile(fi1, fi2 string) bool

SameFile reports whether fi1 and fi2 describe the same file. Overload os.SameFile by file path

func TouchAll added in v0.0.23

func TouchAll(path string) (*os.File, error)

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).

Types

type FileInfos added in v0.0.115

type FileInfos []os.FileInfo

func (FileInfos) Len added in v0.0.115

func (s FileInfos) Len() int

func (FileInfos) Less added in v0.0.115

func (s FileInfos) Less(i, j int) bool

func (FileInfos) Swap added in v0.0.115

func (s FileInfos) Swap(i, j int)

type FileModeTimeDescSlice added in v0.0.135

type FileModeTimeDescSlice []string

sort filename by mode time in decrease order

func (FileModeTimeDescSlice) Len added in v0.0.135

func (s FileModeTimeDescSlice) Len() int

func (FileModeTimeDescSlice) Less added in v0.0.135

func (s FileModeTimeDescSlice) Less(i, j int) bool

func (FileModeTimeDescSlice) Swap added in v0.0.135

func (s FileModeTimeDescSlice) Swap(i, j int)

type FileModeTimeSlice added in v0.0.135

type FileModeTimeSlice []string

sort filename by mode time in increase order

func (FileModeTimeSlice) Len added in v0.0.135

func (s FileModeTimeSlice) Len() int

func (FileModeTimeSlice) Less added in v0.0.135

func (s FileModeTimeSlice) Less(i, j int) bool

func (FileModeTimeSlice) Swap added in v0.0.135

func (s FileModeTimeSlice) Swap(i, j int)

type RotateFile added in v0.0.135

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 added in v0.0.135

func NewRotateFile(layout string) *RotateFile
Example
package main

import (
	"time"

	os_ "github.com/searKing/golang/go/os"
)

func main() {
	file := os_.NewRotateFile("log/test.2006-01-02-15-04-05.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", i, err)
		//}
	}
}
Output:

func NewRotateFileWithStrftime added in v0.0.135

func NewRotateFileWithStrftime(strftimeLayout string) *RotateFile
Example
package main

import (
	"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", i, err)
		//}
	}
}
Output:

func (*RotateFile) Close added in v0.0.135

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 added in v0.0.135

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) Write added in v0.0.135

func (f *RotateFile) Write(b []byte) (n int, err error)

func (*RotateFile) WriteAt added in v0.0.135

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 added in v0.0.135

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 added in v0.0.135

type RotateMode int
const (
	// RotateModeNew create new rotate file directly
	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.
	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.
	RotateModeCopyTruncate RotateMode = iota
)

type WalkFileInfo added in v0.0.115

type WalkFileInfo struct {
	Path     string
	FileInfo os.FileInfo
}

WalkFileInfo is a wrapper for sort of filepath.WalkFunc

type WalkFileInfos added in v0.0.115

type WalkFileInfos []WalkFileInfo

func (WalkFileInfos) Len added in v0.0.115

func (w WalkFileInfos) Len() int

func (WalkFileInfos) Less added in v0.0.115

func (w WalkFileInfos) Less(i, j int) bool

func (WalkFileInfos) Swap added in v0.0.115

func (w WalkFileInfos) Swap(i, j int)

Jump to

Keyboard shortcuts

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