github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/local/generation.go (about)

     1  /*
     2  Copyright 2012 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  /*
    18  Package local implements functionality common to both the "localdisk" and
    19  "diskpacked" storage mechanisms.
    20  */
    21  package local
    22  
    23  import (
    24  	"bytes"
    25  	"crypto/rand"
    26  	"fmt"
    27  	"io"
    28  	"io/ioutil"
    29  	"os"
    30  	"path/filepath"
    31  	"time"
    32  )
    33  
    34  type Generationer struct {
    35  	root string
    36  }
    37  
    38  // NewGenerationer returns a struct for implementing blobserver.Generationer
    39  func NewGenerationer(rootDir string) *Generationer {
    40  	return &Generationer{rootDir}
    41  }
    42  
    43  func (g Generationer) generationFile() string {
    44  	return filepath.Join(g.root, "GENERATION.dat")
    45  }
    46  
    47  // StorageGeneration returns the generation's initialization time,
    48  // and the random string. If the GENERATION.dat file does not exist under
    49  // root yet, then creates it now (by calling ResetGeneration).
    50  func (g Generationer) StorageGeneration() (initTime time.Time, random string, err error) {
    51  	f, err := os.Open(g.generationFile())
    52  	if os.IsNotExist(err) {
    53  		if err = g.ResetStorageGeneration(); err != nil {
    54  			return
    55  		}
    56  		f, err = os.Open(g.generationFile())
    57  	}
    58  	if err != nil {
    59  		return
    60  	}
    61  	defer f.Close()
    62  	bs, err := ioutil.ReadAll(f)
    63  	if err != nil {
    64  		return
    65  	}
    66  	if i := bytes.IndexByte(bs, '\n'); i != -1 {
    67  		bs = bs[:i]
    68  	}
    69  	if fi, err := f.Stat(); err == nil {
    70  		initTime = fi.ModTime()
    71  	}
    72  	random = string(bs)
    73  	return
    74  }
    75  
    76  // ResetStorageGeneration reinitializes the generation by recreating the
    77  // GENERATION.dat file with a new random string
    78  func (g Generationer) ResetStorageGeneration() error {
    79  	var buf bytes.Buffer
    80  	if _, err := io.CopyN(&buf, rand.Reader, 20); err != nil {
    81  		return err
    82  	}
    83  	hex := fmt.Sprintf("%x", buf.Bytes())
    84  	buf.Reset()
    85  	buf.WriteString(hex)
    86  	buf.WriteString(`
    87  
    88  This file's random string on the first line is an optimization and
    89  paranoia facility for clients.
    90  
    91  If the client sees the same random string in multiple upload sessions,
    92  it assumes that the blobserver still has all the same blobs, and also
    93  it's the same server.  This mechanism is not fundamental to
    94  Camlistore's operation: the client could also check each blob before
    95  uploading, or enumerate all blobs from the server too.  This is purely
    96  an optimization so clients can mix this value into their "is this file
    97  uploaded?" local cache keys.
    98  
    99  If you deleted any blobs (or suspect any were corrupted), it's best to
   100  delete this file so clients can safely re-upload them.
   101  
   102  `)
   103  
   104  	return ioutil.WriteFile(g.generationFile(), buf.Bytes(), 0644)
   105  }