operations

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Provide a helper for directory operations.

Provide helper functions related to file.

Provide helper functions.

Index

Constants

View Source
const (
	OneKiB = 1024
	OneMiB = OneKiB * OneKiB
	// ChunkSizeForContentComparison is currently set to 1 MiB.
	ChunkSizeForContentComparison int = OneMiB
)
View Source
const DirPermission_0755 = 0755
View Source
const FilePermission_0400 = 0400
View Source
const FilePermission_0600 = 0600
View Source
const FilePermission_0777 = 0777
View Source
const MiB = 1024 * 1024

Variables

This section is empty.

Functions

func AreFilesIdentical

func AreFilesIdentical(filepath1, filepath2 string) (bool, error)

Finds if two local files have identical content (equivalnt to binary diff). Needs (a) both files to exist, (b)read permission on both the files, (c) both inputs to be proper files, i.e. directories not supported. Compares file names first. If different, compares sizes next. If sizes match, then compares the contents of both the files. Returns true if no error and files match. Returns false if files don't match (captures reason for mismatch in err) or if any other error.

func ClearCacheControlOnGcsObject

func ClearCacheControlOnGcsObject(gcsObjPath string) error

Clears cache-control attributes on given GCS object (with path without 'gs://'). Fails if the file doesn't exist or permission to modify object's metadata is not available. Uses 'gsutil setmeta -h "Cache-Control:" gs://<path>' Preferred approach is 'gcloud storage objects update gs://gs://gcsObjPath --cache-control=' ' ' but it doesn't work on kokoro VM.

func CloseFile

func CloseFile(file *os.File)

func CloseFileShouldNotThrowError

func CloseFileShouldNotThrowError(file *os.File, t *testing.T)

func CopyDir

func CopyDir(srcDirPath string, destDirPath string) (err error)

func CopyDirWithRootPermission

func CopyDirWithRootPermission(srcDirPath string, destDirPath string) (err error)

func CopyFile

func CopyFile(srcFileName, newFileName string) (err error)

func CopyFileAllowOverwrite

func CopyFileAllowOverwrite(srcFileName, newFileName string) (err error)

func CopyFileInBucket

func CopyFileInBucket(srcfilePath, destFilePath, bucket string, t *testing.T)

func CreateDirectory

func CreateDirectory(dirPath string, t *testing.T)

func CreateDirectoryWithNFiles

func CreateDirectoryWithNFiles(numberOfFiles int, dirPath string, prefix string, t *testing.T)

func CreateFile

func CreateFile(filePath string, filePerms os.FileMode, t *testing.T) (f *os.File)

func CreateFileOfSize

func CreateFileOfSize(fileSize int64, filePath string, t *testing.T)

CreateFileOfSize creates a file of given size with random data.

func CreateFileWithContent

func CreateFileWithContent(filePath string, filePerms os.FileMode,
	content string, t *testing.T)

func CreateManagedFoldersInBucket

func CreateManagedFoldersInBucket(managedFolderPath, bucket string, t *testing.T)
func CreateSymLink(filePath, symlink string, t *testing.T)

func DeleteGcsObject

func DeleteGcsObject(gcsObjPath string) error

Deletes a given GCS object (with path without 'gs://'). Fails if the object doesn't exist or permission to delete object is not available. Uses 'gsutil rm gs://gcsObjPath' Alternative 'gcloud storage rm gs://gcsObjPath' but it doesn't work on kokoro VM.

func DeleteManagedFoldersInBucket

func DeleteManagedFoldersInBucket(managedFolderPath, bucket string, t *testing.T)

func DirSizeMiB

func DirSizeMiB(dirPath string) (dirSizeMB int64, err error)

func DownloadGcsObject

func DownloadGcsObject(gcsObjPath, localPath string) error

Downloads given GCS object (with path without 'gs://') to localPath. Fails if the object doesn't exist or permission to read object is not available. Uses 'gsutil cp gs://gcsObjPath localPath' Alternative 'gcloud storage cp gs://gcsObjPath localPath' but it doesn't work on kokoro VM.

