iotool

package
v0.0.0-...-6244057 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2018 License: AGPL-3.0 Imports: 17 Imported by: 14

Documentation

Index

Constants

View Source
const (
	DEBUG = false

	PREFIX_SEPARATOR = "_"

	STDIN_TOKEN = "-"

	// the smallest read for an io.Reader buffer
	READ_BUFFER_SMALL_SIZE = 512
	// the usual read size for an io.Reader buffer
	READ_BUFFER_SIZE = 4 * 1024
	// the maximum read size for an io.Reader buffer
	READ_BUFFER_MAXIMUM_SIZE = 32 * 1024
)

Variables

View Source
var IsDirectoryError = errors.New("That's a directory!")

well, duh!

View Source
var IsNotDirectoryError = errors.New("That's _not_ a directory!")
View Source
var NotAvailableE = errors.New("That function is not available. This file is faking.")

Functions

func Close

func Close(fileList ...FileInterface)

close a bunch of files from a list.

func CloseChannel

func CloseChannel(fileHelper *FileHelper) chan<- FileInterface

give back a close chan to push FileInterfaces to; when done, close the channel.

func CloseFilesFromChannel

func CloseFilesFromChannel(fileHelper *FileHelper, fileList chan FileInterface)

close a bunch of files via a channel

func Copy

func Copy(source io.Reader, destination io.Writer) (written int64, e error)

copy FROM -> TO

func CopyBuffer

func CopyBuffer(source io.Reader, destination io.Writer, buffer []byte) (written int64, e error)

copy FROM -> TO

func CopyN

func CopyN(source io.Reader, destination io.Writer, n int64) (written int64, e error)

copy FROM -> TO

func IsDirectoryE

func IsDirectoryE(e error) bool

is the error "/path/ is a directory"

func IsExist

func IsExist(e error) bool

TODO simplee for now mirror os functions

func IsNotExist

func IsNotExist(e error) bool

TODO simplee for now mirror os functions

func IsPermissionDenied

func IsPermissionDenied(e error) bool

is the error a permission denied error

func ListDirectory

func ListDirectory(path string) (names []string, e error)

func OpenFiles

func OpenFiles(fileHelper *FileHelper, files ...string) <-chan FileInterface

open N files. please keep limits like ulimit file.max in mind. see also parallel.OpenFiles() for a parallel version and parallel.OpenFilesIntoChannel() consider using iotool.ToggleFileAdviceWillNeed(). for error handling, use FileHelper.SetE(). it's your responsibility to close the files...

func OpenFilesFromChannel

func OpenFilesFromChannel(fileHelper *FileHelper, fileList <-chan string) <-chan FileInterface

open N files from channel. please keep limits like ulimit file.max in mind. see also parallel.OpenFiles() for a parallel version and parallel.OpenFilesIntoChannel() consider using iotool.ToggleFileAdviceWillNeed(). for error handling, use FileHelper.SetE(). it's your responsibility to close the files...

func ReadFile

func ReadFile(path string) (result []byte, e error)

func ReadFileAsString

func ReadFileAsString(path string) (result string, e error)

func ReadFileIntoBuffer

func ReadFileIntoBuffer(helper *FileHelper, path string, buffered *NamedBuffer)

go; read a file into a channel buffer

func ReadIntoBuffer

func ReadIntoBuffer(helper *FileHelper, reader io.Reader, buffered *NamedBuffer)

go; read a reader into a buffer

func Remove

func Remove(handler FileInterface) error

FIXME needs rethinking

func TemporaryName

func TemporaryName(prefix string) string

return a filename with full path in os.TempDir(). prefix should be your program name.

func WriteFile

func WriteFile(helper *FileHelper, path string, data []byte) (e error)

write data to a file if you want to append, use FileHelper.ToggleAppend() if you want it truncated, use FileHelper.ToggleTruncate()

func WriteTo

func WriteTo(helper *FileHelper, path string, data []byte) (e error)

alias for WriteFile to mirror io.ioutil behaviour

Types

type CopyEntryInterface

type CopyEntryInterface interface {
	From() string
	To() string

	FromInfo() FileInfoInterface
	ToInfo() FileInfoInterface
}

type CopyHelper

type CopyHelper struct {
	*FileHelper
	// contains filtered or unexported fields
}

type Cp

type Cp struct {
	From,
	To string
	// contains filtered or unexported fields
}

func (*Cp) FromInfo

