refactor: Use standard CLI flags and cmd naming scheme

This commit is contained in:
Felix Pojtinger
2021-11-19 01:17:19 +01:00
parent ce68ab38a1
commit c28d8b9630
6 changed files with 62 additions and 77 deletions

View File

@@ -32,15 +32,15 @@ const (
)
func main() {
file := flag.String("file", "/dev/nst0", "File (tape drive or tar file) to open")
dir := flag.String("dir", ".", "Directory to add to the file")
drive := flag.String("drive", "/dev/nst0", "Tape or tar file to write to")
recordSize := flag.Int("recordSize", 20, "Amount of 512-bit blocks per record")
overwrite := flag.Bool("overwrite", false, "Whether to start writing from the current position instead of from the end of the tape")
src := flag.String("src", ".", "Directory to archive")
overwrite := flag.Bool("overwrite", false, "Start writing from the current position instead of from the end of the tape/file")
flag.Parse()
isRegular := true
stat, err := os.Stat(*file)
stat, err := os.Stat(*drive)
if err == nil {
isRegular = stat.Mode().IsRegular()
} else {
@@ -54,12 +54,12 @@ func main() {
var f *os.File
if isRegular {
if *overwrite {
f, err = os.OpenFile(*file, os.O_WRONLY|os.O_CREATE, 0600)
f, err = os.OpenFile(*drive, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
} else {
f, err = os.OpenFile(*file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
f, err = os.OpenFile(*drive, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
@@ -67,7 +67,7 @@ func main() {
// No need to go to end manually due to `os.O_APPEND`
} else {
f, err = os.OpenFile(*file, os.O_APPEND|os.O_WRONLY, os.ModeCharDevice)
f, err = os.OpenFile(*drive, os.O_APPEND|os.O_WRONLY, os.ModeCharDevice)
if err != nil {
panic(err)
}
@@ -90,7 +90,7 @@ func main() {
}
defer tw.Close()
if err := filepath.Walk(*dir, func(path string, info fs.FileInfo, err error) error {
if err := filepath.Walk(*src, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}

View File

@@ -13,24 +13,24 @@ import (
)
func main() {
file := flag.String("file", "/dev/nst0", "File (tape drive or tar file) to open")
drive := flag.String("drive", "/dev/nst0", "Tape or tar file to read from")
recordSize := flag.Int("recordSize", 20, "Amount of 512-bit blocks per record")
flag.Parse()
fileDescription, err := os.Stat(*file)
fileDescription, err := os.Stat(*drive)
if err != nil {
panic(err)
}
var f *os.File
if fileDescription.Mode().IsRegular() {
f, err = os.Open(*file)
f, err = os.Open(*drive)
if err != nil {
panic(err)
}
} else {
f, err = os.OpenFile(*file, os.O_RDONLY, os.ModeCharDevice)
f, err = os.OpenFile(*drive, os.O_RDONLY, os.ModeCharDevice)
if err != nil {
panic(err)
}

View File

@@ -13,27 +13,28 @@ import (
)
func main() {
file := flag.String("file", "/dev/nst0", "File (tape drive or tar file) to open")
drive := flag.String("drive", "/dev/nst0", "Tape or tar file to read from")
recordSize := flag.Int("recordSize", 20, "Amount of 512-bit blocks per record")
record := flag.Int("record", 0, "Record to seek too")
block := flag.Int("block", 0, "Block in record to seek too")
headerOnly := flag.Bool("headerOnly", false, "Only read the header")
dst := flag.String("dst", "", "File to restore to (archived name by default)")
preview := flag.Bool("preview", false, "Only read the header")
flag.Parse()
fileDescription, err := os.Stat(*file)
fileDescription, err := os.Stat(*drive)
if err != nil {
panic(err)
}
var f *os.File
if fileDescription.Mode().IsRegular() {
f, err = os.Open(*file)
f, err = os.Open(*drive)
if err != nil {
panic(err)
}
} else {
f, err = os.OpenFile(*file, os.O_RDONLY, os.ModeCharDevice)
f, err = os.OpenFile(*drive, os.O_RDONLY, os.ModeCharDevice)
if err != nil {
panic(err)
}
@@ -70,13 +71,17 @@ func main() {
log.Println(hdr)
if !*headerOnly {
dst, err := os.OpenFile(filepath.Base(hdr.Name), os.O_WRONLY|os.O_CREATE, hdr.FileInfo().Mode())
if !*preview {
if *dst == "" {
*dst = filepath.Base(hdr.Name)
}
dstFile, err := os.OpenFile(*dst, os.O_WRONLY|os.O_CREATE, hdr.FileInfo().Mode())
if err != nil {
panic(err)
}
if _, err := io.Copy(dst, tr); err != nil {
if _, err := io.Copy(dstFile, tr); err != nil {
panic(err)
}
}

35
cmd/stbak-seek/main.go Normal file
View File

@@ -0,0 +1,35 @@
package main
import (
"bufio"
"flag"
"os"
"github.com/pojntfx/stfs/pkg/controllers"
)
func main() {
drive := flag.String("drive", "/dev/nst0", "Tape drive to seek on")
recordSize := flag.Int("recordSize", 20, "Amount of 512-bit blocks per record")
record := flag.Int("record", 0, "Record to seek too")
block := flag.Int("block", 0, "Block in record to seek too")
flag.Parse()
f, err := os.OpenFile(*drive, os.O_RDONLY, os.ModeCharDevice)
if err != nil {
panic(err)
}
defer f.Close()
// Seek to record
if err := controllers.SeekToRecordOnTape(f, int32(*record)); err != nil {
panic(err)
}
// Seek to block
br := bufio.NewReaderSize(f, controllers.BlockSize**recordSize)
if _, err := br.Read(make([]byte, *block*controllers.BlockSize)); err != nil {
panic(err)
}
}

View File

@@ -9,11 +9,11 @@ import (
)
func main() {
file := flag.String("file", "/dev/nst0", "File of tape drive to open")
drive := flag.String("drive", "/dev/nst0", "Tape drive to get position from")
flag.Parse()
f, err := os.OpenFile(*file, os.O_RDONLY, os.ModeCharDevice)
f, err := os.OpenFile(*drive, os.O_RDONLY, os.ModeCharDevice)
if err != nil {
panic(err)
}

View File

@@ -1,55 +0,0 @@
package main
import (
"bufio"
"flag"
"os"
"github.com/pojntfx/stfs/pkg/controllers"
)
func main() {
file := flag.String("file", "/dev/nst0", "File (tape drive or tar file) to open")
recordSize := flag.Int("recordSize", 20, "Amount of 512-bit blocks per record")
record := flag.Int("record", 0, "Record to seek too")
block := flag.Int("block", 0, "Block in record to seek too")
flag.Parse()
fileDescription, err := os.Stat(*file)
if err != nil {
panic(err)
}
var f *os.File
if fileDescription.Mode().IsRegular() {
f, err = os.Open(*file)
if err != nil {
panic(err)
}
} else {
f, err = os.OpenFile(*file, os.O_RDONLY, os.ModeCharDevice)
if err != nil {
panic(err)
}
}
defer f.Close()
if fileDescription.Mode().IsRegular() {
// Seek to record and block
if _, err := f.Seek(int64((*recordSize*controllers.BlockSize**record)+*block*controllers.BlockSize), 0); err != nil {
panic(err)
}
} else {
// Seek to record
if err := controllers.SeekToRecordOnTape(f, int32(*record)); err != nil {
panic(err)
}
// Seek to block
br := bufio.NewReaderSize(f, controllers.BlockSize**recordSize)
if _, err := br.Read(make([]byte, *block*controllers.BlockSize)); err != nil {
panic(err)
}
}
}