func ExecuteGcloudCommandf

func ExecuteGcloudCommandf(format string, args ...any) ([]byte, error)

Executes any given gcloud command with given args.

func ExecuteGsutilCommandf

func ExecuteGsutilCommandf(format string, args ...any) ([]byte, error)

Executes any given gsutil command with given args.

func GenerateRandomData

func GenerateRandomData(sizeInBytes int64) ([]byte, error)

GenerateRandomData generates random data that can be used to write to a file.

func GetGcsObjectSize

func GetGcsObjectSize(gcsObjPath string) (int, error)

Returns size of a give GCS object with path (without 'gs://'). Fails if the object doesn't exist or permission to read object's metadata is not available. Uses 'gsutil du -s gs://gcsObjPath'. Alternative 'gcloud storage du -s gs://gcsObjPath', but it doesn't work on kokoro VM.

func MoveDir

func MoveDir(srcDirPath string, destDirPath string) (err error)

func MoveFile

func MoveFile(srcFilePath string, destDirPath string) (err error)

func ReadChunkFromFile

func ReadChunkFromFile(filePath string, chunkSize int64, offset int64, flag int) (chunk []byte, err error)

func ReadDirectory

func ReadDirectory(dirPath string, t *testing.T) (entries []os.DirEntry)

func ReadFile

func ReadFile(filePath string) (content []byte, err error)

func ReadFileSequentially

func ReadFileSequentially(filePath string, chunkSize int64) (content []byte, err error)

func RemoveDir

func RemoveDir(dirPath string)

func RemoveFile

func RemoveFile(filePath string)

func RenameDir

func RenameDir(dirName string, newDirName string) (err error)

func RenameFile

func RenameFile(fileName string, newFileName string) (err error)

func StatFile

func StatFile(file string) (*fs.FileInfo, error)

Returns the stats of a file. Fails if the passed input is a directory.

func SyncFile

func SyncFile(fh *os.File, t *testing.T)

func UploadGcsObject

func UploadGcsObject(localPath, gcsObjPath string, uploadGzipEncoded bool) error

Uploads given local file to GCS object (with path without 'gs://'). Fails if the file doesn't exist or permission to write to object/bucket is not available. Uses 'gsutil cp localPath gs://gcsObjPath' Alternative 'gcloud storage cp localPath gs://gcsObjPath' but it doesn't work on kokoro VM.

func ValidateNoFileOrDirError

func ValidateNoFileOrDirError(path string, t *testing.T)

func VerifyCountOfDirectoryEntries

func VerifyCountOfDirectoryEntries(expected, got int, t *testing.T)

func VerifyDirectoryEntry

func VerifyDirectoryEntry(entry os.DirEntry, dirName string, t *testing.T)

func VerifyFileEntry

func VerifyFileEntry(entry os.DirEntry, fileName string, size int64, t *testing.T)

func VerifyReadFile

func VerifyReadFile(filePath, expectedContent string, t *testing.T)
func VerifyReadLink(expectedTarget, symlinkName string, t *testing.T)

func VerifyStatFile

func VerifyStatFile(filePath string, fileSize int64, filePerms os.FileMode, t *testing.T)

func WriteAt

func WriteAt(content string, offset int64, fh *os.File, t *testing.T)

func WriteChunkOfRandomBytesToFile

func WriteChunkOfRandomBytesToFile(file *os.File, chunkSize int, offset int64) error

Write data of chunkSize in file at given offset.

func WriteFile

func WriteFile(fileName string, content string) (err error)

func WriteFileInAppendMode

func WriteFileInAppendMode(fileName string, content string) (err error)

func WriteFileSequentially

func WriteFileSequentially(filePath string, fileSize int64, chunkSize int64) (err error)

func WriteWithoutClose

func WriteWithoutClose(fh *os.File, content string, t *testing.T)

Types

This section is empty.

Jump to

Keyboard shortcuts

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