github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/util/tempfiles/temp_files.go (about) 1 // Copyright 2020 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tempfiles 16 17 import ( 18 "os" 19 "sync" 20 21 "github.com/dolthub/dolt/go/libraries/utils/file" 22 ) 23 24 // TempFileProvider is an interface which provides methods for creating temporary files. 25 type TempFileProvider interface { 26 // GetTempDir returns the directory where temp files will be created by default 27 GetTempDir() string 28 29 // NewFile creates a new temporary file in the directory dir, opens the file for reading and writing, and returns 30 // the resulting *os.File. If dir is "" then the default temp dir is used. 31 NewFile(dir, pattern string) (*os.File, error) 32 33 // Clean makes a best effort attempt to delete all temp files created by calls to NewFile 34 Clean() 35 } 36 37 // TempFileProviderAt is a TempFileProvider interface which creates temp files at a given path. 38 type TempFileProviderAt struct { 39 tempDir string 40 filesCreated []string 41 mu sync.Mutex 42 } 43 44 // NewTempFileProviderAt creates a new TempFileProviderAt instance with the provided directory to create files in. The 45 // directory is assumed to have been created already. 46 func NewTempFileProviderAt(tempDir string) *TempFileProviderAt { 47 return &TempFileProviderAt{tempDir, nil, sync.Mutex{}} 48 } 49 50 // GetTempDir returns the directory where temp files will be created by default 51 func (tfp *TempFileProviderAt) GetTempDir() string { 52 return tfp.tempDir 53 } 54 55 // NewFile creates a new temporary file in the directory dir, opens the file for reading and writing, and returns 56 // the resulting *os.File. If dir is "" then the default temp dir is used. 57 func (tfp *TempFileProviderAt) NewFile(dir, pattern string) (*os.File, error) { 58 tfp.mu.Lock() 59 defer tfp.mu.Unlock() 60 if dir == "" { 61 dir = tfp.tempDir 62 } 63 64 f, err := os.CreateTemp(dir, pattern) 65 66 if err == nil { 67 tfp.filesCreated = append(tfp.filesCreated, f.Name()) 68 } 69 70 return f, err 71 } 72 73 // Clean makes a best effort attempt to delete all temp files created by calls to NewFile 74 func (tfp *TempFileProviderAt) Clean() { 75 tfp.mu.Lock() 76 defer tfp.mu.Unlock() 77 for _, filename := range tfp.filesCreated { 78 // best effort. ignore errors 79 _ = file.Remove(filename) 80 } 81 } 82 83 // MovableTemFile is an object that implements TempFileProvider that is used by the nbs to create temp files that 84 // ultimately will be renamed. It is important to use this instance rather than using os.TempDir, or os.CreateTemp 85 // directly as those may have errors executing a rename against if the volume the default temporary directory lives on 86 // is different than the volume of the destination of the rename. 87 var MovableTempFileProvider TempFileProvider = NewTempFileProviderAt(os.TempDir())