@@ -45,8 +45,6 @@ const (
45
45
// that were created before we introduced this.
46
46
compatibilityVersion = 1
47
47
48
- dirPerm = 0o755
49
- filePerm = 0o644
50
48
stateDir = ".state"
51
49
52
50
minCheckpointInterval = time .Second
@@ -442,7 +440,7 @@ func (lrs *logResourceStorage) writeBundle(_ context.Context, index uint64, part
442
440
// creating a zero-sized one if it doesn't already exist.
443
441
func (a * appender ) initialise (ctx context.Context ) error {
444
442
// Idempotent: If folder exists, nothing happens.
445
- if err := os . MkdirAll (filepath .Join (a .s .path , stateDir ), dirPerm ); err != nil {
443
+ if err := mkdirAll (filepath .Join (a .s .path , stateDir ), dirPerm ); err != nil {
446
444
return fmt .Errorf ("failed to create log directory: %q" , err )
447
445
}
448
446
// Double locking:
@@ -527,7 +525,7 @@ func (s *Storage) writeTreeState(size uint64, root []byte) error {
527
525
return fmt .Errorf ("error in Marshal: %v" , err )
528
526
}
529
527
530
- if err := s . overwrite (filepath .Join (stateDir , "treeState" ), raw ); err != nil {
528
+ if err := overwrite (filepath .Join (s . path , stateDir , "treeState" ), raw ); err != nil {
531
529
return fmt .Errorf ("failed to create private tree state file: %w" , err )
532
530
}
533
531
return nil
@@ -583,9 +581,7 @@ func (a *appender) publishCheckpoint(ctx context.Context, minStaleness time.Dura
583
581
return fmt .Errorf ("newCP: %v" , err )
584
582
}
585
583
586
- // TODO(mhutchinson): grab witness signatures
587
-
588
- if err := a .s .overwrite (layout .CheckpointPath , cpRaw ); err != nil {
584
+ if err := overwrite (filepath .Join (a .s .path , layout .CheckpointPath ), cpRaw ); err != nil {
589
585
return fmt .Errorf ("overwrite(%s): %v" , layout .CheckpointPath , err )
590
586
}
591
587
@@ -599,8 +595,7 @@ func (a *appender) publishCheckpoint(ctx context.Context, minStaleness time.Dura
599
595
// It will error if a file already exists at the specified location, or it's unable to fully write the
600
596
// data & close the file.
601
597
func (s * Storage ) createExclusive (p string , d []byte ) error {
602
- p = filepath .Join (s .path , p )
603
- return createEx (p , d )
598
+ return createEx (filepath .Join (s .path , p ), d )
604
599
}
605
600
606
601
func (s * Storage ) readAll (p string ) ([]byte , error ) {
@@ -615,84 +610,27 @@ func (s *Storage) readAll(p string) ([]byte, error) {
615
610
func (s * Storage ) createIdempotent (p string , d []byte ) error {
616
611
if err := s .createExclusive (p , d ); err != nil {
617
612
if errors .Is (err , os .ErrExist ) {
618
- if r , err := s .readAll (p ); err != nil {
613
+ r , err := s .readAll (p )
614
+ if err != nil {
619
615
return fmt .Errorf ("file %q already exists, but unable to read it: %v" , p , err )
620
- } else if bytes .Equal (d , r ) {
621
- // Idempotent write
622
- return nil
623
616
}
617
+ if ! bytes .Equal (d , r ) {
618
+ return fmt .Errorf ("file %q already exists but has different contents" , p )
619
+ }
620
+ // Idempotent write.
621
+ return nil
624
622
}
625
623
return err
626
624
}
627
625
return nil
628
626
}
629
627
630
- // overwrite atomically overwrites the specified file relative to the root of the log (or creates it if it doesn't exist)
631
- // with the provided data.
632
- func (s * Storage ) overwrite (p string , d []byte ) error {
633
- p = filepath .Join (s .path , p )
634
- tmpN := p + ".tmp"
635
- if err := os .WriteFile (tmpN , d , filePerm ); err != nil {
636
- return fmt .Errorf ("failed to write temp file %q: %v" , tmpN , err )
637
- }
638
- if err := os .Rename (tmpN , p ); err != nil {
639
- _ = os .Remove (tmpN )
640
- return fmt .Errorf ("failed to move temp file into target location %q: %v" , p , err )
641
- }
642
- return nil
643
- }
644
-
645
628
// stat returns os.Stat info for the speficied file relative to the log root.
646
629
func (s * Storage ) stat (p string ) (os.FileInfo , error ) {
647
630
p = filepath .Join (s .path , p )
648
631
return os .Stat (p )
649
632
}
650
633
651
- // createEx atomically creates a file at the given path containing the provided data.
652
- //
653
- // It will error if a file already exists at the specified location, or it's unable to fully write the
654
- // data & close the file.
655
- func createEx (p string , d []byte ) error {
656
- dir , f := filepath .Split (p )
657
- if err := os .MkdirAll (dir , dirPerm ); err != nil {
658
- return fmt .Errorf ("failed to make entries directory structure: %w" , err )
659
- }
660
- tmpF , err := os .CreateTemp (dir , f + "-*" )
661
- if err != nil {
662
- return fmt .Errorf ("failed to create temp file: %v" , err )
663
- }
664
- if err := tmpF .Chmod (filePerm ); err != nil {
665
- return fmt .Errorf ("failed to chmod temp file: %v" , err )
666
- }
667
- tmpName := tmpF .Name ()
668
- defer func () {
669
- if tmpF != nil {
670
- if err := tmpF .Close (); err != nil {
671
- klog .Warningf ("Failed to close temporary file: %v" , err )
672
- }
673
- }
674
- if err := os .Remove (tmpName ); err != nil {
675
- klog .Warningf ("Failed to remove temporary file %q: %v" , tmpName , err )
676
- }
677
- }()
678
-
679
- if n , err := tmpF .Write (d ); err != nil {
680
- return fmt .Errorf ("unable to write data to temporary file: %v" , err )
681
- } else if l := len (d ); n != l {
682
- return fmt .Errorf ("short write (%d < %d byte) on temporary file" , n , l )
683
- }
684
- if err := tmpF .Close (); err != nil {
685
- return fmt .Errorf ("failed to close temporary file: %v" , err )
686
- }
687
- tmpF = nil
688
- if err := os .Link (tmpName , p ); err != nil {
689
- // Wrap the error here because we need to know if it's os.ErrExists at higher levels.
690
- return fmt .Errorf ("failed to link temporary file to target %q: %w" , p , err )
691
- }
692
-
693
- return nil
694
- }
695
-
696
634
// MigrationWriter creates a new POSIX storage for the MigrationTarget lifecycle mode.
697
635
func (s * Storage ) MigrationWriter (ctx context.Context , opts * tessera.MigrationOptions ) (tessera.MigrationWriter , tessera.LogReader , error ) {
698
636
r := & MigrationStorage {
@@ -743,7 +681,7 @@ func (m *MigrationStorage) AwaitIntegration(ctx context.Context, sourceSize uint
743
681
744
682
func (m * MigrationStorage ) initialise () error {
745
683
// Idempotent: If folder exists, nothing happens.
746
- if err := os . MkdirAll (filepath .Join (m .s .path , stateDir ), dirPerm ); err != nil {
684
+ if err := mkdirAll (filepath .Join (m .s .path , stateDir ), dirPerm ); err != nil {
747
685
return fmt .Errorf ("failed to create log directory: %q" , err )
748
686
}
749
687
// Double locking:
0 commit comments