implement backup system

This commit is contained in:
HikariKnight 2023-04-10 18:33:48 +02:00
parent 7eb7e2fa1c
commit 300ca653cc
6 changed files with 88 additions and 6 deletions

View file

@ -4,13 +4,17 @@ import (
"bufio"
"errors"
"fmt"
"io"
"os"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
)
// This just implements repetetive tasks I have to do with files
/*
* This just implements repetetive tasks I have to do with files
*/
// Creates a file and appends the content to the file (ending newline must be supplied with content string)
func AppendContent(content string, fileName string) {
// Open the file
f, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm)
@ -22,6 +26,7 @@ func AppendContent(content string, fileName string) {
errorcheck.ErrorCheck(err, fmt.Sprintf("Error writing to %s", fileName))
}
// Reads the file and returns a stringlist with each line
func ReadLines(fileName string) []string {
content, err := os.Open(fileName)
errorcheck.ErrorCheck(err, fmt.Sprintf("Error reading file %s", fileName))
@ -41,6 +46,7 @@ func ReadLines(fileName string) []string {
}
// Reads a file and returns all the content as a string
func ReadFile(fileName string) string {
// Read the whole file
content, err := os.ReadFile(fileName)
@ -51,6 +57,7 @@ func ReadFile(fileName string) string {
}
// Checks if a file exists and returns a bool
func FileExist(fileName string) bool {
var exist bool
@ -66,3 +73,27 @@ func FileExist(fileName string) bool {
// Return if the file exists
return exist
}
// Copies a FILE from source to dest
func FileCopy(sourceFile, destFile string) {
// Get the file info
filestat, err := os.Stat(sourceFile)
errorcheck.ErrorCheck(err, "Error getting fileinfo of: %s", sourceFile)
// If the file is a regular file
if filestat.Mode().IsRegular() {
// Open the source file for reading
source, err := os.Open(sourceFile)
errorcheck.ErrorCheck(err, "Error opening %s for copying", sourceFile)
defer source.Close()
// Create the destination file
dest, err := os.Create(destFile)
errorcheck.ErrorCheck(err, "Error creating %s", destFile)
defer dest.Close()
// Copy the contents of source to dest using io
_, err = io.Copy(dest, source)
errorcheck.ErrorCheck(err, "Failed to copy \"%s\" to \"%s\"", sourceFile, destFile)
}
}