@@ -25,8 +25,8 @@ type lockStruct struct {
2525}
2626
2727// lockFile sets a lock on the specified file, using flock.
28- func lockFile (filename string ) (lockStruct , error ) {
29- closer , err := vfs . Default .Lock (filename )
28+ func lockFile (fs vfs. FS , filename string ) (lockStruct , error ) {
29+ closer , err := fs .Lock (filename )
3030 if err != nil {
3131 return lockStruct {}, err
3232 }
@@ -64,7 +64,7 @@ func CreateTempDir(parentDir, prefix string) (_ string, unlockDirFn func(), _ er
6464 }
6565
6666 // Create a lock file.
67- flock , err := lockFile (filepath .Join (absPath , lockFilename ))
67+ flock , err := lockFile (vfs . Default , filepath .Join (absPath , lockFilename ))
6868 if err != nil {
6969 return "" , nil , errors .Wrapf (err , "could not create lock on new temporary directory" )
7070 }
@@ -98,19 +98,23 @@ func RecordTempDir(recordPath, tempPath string) error {
9898// up abandoned temporary directories.
9999// It should also be invoked when a newly created temporary directory is no
100100// longer needed and needs to be removed from the record file.
101- func CleanupTempDirs (recordPath string ) error {
101+ func CleanupTempDirs (ctx context. Context , fs vfs. FS , recordPath string ) error {
102102 // Reading the entire file into memory shouldn't be a problem since
103103 // it is extremely rare for this record file to contain more than a few
104104 // entries.
105- f , err := os . OpenFile (recordPath , os . O_RDWR , 0644 )
105+ f , err := fs . Open (recordPath )
106106 // There is no existing record file and thus nothing to clean up.
107107 if oserror .IsNotExist (err ) {
108108 return nil
109109 }
110110 if err != nil {
111111 return err
112112 }
113- defer f .Close ()
113+ defer func () {
114+ if f != nil {
115+ f .Close ()
116+ }
117+ }()
114118
115119 scanner := bufio .NewScanner (f )
116120 // Iterate through each temporary directory path and remove the
@@ -122,14 +126,14 @@ func CleanupTempDirs(recordPath string) error {
122126 }
123127
124128 // Check if the temporary directory exists; if it does not, skip over it.
125- if _ , err := os .Stat (path ); oserror .IsNotExist (err ) {
126- log .Dev .Warningf (context . Background () , "could not locate previous temporary directory %s, might require manual cleanup, or might have already been cleaned up." , path )
129+ if _ , err := fs .Stat (path ); oserror .IsNotExist (err ) {
130+ log .Dev .Warningf (ctx , "could not locate previous temporary directory %s, might require manual cleanup, or might have already been cleaned up." , path )
127131 continue
128132 }
129133
130134 // Check if another Cockroach instance is using this temporary
131135 // directory i.e. has a lock on the temp dir lock file.
132- flock , err := lockFile (filepath . Join (path , lockFilename ))
136+ flock , err := lockFile (fs , fs . PathJoin (path , lockFilename ))
133137 if err != nil {
134138 return errors .Wrapf (err , "could not lock temporary directory %s, may still be in use" , path )
135139 }
@@ -143,15 +147,21 @@ func CleanupTempDirs(recordPath string) error {
143147 // process is dead because we were able to acquire the lock in the first
144148 // place.
145149 if err := unlockFile (flock ); err != nil {
146- log .Dev .Errorf (context . TODO () , "could not unlock file lock when removing temporary directory: %s" , err .Error ())
150+ log .Dev .Errorf (ctx , "could not unlock file lock when removing temporary directory: %s" , err .Error ())
147151 }
148152
149153 // If path/directory does not exist, error is nil.
150- if err := os .RemoveAll (path ); err != nil {
154+ if err := fs .RemoveAll (path ); err != nil {
151155 return err
152156 }
153157 }
154158
155- // Clear out the record file now that we're done.
156- return f .Truncate (0 )
159+ // Remove the record file now that we're done.
160+ err = errors .CombineErrors (fs .Remove (recordPath ), f .Close ())
161+ f = nil
162+ rmErr := fs .Remove (recordPath )
163+ if rmErr != nil && ! oserror .IsNotExist (rmErr ) {
164+ err = errors .CombineErrors (err , rmErr )
165+ }
166+ return err
157167}
0 commit comments