github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/fileutil/syncdir.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  /*
     5  Copyright hechain. All Rights Reserved.
     6  
     7  SPDX-License-Identifier: Apache-2.0
     8  */
     9  
    10  package fileutil
    11  
    12  import (
    13  	"os"
    14  
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  // SyncDir fsyncs the given dir
    19  func SyncDir(dirPath string) error {
    20  	dir, err := os.Open(dirPath)
    21  	if err != nil {
    22  		return errors.Wrapf(err, "error while opening dir:%s", dirPath)
    23  	}
    24  	if err := dir.Sync(); err != nil {
    25  		dir.Close()
    26  		return errors.Wrapf(err, "error while synching dir:%s", dirPath)
    27  	}
    28  	if err := dir.Close(); err != nil {
    29  		return errors.Wrapf(err, "error while closing dir:%s", dirPath)
    30  	}
    31  	return err
    32  }