Skip to content

Commit

Permalink
feat: Add verbose logging to file processing
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin committed Jul 8, 2024
1 parent 1d9e1e9 commit 047617a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
25 changes: 25 additions & 0 deletions pkg/processor/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package processor

import (
"fmt"
"log"
"os"
"path/filepath"

Expand Down Expand Up @@ -38,15 +39,25 @@ func NewCpProcessor(sourceFile, destFile string, storage *storage.Storage, hashG

// Process processes the file and creates a link at the destination
func (p *CpProcessor) Process(verbose bool) (err error) {
if verbose {
log.Printf("Processing file: %s", p.SourceFile)
}

// Compute file hash
var finalHash string

if p.Storage.Opts.WithMetadata {
if verbose {
log.Println("Computing full hash with metadata")
}
finalHash, err = p.HashGen.ComputeFullHash(p.SourceFile)
if err != nil {
return fmt.Errorf("computing full hash: %w", err)
}
} else {
if verbose {
log.Println("Computing content hash without metadata")
}
finalHash, err = p.HashGen.ComputeFileHash(p.SourceFile)
if err != nil {
return fmt.Errorf("computing content hash: %w", err)
Expand All @@ -63,19 +74,33 @@ func (p *CpProcessor) Process(verbose bool) (err error) {

// If the file does not exist, move it to storage
if !exists {
if verbose {
log.Printf("File does not exist in storage, moving it: %s", dedupPath)
}
err = p.Storage.MoveFileToStorage(p.SourceFile, finalHash)
if err != nil {
return fmt.Errorf("moving file to storage: %w", err)
}
} else {
if verbose {
log.Printf("File already exists in storage: %s", dedupPath)
}
}

// Create a link at the destination pointing to the file in storage,
// removing the destination if it already exists
if verbose {
log.Printf("Creating link at destination: %s", p.DestFile)
}
os.Remove(p.DestFile)
err = os.Link(dedupPath, p.DestFile)
if err != nil {
return fmt.Errorf("linking file: %w", err)
}

if verbose {
log.Printf("Successfully linked file to destination: %s", p.DestFile)
}

return nil
}
36 changes: 34 additions & 2 deletions pkg/processor/dedup.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (p *DedupProcessor) Process(verbose bool) error {
go func() {
defer wg.Done()
for path := range jobs {
err := p.processFile(path)
err := p.processFile(path, verbose)
if err != nil {
if verbose {
log.Printf("Error processing file %s: %v", path, err)
Expand All @@ -116,6 +116,9 @@ func (p *DedupProcessor) Process(verbose bool) error {
return err
}
if !info.IsDir() && path != p.Storage.Opts.Root {
if verbose {
log.Printf("Adding file to job queue: %s", path)
}
jobs <- path
}
return nil
Expand All @@ -129,16 +132,26 @@ func (p *DedupProcessor) Process(verbose bool) error {
return nil
}

func (p *DedupProcessor) processFile(path string) (err error) {
func (p *DedupProcessor) processFile(path string, verbose bool) (err error) {
if verbose {
log.Printf("Processing file: %s", path)
}

// Compute file hash
var finalHash string

if p.Storage.Opts.WithMetadata {
if verbose {
log.Println("Computing full hash with metadata")
}
finalHash, err = p.HashGen.ComputeFullHash(path)
if err != nil {
return fmt.Errorf("computing full hash: %w", err)
}
} else {
if verbose {
log.Println("Computing content hash without metadata")
}
finalHash, err = p.HashGen.ComputeFileHash(path)
if err != nil {
return fmt.Errorf("computing content hash: %w", err)
Expand All @@ -148,6 +161,9 @@ func (p *DedupProcessor) processFile(path string) (err error) {
// Check if the file is already being processed
alreadyProcessing, waitChan := dedupStartProcessing(finalHash)
if alreadyProcessing {
if verbose {
log.Printf("File %s is already being processed, waiting...", path)
}
<-waitChan // Wait for the processing to finish
}

Expand All @@ -160,13 +176,19 @@ func (p *DedupProcessor) processFile(path string) (err error) {
}

if !exists {
if verbose {
log.Printf("File does not exist in storage, moving it: %s", dedupPath)
}
// If the file does not exist in storage, move it there
err = p.Storage.MoveFileToStorage(path, finalHash)
if err != nil {
dedupFinishProcessing(finalHash)
return fmt.Errorf("moving file to storage: %w", err)
}
} else {
if verbose {
log.Printf("File already exists in storage: %s", dedupPath)
}
// If the file already exists in storage, remove the source file
err = os.Remove(path)
if err != nil {
Expand All @@ -181,6 +203,9 @@ func (p *DedupProcessor) processFile(path string) (err error) {
p.mapMutex.Unlock()

// Create a link at the original location
if verbose {
log.Printf("Creating link at original location: %s", path)
}
if _, err := os.Lstat(path); os.IsNotExist(err) {
err = os.Link(dedupPath, path)
if err != nil {
Expand All @@ -198,6 +223,9 @@ func (p *DedupProcessor) processFile(path string) (err error) {
}

destPath := filepath.Join(p.DestDir, relativePath)
if verbose {
log.Printf("Creating link at destination: %s", destPath)
}
if _, err := os.Lstat(destPath); os.IsNotExist(err) {
err = os.Link(dedupPath, destPath)
if err != nil {
Expand All @@ -208,5 +236,9 @@ func (p *DedupProcessor) processFile(path string) (err error) {
}

dedupFinishProcessing(finalHash)

if verbose {
log.Printf("Finished processing file: %s", path)
}
return nil
}

0 comments on commit 047617a

Please sign in to comment.