[C 언어] 파일이 저장된 마운트포인트 획득 하는방법 있나요?

ljh723의 이미지

stat -c %m fileName
리눅스에서 위 명령어와 같이 마운트포인트 획득을 해야합니다.
C언어에서 어떻게 구현해야 할지 감이 안잡힙니다.. 지원되는 API가 있나요??

 의 이미지

https://www.gnu.org/software/coreutils/coreutils.html

GNU coreutils 소스코드를 다운받아서 stat.c 소스를 열면 어떻게 구현되어 있는지 찾을 수 있지요.

/* Print mount point.  Return zero upon success, nonzero upon failure.  */
static bool ATTRIBUTE_WARN_UNUSED_RESULT
out_mount_point (char const *filename, char *pformat, size_t prefix_len,
                 const struct stat *statp)
{
 
  char const *np = "?", *bp = NULL;
  char *mp = NULL;
  bool fail = true;
 
  /* Look for bind mounts first.  Note we output the immediate alias,
     rather than further resolving to a base device mount point.  */
  if (follow_links || !S_ISLNK (statp->st_mode))
    {
      char *resolved = canonicalize_file_name (filename);
      if (!resolved)
        {
          error (0, errno, _("failed to canonicalize %s"), quoteaf (filename));
          goto print_mount_point;
        }
      bp = find_bind_mount (resolved);
      free (resolved);
      if (bp)
        {
          fail = false;
          goto print_mount_point;
        }
    }
 
  /* If there is no direct bind mount, then navigate
     back up the tree looking for a device change.
     Note we don't detect if any of the directory components
     are bind mounted to the same device, but that's OK
     since we've not directly queried them.  */
  if ((mp = find_mount_point (filename, statp)))
    {
      /* This dir might be bind mounted to another device,
         so we resolve the bound source in that case also.  */
      bp = find_bind_mount (mp);
      fail = false;
    }
 
print_mount_point:
 
  out_string (pformat, prefix_len, bp ? bp : mp ? mp : np);
  free (mp);
  return fail;
}