func (cp *Cp) FromInfo() (FileInfoInterface, error)

get cached FileInfo from *Cp.From.

func (*Cp) ToInfo

func (cp *Cp) ToInfo() (FileInfoInterface, error)

get cached FileInfo from *Cp.To.

type FakeStdFile

type FakeStdFile struct {
	// contains filtered or unexported fields
}

quickndirty stdin stdout fake for file

func NewFakeStdFile

func NewFakeStdFile(astdin io.Reader, astdout io.Writer) *FakeStdFile

provide stdin and stdout

func NewFakeStdFileCli

func NewFakeStdFileCli() *FakeStdFile

os.Stdin and os.Stdout

func (*FakeStdFile) Chdir

func (fake *FakeStdFile) Chdir() error

func (*FakeStdFile) Chmod

func (fake *FakeStdFile) Chmod(mode os.FileMode) error

func (*FakeStdFile) Chown

func (fake *FakeStdFile) Chown(uid, gid int) error

func (*FakeStdFile) Close

func (fake *FakeStdFile) Close() error

func (*FakeStdFile) Fd

func (fake *FakeStdFile) Fd() uintptr

func (*FakeStdFile) Name

func (fake *FakeStdFile) Name() string

func (*FakeStdFile) Read

func (fake *FakeStdFile) Read(b []byte) (n int, err error)

func (*FakeStdFile) ReadAt

func (fake *FakeStdFile) ReadAt(b []byte, off int64) (n int, err error)

func (*FakeStdFile) Readdir

func (fake *FakeStdFile) Readdir(n int) (fi []os.FileInfo, err error)

func (*FakeStdFile) Readdirnames

func (fake *FakeStdFile) Readdirnames(n int) (names []string, err error)

func (*FakeStdFile) Seek

func (fake *FakeStdFile) Seek(offset int64, whence int) (ret int64, err error)

func (*FakeStdFile) Stat

func (fake *FakeStdFile) Stat() (os.FileInfo, error)

func (*FakeStdFile) Sync

func (fake *FakeStdFile) Sync() error

func (*FakeStdFile) Truncate

func (fake *FakeStdFile) Truncate(size int64) error

func (*FakeStdFile) Write

func (fake *FakeStdFile) Write(b []byte) (n int, err error)

func (*FakeStdFile) WriteAt

func (fake *FakeStdFile) WriteAt(b []byte, off int64) (n int, err error)

func (*FakeStdFile) WriteString

func (fake *FakeStdFile) WriteString(s string) (n int, err error)

type FileHelper

type FileHelper struct {
	sync.Mutex

	// the more help, the better. lots and lots and lots of help needed!
	abstract.BaseHelper
	abstract.WorkerHelper
	// contains filtered or unexported fields
}

this struct is used to reduce function parameters.

func ReadAndWrite

func ReadAndWrite() *FileHelper

open read and write

func ReadOnly

func ReadOnly() *FileHelper

open for read only

func WriteOnly

func WriteOnly() *FileHelper

open for write only

func (*FileHelper) ApplyFileAdvice

func (helper *FileHelper) ApplyFileAdvice(to FileInterface)

set the file advices on file descriptors (fd). ignore all errors (just like coreutils does)

func (*FileHelper) Copy

func (helper *FileHelper) Copy(from interface{}) *FileHelper

copy several values from a helper

func (*FileHelper) DoNotTestForDirectory

func (helper *FileHelper) DoNotTestForDirectory() bool

get if test for directory

func (*FileHelper) FileInfo

func (helper *FileHelper) FileInfo(path string, lstat bool) (FileInfoInterface, error)

FIXME: correct lstat cache

func (*FileHelper) HasAppend

func (helper *FileHelper) HasAppend() bool

is append already active

func (*FileHelper) HasCreate

func (helper *FileHelper) HasCreate() bool

is append already active

func (*FileHelper) HasExclusive

func (helper *FileHelper) HasExclusive() bool

is exlusive already active

func (*FileHelper) HasSynchronous

func (helper *FileHelper) HasSynchronous() bool

is synchronized already active

func (*FileHelper) HasTruncate

func (helper *FileHelper) HasTruncate() bool

is truncate already active

func (*FileHelper) IsStdinToken

func (helper *FileHelper) IsStdinToken(what string) bool

return if string is equal to STDIN token (usualley "-")

func (*FileHelper) OpenFlags

func (helper *FileHelper) OpenFlags() int

