github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/orderer/util.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     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  package main
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  
    24  	"github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage"
    25  	"github.com/hyperledger/fabric/orderer/ledger"
    26  	fileledger "github.com/hyperledger/fabric/orderer/ledger/file"
    27  	jsonledger "github.com/hyperledger/fabric/orderer/ledger/json"
    28  	ramledger "github.com/hyperledger/fabric/orderer/ledger/ram"
    29  	config "github.com/hyperledger/fabric/orderer/localconfig"
    30  )
    31  
    32  func createLedgerFactory(conf *config.TopLevel) (ledger.Factory, string) {
    33  	var lf ledger.Factory
    34  	var ld string
    35  	switch conf.General.LedgerType {
    36  	case "file":
    37  		ld = conf.FileLedger.Location
    38  		if ld == "" {
    39  			ld = createTempDir(conf.FileLedger.Prefix)
    40  		}
    41  		logger.Debug("Ledger dir:", ld)
    42  		lf = fileledger.New(ld)
    43  		// The file-based ledger stores the blocks for each channel
    44  		// in a fsblkstorage.ChainsDir sub-directory that we have
    45  		// to create separately. Otherwise the call to the ledger
    46  		// Factory's ChainIDs below will fail (dir won't exist).
    47  		createSubDir(ld, fsblkstorage.ChainsDir)
    48  	case "json":
    49  		ld = conf.FileLedger.Location
    50  		if ld == "" {
    51  			ld = createTempDir(conf.FileLedger.Prefix)
    52  		}
    53  		logger.Debug("Ledger dir:", ld)
    54  		lf = jsonledger.New(ld)
    55  	case "ram":
    56  		fallthrough
    57  	default:
    58  		lf = ramledger.New(int(conf.RAMLedger.HistorySize))
    59  	}
    60  	return lf, ld
    61  }
    62  
    63  func createTempDir(dirPrefix string) string {
    64  	dirPath, err := ioutil.TempDir("", dirPrefix)
    65  	if err != nil {
    66  		logger.Panic("Error creating temp dir:", err)
    67  	}
    68  	return dirPath
    69  }
    70  
    71  func createSubDir(parentDirPath string, subDir string) (string, bool) {
    72  	var created bool
    73  	subDirPath := filepath.Join(parentDirPath, subDir)
    74  	if _, err := os.Stat(subDirPath); err != nil {
    75  		if os.IsNotExist(err) {
    76  			if err = os.Mkdir(subDirPath, 0755); err != nil {
    77  				logger.Panic("Error creating sub dir:", err)
    78  			}
    79  			created = true
    80  		}
    81  	} else {
    82  		logger.Debugf("Found %s sub-dir and using it", fsblkstorage.ChainsDir)
    83  	}
    84  	return subDirPath, created
    85  }