|
|
@@ -6,6 +6,7 @@ import (
|
|
|
gopath "path"
|
|
|
"path/filepath"
|
|
|
|
|
|
+ "github.com/opencost/opencost/pkg/log"
|
|
|
"github.com/opencost/opencost/pkg/util/fileutil"
|
|
|
"github.com/pkg/errors"
|
|
|
)
|
|
|
@@ -84,7 +85,7 @@ func (fs *FileStorage) ListDirectories(path string) ([]*StorageInfo, error) {
|
|
|
files = append(files, info)
|
|
|
}
|
|
|
|
|
|
- return DirFilesToStorageInfo(files, path), nil
|
|
|
+ return DirFilesToStorageInfo(files, path, fs.baseDir), nil
|
|
|
}
|
|
|
|
|
|
// Read uses the relative path of the storage combined with the provided path to
|
|
|
@@ -177,15 +178,36 @@ func FileToStorageInfo(fileInfo gofs.FileInfo) *StorageInfo {
|
|
|
|
|
|
// DirFilesToStorageInfo maps a []fs.FileInfo to []*storage.StorageInfo
|
|
|
// but only returning StorageInfo for directories
|
|
|
-func DirFilesToStorageInfo(fileInfo []gofs.FileInfo, path string) []*StorageInfo {
|
|
|
+func DirFilesToStorageInfo(fileInfo []gofs.FileInfo, relativePath string, basePath string) []*StorageInfo {
|
|
|
var stats []*StorageInfo
|
|
|
for _, info := range fileInfo {
|
|
|
if info.IsDir() {
|
|
|
stats = append(stats, &StorageInfo{
|
|
|
- Name: filepath.Join(path, info.Name()),
|
|
|
+ Name: filepath.Join(relativePath, info.Name()),
|
|
|
Size: info.Size(),
|
|
|
ModTime: info.ModTime(),
|
|
|
})
|
|
|
+ } else if info.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
+ targetPath, err := os.Readlink(filepath.Join(basePath, relativePath, info.Name()))
|
|
|
+ if err != nil {
|
|
|
+ log.Warnf("Converting files to storage info failed on supposed symlink that failed to be Readlink-ed (%s): %s", filepath.Join(basePath, relativePath, info.Name()), err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if targetInfo, err := os.Stat(targetPath); err == nil {
|
|
|
+ if targetInfo.IsDir() {
|
|
|
+ stats = append(stats, &StorageInfo{
|
|
|
+ // The Name added here is the path not readlink-ed
|
|
|
+ Name: filepath.Join(relativePath, info.Name()),
|
|
|
+ Size: info.Size(),
|
|
|
+ ModTime: info.ModTime(),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } else if errors.Is(err, os.ErrNotExist) {
|
|
|
+ continue
|
|
|
+ } else {
|
|
|
+ log.Warnf("Converting files to storage info failed to stat target (%s) of symlink (%s): %s", targetPath, info.Name(), err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return stats
|