有没有一种简单的方法来判断文件流是否打开了目录而不是文件?

发布于 2024-08-12 10:53:59 字数 183 浏览 1 评论 0原文

我正在制作一个 HTTP 服务器,当我获得他们请求的文件的路径时,我使用以下命令打开它:

returned_file = fopen(path, "r");

即使路径是目录,这(与我的想法相反)也会成功。有没有一种简单的方法来检查 returned_file 流是否是目录而不是文件?

I'm making an HTTP server and when I get the path of the file they request I open it with the following:

returned_file = fopen(path, "r");

this (contrary to what I would think) succeeds even if the path is a directory. Is there an easy way to check if the returned_file stream is a directory instead of a file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如歌彻婉言 2024-08-19 10:53:59

您可以对 fopen 返回的文件描述符使用 fstat。

编辑:
这是示例程序:

#include <sys/stat.h>
#include <stdio.h>

void printISDir( FILE* fp, char const * name ) {
  int fdes = fileno(fp) ;
  struct stat fileInfo ;
  fstat(fdes, &fileInfo ) ;
  if ( S_ISDIR(fileInfo.st_mode ) ) {
    printf("%s: I'm a dir!\n", name ) ;
  } else {
    printf("%s: I'm a file!\n", name ) ;
  }

}

int main( int argc, char** argv ) {
  char const * directoryName = "/etc" ;
  char const * fileName = "/etc/hosts" ;

  FILE* dirFp = fopen(directoryName, "r") ;
  FILE* fileFp = fopen(fileName, "r") ;
  printISDir( dirFp, directoryName ) ;
  printISDir( fileFp, fileName ) ;
  fclose(dirFp) ;
  fclose(fileFp) ;

  return 0 ;
}

you can use fstat on the file descriptor returned by fopen.

Edit:
Here's and example program:

#include <sys/stat.h>
#include <stdio.h>

void printISDir( FILE* fp, char const * name ) {
  int fdes = fileno(fp) ;
  struct stat fileInfo ;
  fstat(fdes, &fileInfo ) ;
  if ( S_ISDIR(fileInfo.st_mode ) ) {
    printf("%s: I'm a dir!\n", name ) ;
  } else {
    printf("%s: I'm a file!\n", name ) ;
  }

}

int main( int argc, char** argv ) {
  char const * directoryName = "/etc" ;
  char const * fileName = "/etc/hosts" ;

  FILE* dirFp = fopen(directoryName, "r") ;
  FILE* fileFp = fopen(fileName, "r") ;
  printISDir( dirFp, directoryName ) ;
  printISDir( fileFp, fileName ) ;
  fclose(dirFp) ;
  fclose(fileFp) ;

  return 0 ;
}
只怪假的太真实 2024-08-19 10:53:59

详细说明其他答案,您可以在返回的文件描述符上调用 fstat 并检查 st_mode 中的 S_IFDIR 位。 S_ISDIR 辅助宏很有帮助:

  #include <sys/stat.h>

...

  FILE* f = fopen(path, "r");

  struct stat buf;
  if (fstat(fileno(f), &buf) == -1) {
    perror("fstat");
  } else {
    if (S_ISDIR(buf.st_mode)) {
      printf("is directory\n");
    } else {
      printf("not directory\n");
    }
  }

Elaborating on the other answers, you can call fstat on the returned file descriptor and check the st_mode for the S_IFDIR bit. The S_ISDIR helper macro is helpful:

  #include <sys/stat.h>

...

  FILE* f = fopen(path, "r");

  struct stat buf;
  if (fstat(fileno(f), &buf) == -1) {
    perror("fstat");
  } else {
    if (S_ISDIR(buf.st_mode)) {
      printf("is directory\n");
    } else {
      printf("not directory\n");
    }
  }
温柔一刀 2024-08-19 10:53:59

您可以在调用 fopen 之前检查路径是否指向目录吗?

Can you check if the path points to a directory before you call fopen?

伊面 2024-08-19 10:53:59

在打开文件名之前使用 stat(),或者使用 fstat() 文件描述符 fileno(returned_file)。

Use stat() on the file name before you open it, or fstat() the file descriptor fileno(returned_file).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文