file

package
v0.0.0-...-fd3df41 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

From https://github.com/sevlyar/go-daemon

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrWoldBlock indicates on locking pid-file by another process.
	ErrWouldBlock = syscall.EWOULDBLOCK
)

Functions

func CreateDirIfNotExists

func CreateDirIfNotExists(path string) (err error)

CreateDirIfNotExists ensures that the target directory of the directory exists, creating it if it does not exist.

func CreateTargetDirIfNotExists

func CreateTargetDirIfNotExists(path string) (err error)

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 Exists

func Exists(path string) bool

Exists returns whether a file or directory exists.

func ExistsFS

func ExistsFS(q fs.FS, path string) bool

ExistsFS returns whether a file or directory exists in the given FS.

func GetFdName

func GetFdName(fd uintptr) (name string, err error)

GetFdName returns file name for given descriptor.

func IsDir

func IsDir(path string) (bool, error)

IdDir determines if a file represented by `path` is a directory or not.

func ReadPidFile

func ReadPidFile(name string) (pid int, err error)

ReadPidFile reads process id from file with give name and returns pid. If unable read from a file, returns error.

func SaveCurrentPID

func SaveCurrentPID(fileName string) error

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

func ZipFiles(w io.Writer, filePaths []string) error

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

type LockFile struct {
	*os.File
}

LockFile wraps *os.File and provide functions for locking of files.

func CreatePidFile

func CreatePidFile(name string, perm os.FileMode) (lock *LockFile, err error)

CreatePidFile opens the named file, applies exclusive lock and writes current process id to file.

func NewLockFile

func NewLockFile(file *os.File) *LockFile

NewLockFile returns a new LockFile with the given File.

func OpenLockFile

func OpenLockFile(name string, perm os.FileMode) (lock *LockFile, err error)

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

func (file *LockFile) Lock() error

Lock apply exclusive lock on an open file. If file already locked, returns error.

func (*LockFile) ReadPid

func (file *LockFile) ReadPid() (pid int, err error)

ReadPid reads process id from file and returns pid. If unable read from a file, returns error.

func (*LockFile) Remove

func (file *LockFile) Remove() error

Remove removes lock, closes and removes an open file.

func (*LockFile) Unlock

func (file *LockFile) Unlock() error

Unlock remove exclusive lock on an open file.

func (*LockFile) WritePid

func (file *LockFile) WritePid() (err error)

WritePid writes current process id to an open file.

Jump to

Keyboard shortcuts

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