fsx

package
v3.19.0-alpha.5 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package fsx contains io/fs extensions.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotRegularFile = errors.New("not a regular file")

ErrNotRegularFile indicates you're not opening a regular file.

Functions

func DirectoryExists added in v3.17.0

func DirectoryExists(filename string) bool

DirectoryExists returns whether the given filename exists and is a directory.

func OpenFile

func OpenFile(pathname string) (fs.File, error)

OpenFile is a wrapper for os.OpenFile that ensures that we're opening a file rather than a directory. If you are not opening a regular file, this func returns an error.

As mentioned in CONTRIBUTING.md, this is the function you SHOULD be using when opening files.

Example (OpeningDir)
package main

import (
	"errors"
	"log"
	"syscall"

	"github.com/ooni/probe-cli/v3/internal/fsx"
)

func main() {
	filep, err := fsx.OpenFile("testdata")
	if !errors.Is(err, syscall.ENOENT) {
		log.Fatal("unexpected error", err)
	}
	if filep != nil {
		log.Fatal("expected nil fp")
	}
}
Output:

Example (OpeningFile)
package main

import (
	"context"
	"fmt"
	"log"
	"path/filepath"

	"github.com/ooni/probe-cli/v3/internal/fsx"
	"github.com/ooni/probe-cli/v3/internal/netxlite"
)

func main() {
	filep, err := fsx.OpenFile(filepath.Join("testdata", "testfile.txt"))
	if err != nil {
		log.Fatal("unexpected error", err)
	}
	data, err := netxlite.ReadAllContext(context.Background(), filep)
	if err != nil {
		log.Fatal("unexpected error", err)
	}
	fmt.Printf("%d\n", len(data))
}
Output:

func RegularFileExists added in v3.17.0

func RegularFileExists(filename string) bool

RegularFileExists returns whether the given filename exists and is a regular file.

Types

This section is empty.

Jump to

Keyboard shortcuts

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