...

Source file src/github.com/containerd/continuity/fs/du_unix_test.go

Documentation: github.com/containerd/continuity/fs

     1  //go:build !windows
     2  // +build !windows
     3  
     4  /*
     5     Copyright The containerd Authors.
     6  
     7     Licensed under the Apache License, Version 2.0 (the "License");
     8     you may not use this file except in compliance with the License.
     9     You may obtain a copy of the License at
    10  
    11         http://www.apache.org/licenses/LICENSE-2.0
    12  
    13     Unless required by applicable law or agreed to in writing, software
    14     distributed under the License is distributed on an "AS IS" BASIS,
    15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16     See the License for the specific language governing permissions and
    17     limitations under the License.
    18  */
    19  
    20  package fs
    21  
    22  import (
    23  	"errors"
    24  	"fmt"
    25  	"os"
    26  	"strconv"
    27  	"strings"
    28  	"syscall"
    29  	"testing"
    30  )
    31  
    32  func getBsize(root string) (int64, error) {
    33  	var s syscall.Statfs_t
    34  	if err := syscall.Statfs(root, &s); err != nil {
    35  		return 0, err
    36  	}
    37  
    38  	return int64(s.Bsize), nil //nolint: unconvert
    39  }
    40  
    41  // getTmpAlign returns filesystem specific size alignment functions
    42  // first:  aligns filesize to file usage based on blocks
    43  // second: determines directory usage based on directory count (assumes small directories)
    44  func getTmpAlign(t testing.TB) (func(int64) int64, func(int64) int64, error) {
    45  	t1 := t.TempDir()
    46  
    47  	bsize, err := getBsize(t1)
    48  	if err != nil {
    49  		return nil, nil, fmt.Errorf("failed to get bsize: %w", err)
    50  	}
    51  
    52  	align := func(size int64) int64 {
    53  		// Align to blocks
    54  		aligned := (size / bsize) * bsize
    55  
    56  		// Add next block if has remainder
    57  		if size%bsize > 0 {
    58  			aligned += bsize
    59  		}
    60  
    61  		return aligned
    62  	}
    63  
    64  	fi, err := os.Stat(t1)
    65  	if err != nil {
    66  		return nil, nil, fmt.Errorf("failed to stat directory: %w", err)
    67  	}
    68  
    69  	dirSize := fi.Sys().(*syscall.Stat_t).Blocks * blocksUnitSize
    70  	dirs := func(count int64) int64 {
    71  		return count * dirSize
    72  	}
    73  
    74  	return align, dirs, nil
    75  }
    76  
    77  func duCheck(root string) (usage int64, err error) {
    78  	cmd := duCmd(root)
    79  	cmd.Stderr = os.Stderr
    80  	out, err := cmd.Output()
    81  	if err != nil {
    82  		return 0, err
    83  	}
    84  	if len(out) == 0 {
    85  		return 0, errors.New("no du output")
    86  	}
    87  	size := strings.Fields(string(out))[0]
    88  	blocks, err := strconv.ParseInt(size, 10, 64)
    89  	if err != nil {
    90  		return 0, err
    91  	}
    92  
    93  	return blocks * blocksUnitSize, nil
    94  }
    95  

View as plain text