Documentation
¶
Index ¶
- func Copy(src, dst string) error
- func CopyDir(src, dst string) error
- func FilesEqual(paths ...string) (bool, error)
- func IsExist(path string) bool
- func Lock(f *os.File) (bool, error)
- func LockWait(f *os.File, timeout float64) error
- func ParsePath(path string) (string, error)
- func RLock(f *os.File) (bool, error)
- func RLockWait(f *os.File, timeout float64) error
- func Remove(path string) error
- func Touch(path string) error
- func UnLock(f *os.File) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Copy ¶
Copy copies a file from src to dst. It handles files and symbolic links.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/exonlabs/go-utils/pkg/abc/fsx"
)
func main() {
tmpDir := os.TempDir()
srcPath := filepath.Join(tmpDir, "srcfile.txt")
dstPath := filepath.Join(tmpDir, "srcfile_copy.txt")
err := fsx.Copy(srcPath, dstPath)
if err != nil {
fmt.Println(err)
}
}
func CopyDir ¶
CopyDir copies a directory and its contents from src to dst.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/exonlabs/go-utils/pkg/abc/fsx"
)
func main() {
tmpDir := os.TempDir()
srcDir := filepath.Join(tmpDir, "srcdir")
dstDir := filepath.Join(tmpDir, "srcdir_copy")
err := fsx.CopyDir(srcDir, dstDir)
if err != nil {
fmt.Println(err)
}
}
func FilesEqual ¶ added in v0.4.3
FilesEqual checks if files are equal using the extremely fast non-cryptographic xxhash algorithm.
func Lock ¶ added in v0.5.0
Lock acquires an exclusive (write) lock on the file without blocking. Returns false if the lock is already held.
func LockWait ¶ added in v0.5.0
LockWait attempts to acquire an exclusive (write) lock within the given timeout. If timeout is 0 or negative, it waits indefinitely.
func ParsePath ¶
ParsePath validates and returns the absolute path.
Example ¶
package main
import (
"fmt"
"github.com/exonlabs/go-utils/pkg/abc/fsx"
)
func main() {
paths := []string{
"",
" ",
"/",
"/tmp/..",
"/tmp/dir1/../",
"/tmp/dir1/../srcfile.txt",
}
for _, path := range paths {
p, err := fsx.ParsePath(path)
if err == nil {
fmt.Printf("\"%s\" --> \"%s\"\n", path, p)
} else {
fmt.Printf("\"%s\" --> err: %s\n", path, err.Error())
}
}
// Results:
// "" --> err: invalid path
// " " --> err: invalid path
// "/" --> "/"
// "/tmp/.." --> "/"
// "/tmp/dir1/../" --> "/tmp"
// "/tmp/dir1/../srcfile.txt" --> "/tmp/srcfile.txt"
}
func RLock ¶ added in v0.5.0
RLock acquires a shared (read) lock on the file without blocking. Returns false if the lock is already held.
func RLockWait ¶ added in v0.5.0
RLockWait attempts to acquire a shared (read) lock within the given timeout. If timeout is 0 or negative, it waits indefinitely.
Types ¶
This section is empty.