Newer
Older
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
)
var (
backupPath = ""
dbPath = ""
)
// TODO: Symbolic link evelution for this package not supported in windows .symlink file
// SetBackPath set backup folder path
func SetBackPath(folderPath, dbFolderPath string) error {
if folderPath == "" || dbFolderPath == "" {
return errors.New("Backup folder path and DB Path must not be empty")
}
bFolder, bFile := filepath.Split(folderPath)
if bFile != "" {
return errors.New("Backup Path must be folder or you forget to provide slash at the end")
}
dbFolder, dbFile := filepath.Split(dbFolderPath)
if dbFile != "" {
return errors.New("DB Path must be folder or you forget to provide slash at the end")
}
backupPath = bFolder
dbPath = dbFolder
return nil
}
// createFileBackup createfile back in backup folder
func createFileBackup(filePath string) error {
if backupPath == "" || dbPath == "" {
loggermdl.LogError("Backup folder path not set")
return errors.New("Backup folder path not set")
}
backupFilePath := backupPath + strings.TrimPrefix(filePath, dbPath)
_, err := CopyFile(filePath, backupFilePath, true)
if errormdl.CheckErr(err) != nil {
return errormdl.CheckErr(err)
}
return nil
}
//FileHelperServiceObject FileHelperServiceObject must be created while calling FileSearh function
type FileHelperServiceObject struct {
searchResult []string
searchFileName string
}
// ReadFile reads contents from provided file path and retry when timeout occure
path, linkErr := os.Readlink(filePath)
if errormdl.CheckErr1(linkErr) == nil {
filePath = path
}
ba, err := ioutil.ReadFile(filePath)
if errormdl.CheckErr(err) != nil {
if errormdl.CheckErr(err).Error() == "i/o timeout" {
return ioutil.ReadFile(filePath)
}
return nil, errormdl.CheckErr(err)
//createFile creates a new file
func createFile(filePath string) (*os.File, error) {
// function is usable for file module only. Required for zip module
return os.Create(filePath)
}
func createRecursiveDirectoryForFile(filePath string) error {
if errormdl.CheckErr(createError) != nil {
loggermdl.LogError(createError)
return errormdl.CheckErr(createError)
}
return nil
}
// WriteFile writes provided bytes to file
func WriteFile(filePath string, data []byte, makeDir bool, createBackup bool) error {
path, linkErr := os.Readlink(filePath)
if errormdl.CheckErr1(linkErr) == nil {
filePath = path
}
if makeDir {
createError := createRecursiveDirectoryForFile(filePath)
if errormdl.CheckErr(createError) != nil {
loggermdl.LogError(createError)
return errormdl.CheckErr(createError)
if createBackup {
backupErr := createFileBackup(filePath)
if backupErr != nil {
loggermdl.LogError(backupErr)
}
}
//AppendFile appends provided data/text to file
func AppendFile(filename string, text string) (int, error) {
path, linkErr := os.Readlink(filename)
if errormdl.CheckErr1(linkErr) == nil {
filename = path
}
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if errormdl.CheckErr(err) != nil {
loggermdl.LogError(err)
return 0, errormdl.CheckErr(err)
}
defer f.Close()
return f.WriteString(text)
}
//DeleteFile deletes provided file path
func DeleteFile(filePath string) error {
path, linkErr := os.Readlink(filePath)
if errormdl.CheckErr1(linkErr) == nil {
filePath = path
}
err := os.Remove(filePath)
if errormdl.CheckErr(err) != nil {
loggermdl.LogError(err)
return errormdl.CheckErr(err)
}
return nil
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
}
// RenameFile renames/moves file from old path to new path
func RenameFile(oldFilePath, newFilePath string) error {
return os.Rename(oldFilePath, newFilePath)
}
// CreateDirectory creates directory using provided path
func CreateDirectory(directoryPath string) error {
return os.Mkdir(directoryPath, os.ModePerm)
}
// CreateDirectoryRecursive creates directory recursively using provided path
func CreateDirectoryRecursive(directoryPath string) error {
return os.MkdirAll(directoryPath, os.ModePerm)
}
// DeleteDirectory creates directory using provided path
func DeleteDirectory(directoryPath string) error {
return os.RemoveAll(directoryPath)
}
//ListDirectory returns list of all available components of directory
func ListDirectory(directoryPath string) ([]os.FileInfo, error) {
return ioutil.ReadDir(directoryPath)
}
//MoveDirectory MoveDirectory
func MoveDirectory(source, destination string) error {
return os.Rename(source, destination)
}
//MoveFile MoveFile
func MoveFile(source, destination string) error {
return os.Rename(source, destination)
}
// MoveFileToOtherHost MoveFileToOtherHost
func MoveFileToOtherHost(source, destination string) error {
// Copy source file to destination
_, copyError := CopyFile(source, destination, true)
if errormdl.CheckErr(copyError) != nil {
loggermdl.LogError(copyError)
return errormdl.CheckErr(copyError)
}
// Delete source file
deleteError := DeleteFile(source)
if errormdl.CheckErr1(deleteError) != nil {
loggermdl.LogError(deleteError)
return errormdl.CheckErr1(deleteError)
}
return nil
}
//CopyFile CopyFile
func CopyFile(source, destination string, makeDir bool) (int64, error) {
// return os.Rename(source, destination)
path, linkErr := os.Readlink(source)
if errormdl.CheckErr1(linkErr) == nil {
source = path
}
if errormdl.CheckErr(openError) != nil {
loggermdl.LogError(openError)
return 0, errormdl.CheckErr(openError)
}
if makeDir {
createError := createRecursiveDirectoryForFile(destination)
if errormdl.CheckErr1(createError) != nil {
loggermdl.LogError(createError)
return 0, errormdl.CheckErr1(createError)
}
}
out, createError := os.Create(destination)
defer out.Close()
if errormdl.CheckErr2(createError) != nil {
loggermdl.LogError(createError)
return 0, errormdl.CheckErr2(createError)
if errormdl.CheckErr3(copyError) != nil {
loggermdl.LogError(copyError)
return 0, errormdl.CheckErr3(copyError)
}
return n, nil
}
//CopyDir makes copy of source directory to the destination directory
func CopyDir(source string, dest string) (err error) {
path, linkErr := os.Readlink(source)
if errormdl.CheckErr2(linkErr) == nil {
source = path
}
// get properties of source dir
sourceinfo, err := os.Stat(source)
if errormdl.CheckErr(err) != nil {
loggermdl.LogError(err)
return errormdl.CheckErr(err)
}
// create dest dir
err = os.MkdirAll(dest, sourceinfo.Mode())
if errormdl.CheckErr1(err) != nil {
loggermdl.LogError(err)
return errormdl.CheckErr1(err)
objects, err := directory.Readdir(-1)
for _, obj := range objects {
sourcefilepointer := source + "/" + obj.Name()
destinationfilepointer := dest + "/" + obj.Name()
if obj.IsDir() {
// create sub-directories - recursively
err = CopyDir(sourcefilepointer, destinationfilepointer)
if errormdl.CheckErr2(err) != nil {
loggermdl.LogError(err)
}
} else {
// perform copy
_, err = CopyFile(sourcefilepointer, destinationfilepointer, true)
if errormdl.CheckErr2(err) != nil {
loggermdl.LogError(err)
}
}
}
return
}
//ReplaceFile ReplaceFile
func ReplaceFile(data []byte, destination string, createBackup bool) error {
path, linkErr := os.Readlink(destination)
if errormdl.CheckErr(linkErr) == nil {
destination = path
}
return WriteFile(destination, data, false, createBackup)
}
//TruncateFile TruncateFile
func TruncateFile(path string, size int64) error {
tmp, linkErr := os.Readlink(path)
if errormdl.CheckErr(linkErr) == nil {
path = tmp
}
return os.Truncate(path, size)
}
//SeekFile SeekFile
// func SeekFile(path string, offset int64) error {
// file, err := os.OpenFile(path, os.O_RDWR, 0600)
// if err != nil {
// return err
// }
// defer file.Close()
// _, err = file.Seek(offset, 0)
// return err
// }
//FileInfo FileInfo
func FileInfo(path string) (os.FileInfo, error) {
tmp, linkErr := os.Readlink(path)
if errormdl.CheckErr(linkErr) == nil {
path = tmp
}
return os.Stat(path)
}
//FileSearch FileSearch
func (fileHelperServiceObject *FileHelperServiceObject) FileSearch(fileName, path string) ([]string, error) {
fileHelperServiceObject.searchResult = []string{}
fileHelperServiceObject.searchFileName = fileName
searchDirectory, err := os.Open(path)
if errormdl.CheckErr(err) != nil {
loggermdl.LogError(err)
return fileHelperServiceObject.searchResult, errormdl.CheckErr(err)
}
defer searchDirectory.Close()
testFileInfo, _ := searchDirectory.Stat()
if !testFileInfo.IsDir() {
return fileHelperServiceObject.searchResult, err
}
err = filepath.Walk(path, fileHelperServiceObject.findFile)
if errormdl.CheckErr1(err) != nil {
loggermdl.LogError(err)
return fileHelperServiceObject.searchResult, errormdl.CheckErr1(err)
}
return fileHelperServiceObject.searchResult, nil
}
func (fileHelperServiceObject *FileHelperServiceObject) findFile(path string, fileInfo os.FileInfo, err error) error {
if errormdl.CheckErr(err) != nil {
loggermdl.LogError(err)
return errormdl.CheckErr(err)
}
// get absolute path of the folder that we are searching
absolute, err := filepath.Abs(path)
if errormdl.CheckErr1(err) != nil {
loggermdl.LogError(err)
return errormdl.CheckErr1(err)
}
if fileInfo.IsDir() {
testDir, err := os.Open(absolute)
if errormdl.CheckErr2(err) != nil {
loggermdl.LogError(err)
return errormdl.CheckErr2(err)
}
testDir.Close()
return nil
}
matched, err := filepath.Match(fileHelperServiceObject.searchFileName, fileInfo.Name())
if errormdl.CheckErr3(err) != nil {
loggermdl.LogError(err)
return errormdl.CheckErr3(err)
}
if matched {
add := absolute
fileHelperServiceObject.searchResult = append(fileHelperServiceObject.searchResult, add)
}
return nil
}
//FileAvailabilityCheck checks whether file is available at given location
func FileAvailabilityCheck(filePath string) bool {
tmp, linkErr := os.Readlink(filePath)
if errormdl.CheckErr(linkErr) == nil {
filePath = tmp
}
if fileInfo == nil && errormdl.CheckErr(err) != nil {
loggermdl.LogError(err)
return false
}
return true
}
// CleanPath clean path
func CleanPath(path string) string {
path = strings.Replace(path, "\n", "\\n", -1)
path = strings.Replace(path, "\t", "\\t", -1)
path = strings.Replace(path, "\r", "\\r", -1)
path = strings.Replace(path, "\b", "\\b", -1)
path = strings.Replace(path, "\a", "\\b", -1)
path = strings.Replace(path, "\v", "\\b", -1)
//path = strings.Replace(path, '\', '/', -1)
normalizedPath := filepath.Clean(path)
normalizedPath = filepath.ToSlash(normalizedPath)
if strings.HasSuffix(path, string(filepath.Separator)) || strings.HasSuffix(path, "/") {
normalizedPath = normalizedPath + "/"
}
return normalizedPath
}