diff --git a/cmd/encrypt.go b/cmd/encrypt.go index 355a55f..224bb66 100644 --- a/cmd/encrypt.go +++ b/cmd/encrypt.go @@ -48,7 +48,7 @@ var encryptCmd = &cobra.Command{ sl.Quiet = beQuiet inputPath, outputPath := []string{args[0]}, args[1] - if outputFile, err = os.OpenFile(outputPath, os.O_RDWR|os.O_CREATE, 0776); err != nil { + if outputFile, err = os.OpenFile(outputPath, os.O_RDWR|os.O_CREATE, 0755); err != nil { utils.PrintErrsAndExit((&slErrs.ErrInvalidOutputPath{ Path: outputPath, Err: err, diff --git a/safelock/decrypt.go b/safelock/decrypt.go index 45ab368..84af0cd 100644 --- a/safelock/decrypt.go +++ b/safelock/decrypt.go @@ -100,7 +100,6 @@ func (sl *Safelock) decryptFiles( } go sl.updateProgressStatus(ctx, "Decrypting", calc) - defer slReader.cancel() fileHandler := getExtractFileHandler(outputPath) @@ -108,6 +107,7 @@ func (sl *Safelock) decryptFiles( return fmt.Errorf("cannot extract archive file > %w", err) } + slReader.cancel() return } diff --git a/safelock/encrypt.go b/safelock/encrypt.go index 3b1431f..fbe5eaa 100644 --- a/safelock/encrypt.go +++ b/safelock/encrypt.go @@ -107,10 +107,9 @@ func (sl *Safelock) encryptFiles( slWriter *safelockWriter, calc *utils.PercentCalculator, ) (err error) { - sl.updateStatus("Listing and preparing files ", 5.0) - var files []archiver.File var filesMap = make(map[string]string, len(inputPaths)) + var cancelListingStatus = sl.updateListingStatus(ctx, 1.0, calc.Start) for _, path := range inputPaths { filesMap[path] = "" @@ -126,6 +125,7 @@ func (sl *Safelock) encryptFiles( calc.InputSize += int(file.Size()) } + cancelListingStatus() sl.updateProgressStatus(ctx, "Encrypting", calc) }() @@ -138,6 +138,29 @@ func (sl *Safelock) encryptFiles( return } +func (sl *Safelock) updateListingStatus(ctx context.Context, start, end float64) (cancel context.CancelFunc) { + ctx, cancel = context.WithCancel(ctx) + + go func() { + for { + select { + case <-ctx.Done(): + return + default: + if start >= end { + return + } + + start += 1 + sl.updateStatus("Listing and preparing files", start) + time.Sleep(time.Second / 2) + } + } + }() + + return +} + func (sl *Safelock) updateProgressStatus(ctx context.Context, act string, calc *utils.PercentCalculator) { for { select { diff --git a/safelock/reader.go b/safelock/reader.go index cb46534..d360554 100644 --- a/safelock/reader.go +++ b/safelock/reader.go @@ -44,11 +44,6 @@ func newReader( } } -func (sr *safelockReader) setSize() { - sr.size = sr.calc.InputSize - sr.setHeaderSize() -} - func (sr *safelockReader) ReadHeader() (err error) { sr.setSize() @@ -79,6 +74,11 @@ func (sr *safelockReader) ReadHeader() (err error) { return } +func (sr *safelockReader) setSize() { + sr.size = sr.calc.InputSize + sr.headerSize = sr.config.getHeaderSizeOut(sr.size) +} + func (sr *safelockReader) Read(chunk []byte) (read int, err error) { var blockSize int var block string diff --git a/safelock/readerWriter.go b/safelock/readerWriter.go index f6b8cb4..b49ed12 100644 --- a/safelock/readerWriter.go +++ b/safelock/readerWriter.go @@ -19,10 +19,6 @@ type safelockReaderWriterBase struct { err error } -func (srw *safelockReaderWriterBase) setHeaderSize() { - srw.headerSize = srw.config.getHeaderSize(srw.size) -} - func (srw *safelockReaderWriterBase) diffSize() int64 { return int64(srw.size - srw.headerSize) } diff --git a/safelock/safelock.go b/safelock/safelock.go index e124163..4d1e2c3 100644 --- a/safelock/safelock.go +++ b/safelock/safelock.go @@ -29,12 +29,24 @@ type EncryptionConfig struct { GcmBufferSize int } -func (ec *EncryptionConfig) getHeaderSize(fileSize int) int { - if ec.HeaderRatio > fileSize/100 { +func (ec *EncryptionConfig) getHeaderSizeIn(fileSize int) int { + size := fileSize / ec.HeaderRatio + + if ec.HeaderRatio > size { + return ec.HeaderRatio + } + + return (fileSize + size) / ec.HeaderRatio +} + +func (ec *EncryptionConfig) getHeaderSizeOut(fileSize int) int { + size := fileSize / ec.HeaderRatio + + if ec.HeaderRatio > size { return ec.HeaderRatio } - return int(fileSize) / ec.HeaderRatio + return fileSize / ec.HeaderRatio } // archiving and compression configuration settings @@ -78,7 +90,7 @@ func New() *Safelock { KeyLength: 48, NonceLength: 12, MinPasswordLength: 8, - HeaderRatio: 7400, + HeaderRatio: 1024 * 4, GcmBufferSize: 500, Hash: sha512.New384, }, diff --git a/safelock/writer.go b/safelock/writer.go index bec8295..3e65611 100644 --- a/safelock/writer.go +++ b/safelock/writer.go @@ -36,11 +36,6 @@ func newWriter( } } -func (sw *safelockWriter) setSize() { - sw.size = sw.calc.OutputSize - sw.setHeaderSize() -} - func (sw *safelockWriter) Write(chunk []byte) (written int, err error) { encrypted := sw.asyncGcm.encryptChunk(chunk) @@ -74,3 +69,8 @@ func (sw *safelockWriter) WriteHeader() (err error) { return } + +func (sw *safelockWriter) setSize() { + sw.size = sw.calc.OutputSize + sw.headerSize = sw.config.getHeaderSizeIn(sw.size) +} diff --git a/utils/percentCalculator.go b/utils/percentCalculator.go index 31d9d09..0e3ef65 100644 --- a/utils/percentCalculator.go +++ b/utils/percentCalculator.go @@ -8,23 +8,23 @@ import ( type PercentCalculator struct { OutputSize int InputSize int - start float64 - end float64 + Start float64 + End float64 } // create new instance of [utils.PercentCalculator] for input paths func NewPathsCalculator(start float64) *PercentCalculator { return &PercentCalculator{ - start: start, - end: 100.0, + Start: start, + End: 100.0, } } // create new instance of [utils.PercentCalculator] for [io.Seeker] func NewSeekerCalculator(inputSeeker io.Seeker, start float64) (pc *PercentCalculator, err error) { pc = &PercentCalculator{ - start: start, - end: 100.0, + Start: start, + End: 100.0, } err = pc.setInputSeekerSize(inputSeeker) return @@ -38,11 +38,11 @@ func (pc *PercentCalculator) setInputSeekerSize(seeker io.Seeker) (err error) { // get current completion percentage func (pc *PercentCalculator) GetPercent() float64 { - percent := 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 { + if pc.End > percent { return percent } - return pc.end + return pc.End }