@@ -396,7 +396,7 @@ func (lrs *logResourceStorage) storeTile(ctx context.Context, level, index, logS
396
396
func (lrs * logResourceStorage ) writeTile (_ context.Context , level , index uint64 , partial uint8 , t []byte ) error {
397
397
tPath := layout .TilePath (level , index , partial )
398
398
399
- if err := lrs .s .createIdempotent (tPath , t ); err != nil {
399
+ if err := lrs .s .createOverwrite (tPath , t ); err != nil {
400
400
return err
401
401
}
402
402
@@ -428,7 +428,7 @@ func (lrs *logResourceStorage) writeTile(_ context.Context, level, index uint64,
428
428
// writeBundle takes care of writing out the serialised entry bundle file.
429
429
func (lrs * logResourceStorage ) writeBundle (_ context.Context , index uint64 , partial uint8 , bundle []byte ) error {
430
430
bf := lrs .entriesPath (index , partial )
431
- if err := lrs .s .createIdempotent (bf , bundle ); err != nil {
431
+ if err := lrs .s .createOverwrite (bf , bundle ); err != nil {
432
432
if ! errors .Is (err , os .ErrExist ) {
433
433
return err
434
434
}
@@ -525,7 +525,7 @@ func (s *Storage) writeTreeState(size uint64, root []byte) error {
525
525
return fmt .Errorf ("error in Marshal: %v" , err )
526
526
}
527
527
528
- if err := overwrite (filepath .Join (s . path , stateDir , "treeState" ), raw ); err != nil {
528
+ if err := s . createOverwrite (filepath .Join (stateDir , "treeState" ), raw ); err != nil {
529
529
return fmt .Errorf ("failed to create private tree state file: %w" , err )
530
530
}
531
531
return nil
@@ -581,8 +581,8 @@ func (a *appender) publishCheckpoint(ctx context.Context, minStaleness time.Dura
581
581
return fmt .Errorf ("newCP: %v" , err )
582
582
}
583
583
584
- if err := overwrite ( filepath . Join ( a .s .path , layout .CheckpointPath ) , cpRaw ); err != nil {
585
- return fmt .Errorf ("overwrite (%s): %v" , layout .CheckpointPath , err )
584
+ if err := a .s .createOverwrite ( layout .CheckpointPath , cpRaw ); err != nil {
585
+ return fmt .Errorf ("createOverwrite (%s): %v" , layout .CheckpointPath , err )
586
586
}
587
587
588
588
klog .V (2 ).Infof ("Published latest checkpoint: %d, %x" , size , root )
@@ -598,33 +598,17 @@ func (s *Storage) createExclusive(p string, d []byte) error {
598
598
return createEx (filepath .Join (s .path , p ), d )
599
599
}
600
600
601
+ // createOverwrite atomically creates or overwrites a file at the given path with the provided data.
602
+ func (s * Storage ) createOverwrite (p string , d []byte ) error {
603
+ return overwrite (filepath .Join (s .path , p ), d )
604
+ }
605
+
601
606
func (s * Storage ) readAll (p string ) ([]byte , error ) {
602
607
p = filepath .Join (s .path , p )
603
608
return os .ReadFile (p )
604
609
605
610
}
606
611
607
- // createIdempotent atomically writes the provided data to a file at the provided path, relative to the root of the log.
608
- // An error will be returned if a file already exists in the named location AND that file's
609
- // contents is not identical to the provided data.
610
- func (s * Storage ) createIdempotent (p string , d []byte ) error {
611
- if err := s .createExclusive (p , d ); err != nil {
612
- if errors .Is (err , os .ErrExist ) {
613
- r , err := s .readAll (p )
614
- if err != nil {
615
- return fmt .Errorf ("file %q already exists, but unable to read it: %v" , p , err )
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
622
- }
623
- return err
624
- }
625
- return nil
626
- }
627
-
628
612
// stat returns os.Stat info for the speficied file relative to the log root.
629
613
func (s * Storage ) stat (p string ) (os.FileInfo , error ) {
630
614
p = filepath .Join (s .path , p )
0 commit comments