Skip to content

Commit

Permalink
chore: refactor listing files and fix bench script
Browse files Browse the repository at this point in the history
  • Loading branch information
mrf345 committed Sep 5, 2024
1 parent d8ac25a commit 1afdf79
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 60 deletions.
2 changes: 1 addition & 1 deletion benchmark/bench_and_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def encrypt():
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {get_name('256')} --quiet --sha256' "
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {get_name('512')} --quiet --sha512' "
f"'7z a -p{pwd} -mx1 {get_name('7z')} {input_path}' "
f"'gpgtar -e -o {get_name('gpg')} -c --yes --batch --gpg-args \"--passphrase {pwd}\" Videos/' "
f"'gpgtar -e -o {get_name('gpg')} -c --yes --batch --gpg-args \"--passphrase {pwd}\" {input_path}' "
f"--export-json {root}/encryption.json"
)

Expand Down
65 changes: 21 additions & 44 deletions safelock/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
func (sl *Safelock) Encrypt(ctx context.Context, inputPaths []string, output io.Writer, password string) (err error) {
errs := make(chan error)
signals, closeSignals := sl.getExitSignals()
start := 20.0

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

go func() {
var inputFiles []archiver.File

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

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)
calc := utils.NewPathsCalculator(20.0)
rw := newWriter(password, output, cancel, calc, sl.EncryptionConfig, errs)
rw.asyncGcm = newAsyncGcm(password, sl.EncryptionConfig, errs)

if err = sl.encryptFiles(ctx, inputFiles, rw, calc); err != nil {
errs <- fmt.Errorf("failed to create encrypted archive file > %w", err)
if err = sl.encryptFiles(ctx, inputPaths, rw, calc); err != nil {
errs <- err
return
}

Expand Down Expand Up @@ -109,51 +101,36 @@ func (sl *Safelock) validateEncryptionInputs(inputPaths []string, pwd string) (e
return
}

func (sl *Safelock) getInputFiles(
func (sl *Safelock) encryptFiles(
ctx context.Context,
paths []string,
start, end float64,
) (files []archiver.File, err error) {
sl.updateStatus("Listing and preparing files ", start)

filesMap := make(map[string]string, len(paths))
ctx, cancel := context.WithCancel(ctx)
defer cancel()
inputPaths []string,
slWriter *safelockWriter,
calc *utils.PercentCalculator,
) (err error) {
sl.updateStatus("Listing and preparing files ", 5.0)

go func() {
for {
select {
case <-ctx.Done():
return
default:
if end >= start {
start += 1.0
time.Sleep(time.Second / 5)
}
}
}
}()
var files []archiver.File
var filesMap = make(map[string]string, len(inputPaths))

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

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

return
}
go func() {
for _, file := range files {
calc.InputSize += int(file.Size())
}

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

if err = sl.archive(ctx, slWriter, inputFiles); err != nil {
if err = sl.archive(ctx, slWriter, files); err != nil {
err = fmt.Errorf("failed to create encrypted archive file > %w", err)
return
}

Expand Down
24 changes: 9 additions & 15 deletions utils/percentCalculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package utils

import (
"io"

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

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

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

func (pc *PercentCalculator) setInputPathsSize(files []archiver.File) {
for _, file := range files {
if !file.FileInfo.IsDir() {
pc.InputSize += int(file.FileInfo.Size())
}
}
}

// create new instance of [utils.PercentCalculator] for [io.Seeker]
Expand All @@ -50,5 +38,11 @@ func (pc *PercentCalculator) setInputSeekerSize(seeker io.Seeker) (err error) {

// get current completion percentage
func (pc *PercentCalculator) GetPercent() float64 {
return pc.start + (float64(pc.OutputSize) / float64(pc.InputSize) * pc.end)
percent := pc.start + (float64(pc.OutputSize) / float64(pc.InputSize) * pc.end)

if pc.end > percent {
return percent
}

return pc.end
}

0 comments on commit 1afdf79

Please sign in to comment.