fileOp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2021 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChangeFileOwner

func ChangeFileOwner(filename string) (err error)

ChangeFileOwner: set the owner of the file to real user instead of root. Obtaining root rights differs when sudo or pkexec is used, this function checks which command was used and acts to perform the job correctly.

func GetFileOwner

func GetFileOwner(filename string) (uid, gid int, ok bool, err error)

GetFileOwner: Work only for linux users.

func GetRootCurrRealUser

func GetRootCurrRealUser() (currentUser, realUser *user.User, root bool, err error)

GetRootCurrRealUser: Retrieve information about root state and current and real user. Handle different behavior between sudo and pkexec commands.

Types

type FileDetails

type FileDetails struct {
	BaseDir,
	FileRel,
	LinkTarget,
	FilenameFull,
	Error string

	Mode os.FileMode

	IsDir,
	IsSymlink bool

	Size int64
	// Used in callback function for FilesRaw method, that permit to eventually change
	// filename on condition (target slink instead on slink...)
	FileAsRaw string
	// contains filtered or unexported fields
}

FileDetails: Contains all the information about a scanned file. More convenient way than the native 'os.FileInfo' structure, all information is directly accessible.

type FilesOpStruct

type FilesOpStruct struct {
	Masks []string

	Files []FileDetails

	ExcludeMasks,
	AppendToFiles,
	Backup,
	IncludeDir,
	RealUserOwner,

	SkipDup bool

	SrceDir string

	Owner *OwnerStruct
	Perms *PermsStruct

	// ReadDir function specific options
	ReadDirDepth int // default 1
	// contains filtered or unexported fields
}

FilesOpStruct: Contains all the information of the scanned files (directories / files). This structure also contains many methods for handling files, filtering, directories, perms, owner options relating to the linux filesystem.

func FilesOpStructNew

func FilesOpStructNew() (fos *FilesOpStruct, err error)

FilesOpStructNew: Create a new 'FilesOpStruct' that hold some useful file/dir functions. The goal of it is to facilitate file interactions, can create copy, files/dir perserving owner/permissions. Files can be stored with all information needed to recreate them.

func (*FilesOpStruct) BackupFilesDetails

func (fos *FilesOpStruct) BackupFilesDetails()

backupFilesDetails: backup previous set of 'Files', usefull when append is used

func (*FilesOpStruct) CopyFile

func (fos *FilesOpStruct) CopyFile(src, dest string) (err error)

Copy: file/dir tree preserving permissions & owner (if requested).

func (*FilesOpStruct) FileDetailsNew

func (fos *FilesOpStruct) FileDetailsNew(path string, stat os.FileInfo) (fd *FileDetails, err error)

FileDetailsNew: Make a new 'FileDetails' Structure.

func (*FilesOpStruct) FilesGetAsRaw

func (fos *FilesOpStruct) FilesGetAsRaw(callbackOnAdd ...func(file *FileDetails) bool) []string

FilesGetAsRaw: Get filenames in simpliest way as []string. Were provided, 'callbackOnAdd' function allow to filtering some file details before adding or not this one to results, in this case, 'FileDetails.FileAsRaw' must contain desired outpout that will be added to the returned strings slice.

func (*FilesOpStruct) GetFilesDetails

func (fos *FilesOpStruct) GetFilesDetails(root string) (err error)

GetFilesDetails: Retrieve files details starting at "root", they will be stored as 'FileDetails' structure in fos.Files slice.

func (*FilesOpStruct) GetFilesDetailsOld

func (fos *FilesOpStruct) GetFilesDetailsOld(root string) (err error)

TODO Remove this one GetFilesDetails: Retrieve files details starting at "root", they will be stored as 'FileDetails' structure in fos.Files slice.

func (*FilesOpStruct) GetFilesDetailsReadDir

func (fos *FilesOpStruct) GetFilesDetailsReadDir(root string) (err error)

GetFilesDetailsReadDir: Retrieve files details starting at "root", and limit depth scanning directory using 'ReadDirDepth' struct variable, details will be stored as 'FileDetails' structure in fos.Files slice.