return open flags

func (*FileHelper) Permissions

func (helper *FileHelper) Permissions() os.FileMode

return permissions

func (*FileHelper) ReadAndWrite

func (helper *FileHelper) ReadAndWrite() *FileHelper

sets to read and write and discards all other flags

func (*FileHelper) ReadOnly

func (helper *FileHelper) ReadOnly() *FileHelper

sets to read only and discards all other flags

func (*FileHelper) ReadSize

func (helper *FileHelper) ReadSize() int

get read size

func (*FileHelper) ResetFileAdvice

func (helper *FileHelper) ResetFileAdvice(to FileInterface)

resets all file advices on $to and in this helper

func (*FileHelper) SetPermissions

func (helper *FileHelper) SetPermissions(to os.FileMode) *FileHelper

set permissions

func (*FileHelper) SetReadSize

func (helper *FileHelper) SetReadSize(to int) *FileHelper

func (*FileHelper) SetStderr

func (helper *FileHelper) SetStderr(astderr io.Writer)

set STDERR to a io.Writer

func (*FileHelper) SetStdinStdout

func (helper *FileHelper) SetStdinStdout(astdinToken string, astdin io.Reader, astdout io.Writer)

set STDIN and STDOUT. only support supplying both

func (*FileHelper) ShouldFileAdviceDontNeed

func (helper *FileHelper) ShouldFileAdviceDontNeed() bool

thats a question to be answered

func (*FileHelper) ShouldFileAdviceNoReuse

func (helper *FileHelper) ShouldFileAdviceNoReuse() bool

thats a question to be answered

func (*FileHelper) ShouldFileAdviceReadRandom

func (helper *FileHelper) ShouldFileAdviceReadRandom() bool

thats a question to be answered

func (*FileHelper) ShouldFileAdviceReadSequential

func (helper *FileHelper) ShouldFileAdviceReadSequential() bool

thats a question to be answered

func (*FileHelper) ShouldFileAdviceWillNeed

func (helper *FileHelper) ShouldFileAdviceWillNeed() bool

thats a question to be answered

func (*FileHelper) Stderr

func (helper *FileHelper) Stderr() io.Writer

get STDERR to use

func (*FileHelper) Stdin

func (helper *FileHelper) Stdin() io.Reader

get STDIN to use

func (*FileHelper) Stdout

func (helper *FileHelper) Stdout() io.Writer

get STDOUT to use

func (*FileHelper) SupportCli

func (helper *FileHelper) SupportCli() bool

return if cli output is supported

func (*FileHelper) ToggleAppend

func (helper *FileHelper) ToggleAppend() *FileHelper

add append to flags

func (*FileHelper) ToggleCache

func (helper *FileHelper) ToggleCache()

@override toggle cache you should

func (*FileHelper) ToggleCreate

func (helper *FileHelper) ToggleCreate() *FileHelper

add create to flags

func (*FileHelper) ToggleDoNotTestForDirectory

func (helper *FileHelper) ToggleDoNotTestForDirectory() *FileHelper

please do not test for directory

func (*FileHelper) ToggleExclusive

func (helper *FileHelper) ToggleExclusive() *FileHelper

add exclusive to flags

func (*FileHelper) ToggleFileAdviceDontNeed

func (helper *FileHelper) ToggleFileAdviceDontNeed() *FileHelper

please do file advice DONT_NEED

func (*FileHelper) ToggleFileAdviceNoReuse

func (helper *FileHelper) ToggleFileAdviceNoReuse() *FileHelper

please do file advice NO_REUSE

func (*FileHelper) ToggleFileAdviceReadRandom

func (helper *FileHelper) ToggleFileAdviceReadRandom() *FileHelper

please do file advice RANDOM_READ

func (*FileHelper) ToggleFileAdviceReadSequential

func (helper *FileHelper) ToggleFileAdviceReadSequential() *FileHelper

please do file advice SEQUENTIAL_READ

func (*FileHelper) ToggleFileAdviceWillNeed

func (helper *FileHelper) ToggleFileAdviceWillNeed() *FileHelper

please do file advice WILLNEED

func (*FileHelper) ToggleSynchronous

func (helper *FileHelper) ToggleSynchronous() *FileHelper

add sync to flags

func (*FileHelper) ToggleTruncate

func (helper *FileHelper) ToggleTruncate() *FileHelper

add trunc to flags

func (*FileHelper) WriteOnly

