commit 992706d9b77cdcac115d9a9482786885a3bf523b Author: Tovi Jaeschke-Rogers Date: Thu Jul 8 05:59:53 2021 +0930 Add Tar archive functionality diff --git a/Archive/Archive.go b/Archive/Archive.go new file mode 100644 index 0000000..8d0e0b3 --- /dev/null +++ b/Archive/Archive.go @@ -0,0 +1,129 @@ +package Archive + +import ( + "archive/tar" + "compress/gzip" + "io" + "os" + "path/filepath" + "strings" +) + +func Gzip(source, target string) error { + var ( + reader, writer *os.File + archiver *gzip.Writer + filename string + e error + ) + + reader, e = os.Open(source) + if e != nil { + return e + } + + //filename = filepath.Base(source) + //target = filepath.Join(target, fmt.Sprintf("%s.gz", filename)) + + writer, e = os.Create(target) + if e != nil { + return e + } + + defer writer.Close() + + archiver = gzip.NewWriter(writer) + archiver.Name = filename + defer archiver.Close() + + _, e = io.Copy(archiver, reader) + return e +} + +func Tar(source, target string) error { + var ( + tarfile, file *os.File + tarball *tar.Writer + info os.FileInfo + header *tar.Header + e error + ) + + tarfile, e = os.Create(target) + if e != nil { + return e + } + defer tarfile.Close() + + tarball = tar.NewWriter(tarfile) + defer tarball.Close() + + info, e = os.Stat(source) + if e != nil { + return e + } + + var baseDir string + if info.IsDir() { + baseDir = filepath.Base(source) + } + + return filepath.Walk(source, + func(path string, info os.FileInfo, e error) error { + if e != nil { + return e + } + + header, e = tar.FileInfoHeader(info, info.Name()) + if e != nil { + return e + } + + if baseDir != "" { + header.Name = filepath.Join( + baseDir, + strings.TrimPrefix( + strings.Join(strings.Split(path, "/")[1:], "/"), + source, + ), + ) + } + + e = tarball.WriteHeader(header) + if e != nil { + return e + } + + if info.IsDir() { + return nil + } + + file, e = os.Open(path) + if e != nil { + return e + } + defer file.Close() + + _, e = io.Copy(tarball, file) + return e + }) +} + +func TarGzip(source, target string) error { + var ( + tarPath string = strings.ReplaceAll(target, ".gz", "") + e error + ) + + e = Tar(source, tarPath) + if e != nil { + return e + } + + e = Gzip(tarPath, target) + if e != nil { + return e + } + + return os.Remove(tarPath) +} diff --git a/Archive/Unarchive.go b/Archive/Unarchive.go new file mode 100644 index 0000000..eaa2530 --- /dev/null +++ b/Archive/Unarchive.go @@ -0,0 +1,111 @@ +package Archive + +import ( + "archive/tar" + "compress/gzip" + "io" + "io/fs" + "os" + "path/filepath" + "strings" +) + +func UnGzip(source, target string) error { + var ( + reader, writer *os.File + archive *gzip.Reader + e error + ) + + reader, e = os.Open(source) + if e != nil { + return e + } + defer reader.Close() + + archive, e = gzip.NewReader(reader) + if e != nil { + return e + } + defer archive.Close() + + target = filepath.Join(target, archive.Name) + + writer, e = os.Create(target) + if e != nil { + return e + } + defer writer.Close() + + _, e = io.Copy(writer, archive) + return e +} + +func Untar(tarball, target string) error { + var ( + reader *os.File + tarReader *tar.Reader + header *tar.Header + info fs.FileInfo + file *os.File + path string + e error + ) + + reader, e = os.Open(tarball) + if e != nil { + return e + } + + defer reader.Close() + tarReader = tar.NewReader(reader) + + for { + header, e = tarReader.Next() + if e == io.EOF { + break + } + if e != nil { + return e + } + + path = "/" + strings.Join(strings.Split(header.Name, "/")[1:], "/") + + info = header.FileInfo() + if info.IsDir() { + e = os.MkdirAll(path, info.Mode()) + if e != nil { + return e + } + continue + } + + file, e = os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode()) + if e != nil { + return e + } + + defer file.Close() + + _, e = io.Copy(file, tarReader) + if e != nil { + return e + } + } + + return nil +} + +func UntarGzip(source, target string) error { + var ( + tarPath string = strings.ReplaceAll(source, ".gz", "") + e error + ) + + e = UnGzip(source, tarPath) + if e != nil { + return e + } + + return Untar(tarPath, target) +} diff --git a/Client/Database/Init.go b/Client/Database/Init.go new file mode 100644 index 0000000..9410e25 --- /dev/null +++ b/Client/Database/Init.go @@ -0,0 +1,24 @@ +package Database + +import ( + "database/sql" + "log" + + _ "github.com/mattn/go-sqlite3" +) + +var ( + DB *sql.DB +) + +func init() { + var e error + DB, e = sql.Open("sqlite3", "./foo.db") + if e != nil { + panic(e) + } +} + +func InitDB() { + log.Println("Initialising Database...") +} diff --git a/Client/main.go b/Client/main.go new file mode 100644 index 0000000..4675230 --- /dev/null +++ b/Client/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + + "PackageManager/Archive" +) + +func main() { + //e := Archive.TarGzip("/tmp/test", "/tmp/test.tar.gz") + e := Archive.UntarGzip("/tmp/test.tar.gz", "/tmp/test") + fmt.Println(e) +}