"Fossies" - the Fresh Open Source Software Archive

Member "geoipupdate-4.3.0/pkg/geoipupdate/database/writer.go" (16 Apr 2020, 1304 Bytes) of package /linux/misc/geoipupdate-4.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "writer.go": 4.1.5_vs_4.2.0.

    1 package database
    2 
    3 import (
    4     "github.com/gofrs/flock"
    5     "github.com/pkg/errors"
    6     "io"
    7     "log"
    8     "os"
    9     "path/filepath"
   10     "time"
   11 )
   12 
   13 // ZeroMD5 is the default value provided as an MD5 hash for a non-existent
   14 // database.
   15 const ZeroMD5 = "00000000000000000000000000000000"
   16 
   17 // Writer provides an interface for writing a database to a target location.
   18 type Writer interface {
   19     io.WriteCloser
   20     ValidHash(expectedHash string) error
   21     GetHash() string
   22     SetFileModificationTime(lastModified time.Time) error
   23     Commit() error
   24 }
   25 
   26 // CreateLockFile takes the provided filePath and lockFilePath name to create a
   27 // file lock. All output errors are wrapped in more detailed messages for
   28 // debugging.
   29 func CreateLockFile(lockFilePath string, verbose bool) (*flock.Flock, error) {
   30     fi, err := os.Stat(filepath.Dir(lockFilePath))
   31     if err != nil {
   32         return nil, errors.Wrap(err, "database directory is not available")
   33     }
   34     if !fi.IsDir() {
   35         return nil, errors.New("database directory is not a directory")
   36     }
   37     lock := flock.New(lockFilePath)
   38     ok, err := lock.TryLock()
   39     if err != nil {
   40         return nil, errors.Wrap(err, "error acquiring a lock")
   41     }
   42     if !ok {
   43         return nil, errors.Errorf("could not acquire lock on %s", lockFilePath)
   44     }
   45     if verbose {
   46         log.Printf("Acquired lock file lock (%s)", lockFilePath)
   47     }
   48     return lock, nil
   49 }