Documentation
¶
Overview ¶
Index ¶
- Variables
- func CreateDirIfNotExists(path string) (err error)
- func CreateTargetDirIfNotExists(path string) (err error)
- func Exists(path string) bool
- func ExistsFS(q fs.FS, path string) bool
- func GetFdName(fd uintptr) (name string, err error)
- func IsDir(path string) (bool, error)
- func ReadPidFile(name string) (pid int, err error)
- func SaveCurrentPID(fileName string) error
- func ZipFiles(w io.Writer, filePaths []string) error
- type LockFile
Constants ¶
This section is empty.
Variables ¶
var ( // ErrWoldBlock indicates on locking pid-file by another process. ErrWouldBlock = syscall.EWOULDBLOCK )
Functions ¶
func CreateDirIfNotExists ¶
CreateDirIfNotExists ensures that the target directory of the directory exists, creating it if it does not exist.
func CreateTargetDirIfNotExists ¶
CreateTargetDirIfNotExists ensures that the target directory of the file/directory exists, creating it if it does not exist. If "path" ends by a slash it's considering as a directory, else it's a file
func ReadPidFile ¶
ReadPidFile reads process id from file with give name and returns pid. If unable read from a file, returns error.
func SaveCurrentPID ¶
SaveCurrentPID Writes a pid file, but first make sure it doesn't exist with a running pid. https://gist.github.com/davidnewhall/3627895a9fc8fa0affbd747183abca39
func ZipFiles ¶
ZipFiles writes a zip archive containing the specified files to w. Only the base name of each file is used in the archive (not the full path). Files are compressed using the Deflate method.
Example - Write to file:
f, err := os.Create("archive.zip")
if err != nil {
return err
}
defer f.Close()
err = file.ZipFiles(f, []string{"/path/to/file1.txt", "/path/to/file2.txt"})
Example - Write to memory buffer:
var buf bytes.Buffer
err := file.ZipFiles(&buf, []string{"/path/to/file.txt"})
if err != nil {
return err
}
data := buf.Bytes()
Example - Write to HTTP response:
func handleDownload(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", "attachment; filename=archive.zip")
err := file.ZipFiles(w, filePaths)
// ...
}
Types ¶
type LockFile ¶
LockFile wraps *os.File and provide functions for locking of files.
func CreatePidFile ¶
CreatePidFile opens the named file, applies exclusive lock and writes current process id to file.
func NewLockFile ¶
NewLockFile returns a new LockFile with the given File.
func OpenLockFile ¶
OpenLockFile opens the named file with flags os.O_RDWR|os.O_CREATE and specified perm. If successful, function returns LockFile for opened file.
func (*LockFile) Lock ¶
Lock apply exclusive lock on an open file. If file already locked, returns error.
func (*LockFile) ReadPid ¶
ReadPid reads process id from file and returns pid. If unable read from a file, returns error.