Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix closed signal issue, and improve list files #14

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var rootCmd = &cobra.Command{
Use: "safelock-cli",
Short: "Simple tool to encrypt/decrypt files with AES encryption",
Long: "Simple command-line tool to encrypt and decrypt files with AES encryption",
Version: "0.4.1",
Version: "0.4.2",
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
Expand Down
4 changes: 2 additions & 2 deletions safelock/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// NOTE: `ctx` context is optional you can pass `nil` and the method will handle it
func (sl *Safelock) Decrypt(ctx context.Context, input InputReader, outputPath, password string) (err error) {
errs := make(chan error)
signals := sl.getExitSignalsChannel()
signals, closeSignals := sl.getExitSignals()

if ctx == nil {
ctx = context.Background()
Expand Down Expand Up @@ -59,8 +59,8 @@ func (sl *Safelock) Decrypt(ctx context.Context, input InputReader, outputPath,
}

sl.updateStatus("All set and decrypted!", 100.0)
close(signals)
close(errs)
closeSignals()
}()

for {
Expand Down
73 changes: 54 additions & 19 deletions safelock/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
// NOTE: `ctx` context is optional you can pass `nil` and the method will handle it
func (sl *Safelock) Encrypt(ctx context.Context, inputPaths []string, output io.Writer, password string) (err error) {
errs := make(chan error)
signals := sl.getExitSignalsChannel()
signals, closeSignals := sl.getExitSignals()
start := 20.0

if ctx == nil {
ctx = context.Background()
Expand All @@ -35,23 +36,24 @@ func (sl *Safelock) Encrypt(ctx context.Context, inputPaths []string, output io.
Trigger(StatusEnd.Str())

go func() {
var calc *utils.PercentCalculator
var inputFiles []archiver.File

if err = sl.validateEncryptionInputs(inputPaths, password); err != nil {
errs <- fmt.Errorf("invalid encryption input > %w", err)
return
}

if calc, err = utils.NewPathsCalculator(inputPaths, 20.0); err != nil {
errs <- fmt.Errorf("failed to read input paths > %w", err)
if inputFiles, err = sl.getInputFiles(ctx, inputPaths, 5.0, start); err != nil {
errs <- fmt.Errorf("failed to read and list input paths > %w", err)
return
}

ctx, cancel := context.WithCancel(ctx)
calc := utils.NewPathsCalculator(start, inputFiles)
rw := newWriter(password, output, cancel, calc, sl.EncryptionConfig, errs)
rw.asyncGcm = newAsyncGcm(password, sl.EncryptionConfig, errs)

if err = sl.encryptFiles(ctx, inputPaths, rw, calc); err != nil {
if err = sl.encryptFiles(ctx, inputFiles, rw, calc); err != nil {
errs <- fmt.Errorf("failed to create encrypted archive file > %w", err)
return
}
Expand All @@ -62,8 +64,8 @@ func (sl *Safelock) Encrypt(ctx context.Context, inputPaths []string, output io.
}

sl.updateStatus("All set and encrypted!", 100.0)
close(signals)
close(errs)
closeSignals()
}()

for {
Expand All @@ -80,10 +82,15 @@ func (sl *Safelock) Encrypt(ctx context.Context, inputPaths []string, output io.
}
}

func (sl *Safelock) getExitSignalsChannel() chan os.Signal {
func (sl *Safelock) getExitSignals() (<-chan os.Signal, func()) {
signals := make(chan os.Signal, 2)
close := func() {
signal.Stop(signals)
close(signals)
}

signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)
return signals
return signals, close
}

func (sl *Safelock) validateEncryptionInputs(inputPaths []string, pwd string) (err error) {
Expand All @@ -102,28 +109,56 @@ func (sl *Safelock) validateEncryptionInputs(inputPaths []string, pwd string) (e
return
}

func (sl *Safelock) encryptFiles(
func (sl *Safelock) getInputFiles(
ctx context.Context,
inputPaths []string,
slWriter *safelockWriter,
calc *utils.PercentCalculator,
) (err error) {
var files []archiver.File
var filesMap = make(map[string]string, len(inputPaths))
paths []string,
start, end float64,
) (files []archiver.File, err error) {
sl.updateStatus("Listing and preparing files ", start)

for _, path := range inputPaths {
filesMap := make(map[string]string, len(paths))
ctx, cancel := context.WithCancel(ctx)
defer cancel()

go func() {
for {
select {
case <-ctx.Done():
return
default:
if end >= start {
start += 1.0
time.Sleep(time.Second / 5)
}
}
}
}()

for _, path := range paths {
filesMap[path] = ""
}

if files, err = archiver.FilesFromDisk(nil, filesMap); err != nil {
err = fmt.Errorf("failed to list archive files > %w", err)
return
}

return
}

func (sl *Safelock) encryptFiles(
ctx context.Context,
inputFiles []archiver.File,
slWriter *safelockWriter,
calc *utils.PercentCalculator,
) (err error) {
go sl.updateProgressStatus(ctx, "Encrypting", calc)
defer slWriter.cancel()

return sl.archive(ctx, slWriter, files)
if err = sl.archive(ctx, slWriter, inputFiles); err != nil {
return
}

slWriter.cancel()
return
}

func (sl *Safelock) updateProgressStatus(ctx context.Context, act string, calc *utils.PercentCalculator) {
Expand Down
20 changes: 0 additions & 20 deletions utils/getDirSize.go

This file was deleted.

32 changes: 10 additions & 22 deletions utils/percentCalculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package utils

import (
"io"
"io/fs"
"os"

"github.com/mholt/archiver/v4"
)

// helps calculating file completion percentage
Expand All @@ -15,33 +15,21 @@ type PercentCalculator struct {
}

// create new instance of [utils.PercentCalculator] for input paths
func NewPathsCalculator(inputPaths []string, start float64) (pc *PercentCalculator, err error) {
pc = &PercentCalculator{
func NewPathsCalculator(start float64, files []archiver.File) *PercentCalculator {
pc := &PercentCalculator{
start: start,
end: 100.0,
}
err = pc.setInputPathsSize(inputPaths)
return
pc.setInputPathsSize(files)
return pc
}

func (pc *PercentCalculator) setInputPathsSize(paths []string) (err error) {
for _, path := range paths {
var info fs.FileInfo

if info, err = os.Stat(path); err != nil {
return
}

if info.IsDir() {
if pc.InputSize, err = GetDirSize(path); err != nil {
return
}
} else {
pc.InputSize += int(info.Size())
func (pc *PercentCalculator) setInputPathsSize(files []archiver.File) {
for _, file := range files {
if !file.FileInfo.IsDir() {
pc.InputSize += int(file.FileInfo.Size())
}
}

return
}

// create new instance of [utils.PercentCalculator] for [io.Seeker]
Expand Down