github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/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  	"io/ioutil"
    19  	"os"
    20  	"sync"
    21  )
    22  
    23  // TempFileProvider is an interface which provides methods for creating temporary files.
    24  type TempFileProvider interface {
    25  	// GetTempDir returns the directory where temp files will be created by default
    26  	GetTempDir() string
    27  
    28  	// NewFile creates a new temporary file in the directory dir, opens the file for reading and writing, and returns
    29  	// the resulting *os.File. If dir is "" then the default temp dir is used.
    30  	NewFile(dir, pattern string) (*os.File, error)
    31  
    32  	// Clean makes a best effort attempt to delete all temp files created by calls to NewFile
    33  	Clean()
    34  }
    35  
    36  // TempFileProviderAt is a TempFileProvider interface which creates temp files at a given path.
    37  type TempFileProviderAt struct {
    38  	tempDir      string
    39  	filesCreated []string
    40  	mu           sync.Mutex
    41  }
    42  
    43  // NewTempFileProviderAt creates a new TempFileProviderAt instance with the provided directory to create files in. The
    44  // directory is assumed to have been created already.
    45  func NewTempFileProviderAt(tempDir string) *TempFileProviderAt {
    46  	return &TempFileProviderAt{tempDir, nil, sync.Mutex{}}
    47  }
    48  
    49  // GetTempDir returns the directory where temp files will be created by default
    50  func (tfp *TempFileProviderAt) GetTempDir() string {
    51  	return tfp.tempDir
    52  }
    53  
    54  // NewFile creates a new temporary file in the directory dir, opens the file for reading and writing, and returns
    55  // the resulting *os.File. If dir is "" then the default temp dir is used.
    56  func (tfp *TempFileProviderAt) NewFile(dir, pattern string) (*os.File, error) {
    57  	tfp.mu.Lock()
    58  	defer tfp.mu.Unlock()
    59  	if dir == "" {
    60  		dir = tfp.tempDir
    61  	}
    62  
    63  	f, err := ioutil.TempFile(dir, pattern)
    64  
    65  	if err == nil {
    66  		tfp.filesCreated = append(tfp.filesCreated, f.Name())
    67  	}
    68  
    69  	return f, err
    70  }
    71  
    72  // Clean makes a best effort attempt to delete all temp files created by calls to NewFile
    73  func (tfp *TempFileProviderAt) Clean() {
    74  	tfp.mu.Lock()
    75  	defer tfp.mu.Unlock()
    76  	for _, filename := range tfp.filesCreated {
    77  		// best effort. ignore errors
    78  		_ = os.Remove(filename)
    79  	}
    80  }
    81  
    82  // MovableTemFile is an object that implements TempFileProvider that is used by the nbs to create temp files that
    83  // ultimately will be renamed.  It is important to use this instance rather than using os.TempDir, or ioutil.TempFile
    84  // directly as those may have errors executing a rename against if the volume the default temporary directory lives on
    85  // is different than the volume of the destination of the rename.
    86  var MovableTempFileProvider TempFileProvider = NewTempFileProviderAt(os.TempDir())