func (*FilesOpStruct) GetTotalSize

func (fos *FilesOpStruct) GetTotalSize() uint64

GetTotalSize: retrieve total files size currently stored.

func (*FilesOpStruct) MkdirAll

func (fos *FilesOpStruct) MkdirAll(path string, perm os.FileMode) error

MkdirAll: This is a modified version of original golang function, this one set the defined (specific) owner for each created directories.

func (*FilesOpStruct) Read

func (fos *FilesOpStruct) Read(filename string) (err error)

Read FilesOpStruct from file

func (*FilesOpStruct) ResetAll

func (fos *FilesOpStruct) ResetAll() error

init: Initialize permissions and current/real user information.

func (*FilesOpStruct) SetOwner

func (fos *FilesOpStruct) SetOwner(path string, callbeforefunc ...interface{}) (err error)

SetOwner: Set filename user owner, uid and gid are defined in main structure, fos.RealUserOwner must be enabled. The 'callbeforefunc' permit to run an operation just before chown command (like saving png, jpg ...).

func (*FilesOpStruct) SortFiles

func (fos *FilesOpStruct) SortFiles(ascend ...bool)

Sort: Descending as default behavior

func (*FilesOpStruct) UndoLastFilesChanges

func (fos *FilesOpStruct) UndoLastFilesChanges()

UndoLastFilesChanges: restore previous 'Files' state, usefull when append is used

func (*FilesOpStruct) Write

func (fos *FilesOpStruct) Write(filename string) (err error)

Write FilesOpStruct to file

func (*FilesOpStruct) WriteFile

func (fos *FilesOpStruct) WriteFile(filename string, data []byte, perm os.FileMode) (err error)

WriteFile: can set owner (if requested).

type OwnerStruct

type OwnerStruct struct {
	Uid,
	Gid int

	Root bool

	RealUser,
	CurrentUser *user.User
}

func OwnerStructNew

func OwnerStructNew() (owns *OwnerStruct, err error)

OwnerStructNew: This structure gathers file permissions, the files owner change methods and Real, Current user information.

func (*OwnerStruct) ChangeFileOwner

func (owns *OwnerStruct) ChangeFileOwner(filename string) (err error)

ChangeFileOwner: set the owner of the file to user defined in Uid and Gid. If not modified, values target current realuser. Obtaining root rights differs when sudo or pkexec is used, this function checks which command was used and acts to perform the job correctly.

func (*OwnerStruct) GetFileOwner

func (owns *OwnerStruct) GetFileOwner(filename string) (uid, gid int, ok bool, err error)

GetFileOwner: Work only for linux users.

type PermsStruct

type PermsStruct struct {
	USER_R,
	USER_W,
	USER_X,
	USER_RW,
	USER_RX,
	USER_RWX,

	GROUP_R,
	GROUP_W,
	GROUP_X,
	GROUP_RW,
	GROUP_RX,
	GROUP_RWX,

	OTH_R,
	OTH_W,
	OTH_X,
	OTH_RW,
	OTH_RX,
	OTH_RWX,

	ALL_R,
	ALL_W,
	ALL_X,
	ALL_RW,
	ALL_RX,
	ALL_RWX,

	File,
	FileX,
	Dir os.FileMode
	// contains filtered or unexported fields
}

Structure that hold file permissions

func PermsStructNew

func PermsStructNew() (osp *PermsStruct)

func (*PermsStruct) DispPerms

func (osp *PermsStruct) DispPerms(perm os.FileMode)

DispPerms: display right. i.e: DispPerms(OS.GROUP_RW | OS.USER_RW | OS.ALL_R) -> 664

func (*PermsStruct) GetFileMode

func (osp *PermsStruct) GetFileMode(perm os.FileMode) os.FileMode

GetFileMode: add os.ModePerm to os.FileMode, that permit to use it directly in read or write golang functions.

Jump to

Keyboard shortcuts

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