github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/core/rawdb/freezer_utils.go (about)

     1  // Copyright 2022 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rawdb
    18  
    19  import (
    20  	"io"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  )
    25  
    26  // copyFrom copies data from 'srcPath' at offset 'offset' into 'destPath'.
    27  // The 'destPath' is created if it doesn't exist, otherwise it is overwritten.
    28  // Before the copy is executed, there is a callback can be registered to
    29  // manipulate the dest file.
    30  // It is perfectly valid to have destPath == srcPath.
    31  func copyFrom(srcPath, destPath string, offset uint64, before func(f *os.File) error) error {
    32  	// Create a temp file in the same dir where we want it to wind up
    33  	f, err := ioutil.TempFile(filepath.Dir(destPath), "*")
    34  	if err != nil {
    35  		return err
    36  	}
    37  	fname := f.Name()
    38  
    39  	// Clean up the leftover file
    40  	defer func() {
    41  		if f != nil {
    42  			f.Close()
    43  		}
    44  		os.Remove(fname)
    45  	}()
    46  	// Apply the given function if it's not nil before we copy
    47  	// the content from the src.
    48  	if before != nil {
    49  		if err := before(f); err != nil {
    50  			return err
    51  		}
    52  	}
    53  	// Open the source file
    54  	src, err := os.Open(srcPath)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	if _, err = src.Seek(int64(offset), 0); err != nil {
    59  		src.Close()
    60  		return err
    61  	}
    62  	// io.Copy uses 32K buffer internally.
    63  	_, err = io.Copy(f, src)
    64  	if err != nil {
    65  		src.Close()
    66  		return err
    67  	}
    68  	// Rename the temporary file to the specified dest name.
    69  	// src may be same as dest, so needs to be closed before
    70  	// we do the final move.
    71  	src.Close()
    72  
    73  	if err := f.Close(); err != nil {
    74  		return err
    75  	}
    76  	f = nil
    77  
    78  	if err := os.Rename(fname, destPath); err != nil {
    79  		return err
    80  	}
    81  	return nil
    82  }
    83  
    84  // openFreezerFileForAppend opens a freezer table file and seeks to the end
    85  func openFreezerFileForAppend(filename string) (*os.File, error) {
    86  	// Open the file without the O_APPEND flag
    87  	// because it has differing behaviour during Truncate operations
    88  	// on different OS's
    89  	file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	// Seek to end for append
    94  	if _, err = file.Seek(0, io.SeekEnd); err != nil {
    95  		return nil, err
    96  	}
    97  	return file, nil
    98  }
    99  
   100  // openFreezerFileForReadOnly opens a freezer table file for read only access
   101  func openFreezerFileForReadOnly(filename string) (*os.File, error) {
   102  	return os.OpenFile(filename, os.O_RDONLY, 0644)
   103  }
   104  
   105  // openFreezerFileTruncated opens a freezer table making sure it is truncated
   106  func openFreezerFileTruncated(filename string) (*os.File, error) {
   107  	return os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
   108  }
   109  
   110  // truncateFreezerFile resizes a freezer table file and seeks to the end
   111  func truncateFreezerFile(file *os.File, size int64) error {
   112  	if err := file.Truncate(size); err != nil {
   113  		return err
   114  	}
   115  	// Seek to end for append
   116  	if _, err := file.Seek(0, io.SeekEnd); err != nil {
   117  		return err
   118  	}
   119  	return nil
   120  }