ficsit-cli-flake/utils/io.go

190 lines
4 KiB
Go
Raw Normal View History

2022-05-02 20:07:15 +00:00
package utils
import (
"archive/zip"
"crypto/sha256"
"encoding/hex"
"fmt"
2022-05-02 20:07:15 +00:00
"io"
"os"
2022-06-05 01:56:46 +00:00
"path/filepath"
"sync"
"sync/atomic"
2022-05-02 20:07:15 +00:00
2022-06-22 22:24:35 +00:00
"github.com/satisfactorymodding/ficsit-cli/cli/disk"
2022-05-02 20:07:15 +00:00
)
type GenericProgress struct {
Completed int64
Total int64
}
func (gp GenericProgress) Percentage() float64 {
if gp.Total == 0 {
return 0
}
return float64(gp.Completed) / float64(gp.Total)
}
type Progresser struct {
io.Reader
Updates chan<- GenericProgress
Total int64
Running int64
}
func (pt *Progresser) Read(p []byte) (int, error) {
n, err := pt.Reader.Read(p)
pt.Running += int64(n)
if err == nil {
if pt.Updates != nil {
select {
case pt.Updates <- GenericProgress{Completed: pt.Running, Total: pt.Total}:
default:
}
}
}
if err == io.EOF {
return n, io.EOF
}
if err != nil {
return 0, fmt.Errorf("failed to read: %w", err)
}
return n, nil
}
2022-05-02 20:07:15 +00:00
func SHA256Data(f io.Reader) (string, error) {
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", fmt.Errorf("failed to compute hash: %w", err)
2022-05-02 20:07:15 +00:00
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates chan<- GenericProgress, d disk.Disk) error {
hashFile := filepath.Join(location, ".smm")
2022-06-22 22:24:35 +00:00
hashBytes, err := d.Read(hashFile)
if err != nil {
2022-06-22 22:24:35 +00:00
if !d.IsNotExist(err) {
return fmt.Errorf("failed to read .smm mod hash file: %w", err)
}
} else {
if hash == string(hashBytes) {
return nil
}
}
2022-06-22 22:24:35 +00:00
if err := d.MkDir(location); err != nil {
if !d.IsExist(err) {
return fmt.Errorf("failed to create mod directory: %s: %w", location, err)
2022-05-02 20:07:15 +00:00
}
2022-06-22 22:24:35 +00:00
if err := d.Remove(location); err != nil {
return fmt.Errorf("failed to remove directory: %s: %w", location, err)
2022-05-02 20:07:15 +00:00
}
2022-06-22 22:24:35 +00:00
if err := d.MkDir(location); err != nil {
return fmt.Errorf("failed to create mod directory: %s: %w", location, err)
2022-05-02 20:07:15 +00:00
}
}
reader, err := zip.NewReader(f, size)
if err != nil {
return fmt.Errorf("failed to read file as zip: %w", err)
2022-05-02 20:07:15 +00:00
}
totalSize := int64(0)
for _, file := range reader.File {
totalSize += int64(file.UncompressedSize64)
}
totalExtracted := int64(0)
totalExtractedPtr := &totalExtracted
channelUsers := sync.WaitGroup{}
if updates != nil {
defer func() {
channelUsers.Wait()
}()
}
for _, file := range reader.File {
2022-05-02 20:07:15 +00:00
if !file.FileInfo().IsDir() {
2022-06-05 01:56:46 +00:00
outFileLocation := filepath.Join(location, file.Name)
2022-05-02 20:07:15 +00:00
2022-06-22 22:24:35 +00:00
if err := d.MkDir(filepath.Dir(outFileLocation)); err != nil {
return fmt.Errorf("failed to create mod directory: %s: %w", location, err)
2022-05-02 20:07:15 +00:00
}
var fileUpdates chan GenericProgress
if updates != nil {
fileUpdates = make(chan GenericProgress)
channelUsers.Add(1)
go func() {
defer channelUsers.Done()
for fileUpdate := range fileUpdates {
updates <- GenericProgress{
Completed: atomic.LoadInt64(totalExtractedPtr) + fileUpdate.Completed,
Total: totalSize,
}
}
}()
2022-05-02 20:07:15 +00:00
}
if err := writeZipFile(outFileLocation, file, d, fileUpdates); err != nil {
return err
2022-05-02 20:07:15 +00:00
}
atomic.AddInt64(totalExtractedPtr, int64(file.UncompressedSize64))
}
}
2022-05-02 20:07:15 +00:00
2022-06-22 22:24:35 +00:00
if err := d.Write(hashFile, []byte(hash)); err != nil {
return fmt.Errorf("failed to write .smm mod hash file: %w", err)
}
if updates != nil {
updates <- GenericProgress{Completed: totalSize, Total: totalSize}
2022-05-02 20:07:15 +00:00
}
return nil
}
func writeZipFile(outFileLocation string, file *zip.File, d disk.Disk, updates chan<- GenericProgress) error {
if updates != nil {
defer close(updates)
}
2022-06-22 22:24:35 +00:00
outFile, err := d.Open(outFileLocation, os.O_CREATE|os.O_RDWR)
if err != nil {
return fmt.Errorf("failed to write to file: %s: %w", outFileLocation, err)
}
defer outFile.Close()
inFile, err := file.Open()
if err != nil {
return fmt.Errorf("failed to process mod zip: %w", err)
}
defer inFile.Close()
progressInReader := &Progresser{
Reader: inFile,
Total: int64(file.UncompressedSize64),
Updates: updates,
}
if _, err := io.Copy(outFile, progressInReader); err != nil {
return fmt.Errorf("failed to write to file: %s: %w", outFileLocation, err)
}
return nil
}