func (helper *FileHelper) WriteOnly() *FileHelper

sets to write only and discards all other flags

type FileInfo

type FileInfo struct {
	os.FileInfo
	// contains filtered or unexported fields
}

wrapper around os.FileInfo for easier access to os.FileInfo.Sys()

func NewFileInfo

func NewFileInfo(apath string) (info *FileInfo, e error)

fresh and shiny

func NewOsFileInfo

func NewOsFileInfo(apath string, from os.FileInfo) (info *FileInfo)

fresh and shiny

func (*FileInfo) BlockSize

func (info *FileInfo) BlockSize() (blockSize int64)

blocksize for file system I/O

func (*FileInfo) Blocks

func (info *FileInfo) Blocks() (blocks int64)

number of 512B blocks allocated

func (*FileInfo) DeviceId

func (info *FileInfo) DeviceId() (deviceId uint64)

ID of device containing file

func (*FileInfo) Group

func (info *FileInfo) Group() (group string)

group name of owner

func (*FileInfo) GroupId

func (info *FileInfo) GroupId() (groupId int)

group id of owner

func (info *FileInfo) HardLinks() (hardLinks uint64)

number of hard links

func (*FileInfo) Inode

func (info *FileInfo) Inode() (inode uint64)

inode number

func (*FileInfo) IsExecuteable

func (info *FileInfo) IsExecuteable() bool

is file not a directory and exeCUTEable

func (*FileInfo) Owner

func (info *FileInfo) Owner() (owner string)

user name of owner

func (*FileInfo) Path

func (info *FileInfo) Path() string

the given path unaltered

func (*FileInfo) Same

func (info *FileInfo) Same(compare os.FileInfo) bool

are two files the same

func (*FileInfo) UserId

func (info *FileInfo) UserId() (userId int)

user ID of owner

func (*FileInfo) Username

func (info *FileInfo) Username() (user string)

complete username of owner (eg: Username = gdm, Name = Gnome Display Manager)

type FileInfoInterface

type FileInfoInterface interface {
	os.FileInfo

	// the originally given path
	Path() string

	// is same file
	Same(compare os.FileInfo) bool

	// user info
	Owner() string
	Username() string
	UserId() int

	// group info
	Group() string
	GroupId() int

	// is executeable
	IsExecuteable() bool
}

type FileInterface

type FileInterface interface {
	io.Reader
	io.ReaderAt
	io.Writer
	io.WriterAt
	io.Seeker

	Chdir() error
	Chmod(mode os.FileMode) error
	Chown(uid, gid int) error
	Fd() uintptr
	Name() string
	Readdir(n int) (fi []os.FileInfo, err error)
	Readdirnames(n int) (names []string, err error)
	Stat() (os.FileInfo, error)
	Sync() error
	Truncate(size int64) error
	WriteString(s string) (n int, err error)
	Close() error
}

func Create

func Create(fileHelper *FileHelper, path string) (handler FileInterface, e error)

create a file. if you need it truncated, use FileHelper.ToggleTruncate().

func Open

func Open(fileHelper *FileHelper, path string) (handler FileInterface, e error)

open a file with FileHelper. if its a dir, return nil. it's your responsibility to close the file...

func OpenDirectory

func OpenDirectory(path string) (handler FileInterface, e error)

open a directory

func Temporary

func Temporary(fileHelper *FileHelper, prefix string) (handler FileInterface, e error)

create a temporary file. only makes sure the file is unique. file will not be removed. prefix should be your program name.

type NamedBuffer

type NamedBuffer struct {
	*io.PipeReader
	*io.PipeWriter
	// contains filtered or unexported fields
}

func NewNamedBuffer

func NewNamedBuffer(aname string) (namedBuffer *NamedBuffer)

shiny and fresh

func (*NamedBuffer) Cancel

func (buffer *NamedBuffer) Cancel()

cancel reading; clear buffers; blocks till cancel is done

func (*NamedBuffer) Close

func (buffer *NamedBuffer) Close()

func (*NamedBuffer) IsCanceled

func (buffer *NamedBuffer) IsCanceled() bool

func (*NamedBuffer) Key

func (buffer *NamedBuffer) Key() uint

if the file was in a sequence, eg a list, returns the key

func (*NamedBuffer) Name

func (buffer *NamedBuffer) Name() string

returns the name of the file

func (*NamedBuffer) SetKey

func (buffer *NamedBuffer) SetKey(to uint)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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