Skip to content

Commit 9a2c2f5

Browse files
authored
Merge pull request #15 from mrf345/testing
Refactor listing files and fix bench script
2 parents a359879 + 1afdf79 commit 9a2c2f5

File tree

3 files changed

+31
-60
lines changed

3 files changed

+31
-60
lines changed

benchmark/bench_and_plot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def encrypt():
5454
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {get_name('256')} --quiet --sha256' "
5555
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {get_name('512')} --quiet --sha512' "
5656
f"'7z a -p{pwd} -mx1 {get_name('7z')} {input_path}' "
57-
f"'gpgtar -e -o {get_name('gpg')} -c --yes --batch --gpg-args \"--passphrase {pwd}\" Videos/' "
57+
f"'gpgtar -e -o {get_name('gpg')} -c --yes --batch --gpg-args \"--passphrase {pwd}\" {input_path}' "
5858
f"--export-json {root}/encryption.json"
5959
)
6060

safelock/encrypt.go

+21-44
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
func (sl *Safelock) Encrypt(ctx context.Context, inputPaths []string, output io.Writer, password string) (err error) {
2222
errs := make(chan error)
2323
signals, closeSignals := sl.getExitSignals()
24-
start := 20.0
2524

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

3837
go func() {
39-
var inputFiles []archiver.File
40-
4138
if err = sl.validateEncryptionInputs(inputPaths, password); err != nil {
4239
errs <- fmt.Errorf("invalid encryption input > %w", err)
4340
return
4441
}
4542

46-
if inputFiles, err = sl.getInputFiles(ctx, inputPaths, 5.0, start); err != nil {
47-
errs <- fmt.Errorf("failed to read and list input paths > %w", err)
48-
return
49-
}
50-
5143
ctx, cancel := context.WithCancel(ctx)
52-
calc := utils.NewPathsCalculator(start, inputFiles)
44+
calc := utils.NewPathsCalculator(20.0)
5345
rw := newWriter(password, output, cancel, calc, sl.EncryptionConfig, errs)
5446
rw.asyncGcm = newAsyncGcm(password, sl.EncryptionConfig, errs)
5547

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

@@ -109,51 +101,36 @@ func (sl *Safelock) validateEncryptionInputs(inputPaths []string, pwd string) (e
109101
return
110102
}
111103

112-
func (sl *Safelock) getInputFiles(
104+
func (sl *Safelock) encryptFiles(
113105
ctx context.Context,
114-
paths []string,
115-
start, end float64,
116-
) (files []archiver.File, err error) {
117-
sl.updateStatus("Listing and preparing files ", start)
118-
119-
filesMap := make(map[string]string, len(paths))
120-
ctx, cancel := context.WithCancel(ctx)
121-
defer cancel()
106+
inputPaths []string,
107+
slWriter *safelockWriter,
108+
calc *utils.PercentCalculator,
109+
) (err error) {
110+
sl.updateStatus("Listing and preparing files ", 5.0)
122111

123-
go func() {
124-
for {
125-
select {
126-
case <-ctx.Done():
127-
return
128-
default:
129-
if end >= start {
130-
start += 1.0
131-
time.Sleep(time.Second / 5)
132-
}
133-
}
134-
}
135-
}()
112+
var files []archiver.File
113+
var filesMap = make(map[string]string, len(inputPaths))
136114

137-
for _, path := range paths {
115+
for _, path := range inputPaths {
138116
filesMap[path] = ""
139117
}
140118

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

145-
return
146-
}
124+
go func() {
125+
for _, file := range files {
126+
calc.InputSize += int(file.Size())
127+
}
147128

148-
func (sl *Safelock) encryptFiles(
149-
ctx context.Context,
150-
inputFiles []archiver.File,
151-
slWriter *safelockWriter,
152-
calc *utils.PercentCalculator,
153-
) (err error) {
154-
go sl.updateProgressStatus(ctx, "Encrypting", calc)
129+
sl.updateProgressStatus(ctx, "Encrypting", calc)
130+
}()
155131

156-
if err = sl.archive(ctx, slWriter, inputFiles); err != nil {
132+
if err = sl.archive(ctx, slWriter, files); err != nil {
133+
err = fmt.Errorf("failed to create encrypted archive file > %w", err)
157134
return
158135
}
159136

utils/percentCalculator.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package utils
22

33
import (
44
"io"
5-
6-
"github.com/mholt/archiver/v4"
75
)
86

97
// helps calculating file completion percentage
@@ -15,21 +13,11 @@ type PercentCalculator struct {
1513
}
1614

1715
// create new instance of [utils.PercentCalculator] for input paths
18-
func NewPathsCalculator(start float64, files []archiver.File) *PercentCalculator {
19-
pc := &PercentCalculator{
16+
func NewPathsCalculator(start float64) *PercentCalculator {
17+
return &PercentCalculator{
2018
start: start,
2119
end: 100.0,
2220
}
23-
pc.setInputPathsSize(files)
24-
return pc
25-
}
26-
27-
func (pc *PercentCalculator) setInputPathsSize(files []archiver.File) {
28-
for _, file := range files {
29-
if !file.FileInfo.IsDir() {
30-
pc.InputSize += int(file.FileInfo.Size())
31-
}
32-
}
3321
}
3422

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

5139
// get current completion percentage
5240
func (pc *PercentCalculator) GetPercent() float64 {
53-
return pc.start + (float64(pc.OutputSize) / float64(pc.InputSize) * pc.end)
41+
percent := pc.start + (float64(pc.OutputSize) / float64(pc.InputSize) * pc.end)
42+
43+
if pc.end > percent {
44+
return percent
45+
}
46+
47+
return pc.end
5448
}

0 commit comments

Comments
 (0)