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

     1  /*
     2  Copyright 2011 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  package localdisk
    18  
    19  import (
    20  	"os"
    21  
    22  	"camlistore.org/pkg/blob"
    23  	"camlistore.org/pkg/syncutil"
    24  	"camlistore.org/pkg/types"
    25  )
    26  
    27  const maxParallelStats = 20
    28  
    29  var statGate = syncutil.NewGate(maxParallelStats)
    30  
    31  func (ds *DiskStorage) StatBlobs(dest chan<- blob.SizedRef, blobs []blob.Ref) error {
    32  	if len(blobs) == 0 {
    33  		return nil
    34  	}
    35  
    36  	statSend := func(ref blob.Ref) error {
    37  		fi, err := os.Stat(ds.blobPath(ref))
    38  		switch {
    39  		case err == nil && fi.Mode().IsRegular():
    40  			dest <- blob.SizedRef{Ref: ref, Size: types.U32(fi.Size())}
    41  			return nil
    42  		case err != nil && !os.IsNotExist(err):
    43  			return err
    44  		}
    45  		return nil
    46  	}
    47  
    48  	if len(blobs) == 1 {
    49  		return statSend(blobs[0])
    50  	}
    51  
    52  	var wg syncutil.Group
    53  	for _, ref := range blobs {
    54  		ref := ref
    55  		statGate.Start()
    56  		wg.Go(func() error {
    57  			defer statGate.Done()
    58  			return statSend(ref)
    59  		})
    60  	}
    61  	return wg.Err()
    62  }