简体   繁体   中英

How to get file size given name and parent directory handle or inode?

I'm writing a little utility to scan through a directory tree recursively and record the tree structure with file names and sizes. (as fast as possible)

Perhaps I've coded myself into a corner. I can get a directory's contents with readdir(3) and treat regular files and directories separately.

I'm not seeing how to obtain the regular file's file size except by openat(), fstat(), close() calls.

Is there a way to get the file size more directly as I'm processing the directory entries? Perhaps from the inode number?

I was expecting this would exist: statat(int parentDirFD, const char * filename) since I only have the filename, not the file path.

I don't appear to have O_PATH support in my kernel.

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

DIR* p_dir = ...;

int dir_fd = dirfd(p_dir);


size_t get_file_size(const char * filename_only, int dir_fd, DIR* pdir )
{
  //// this just strikes me as overly expensive:
  int fd = openat(dir_fd, filename_only, O_RDONLY );
  struct stat sb;
  fstat( fd, &sb );
  size_t file_size = sb.st_size;
  close(fd);
  //////

  return file_size;
}

Once Wumpus Q. Wumbley gave the answer in the comments, this turned out to be quite easy. fstatat()

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

DIR* p_dir = ...;

int dir_fd = dirfd(p_dir);


size_t get_file_size(const char * filename_only, int dir_fd )
{
  struct stat sb;
  int fstatat_flags = 0; //| AT_SYMLINK_NOFOLLOW
  int stat_res = fstatat( dir_fd, filename_only, &sb, fstatat_flags );
  size_t file_size = sb.st_size;

  return file_size;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM