【问题标题】:Modification of directory into file in Linux by using inode structure in C programC程序中使用inode结构在Linux中将目录修改为文件
【发布时间】:2012-03-23 16:11:48
【问题描述】:

我想将目录更改为文件,我做了一些研究。在 Linux 中,inode 结构存储有关文件和目录的元数据。我想把文件保护模式从目录改成文件,

Print some general file info

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(int argc, char *argv[]) {
 struct stat file_stats;

 if(argc != 2)
  fprintf(stderr, "Usage: fstat FILE...\n"), exit(EXIT_FAILURE);

 if((stat(argv[1], &file_stats)) == -1) {
  perror("fstat"); 
  return 1;
 }

 printf("filename: %s\n", argv[1]);
 printf(" device: %lld\n",                       file_stats.st_dev);
 printf(" inode: %ld\n",                         file_stats.st_ino);
 printf(" protection: %o\n",                     file_stats.st_mode);
 printf(" number of hard links: %d\n",           file_stats.st_nlink);
 printf(" user ID of owner: %d\n",               file_stats.st_uid);
 printf(" group ID of owner: %d\n",              file_stats.st_gid);
 printf(" device type (if inode device): %lld\n",file_stats.st_rdev);
 printf(" total size, in bytes: %ld\n",          file_stats.st_size);
 printf(" blocksize for filesystem I/O: %ld\n",  file_stats.st_blksize);
 printf(" number of blocks allocated: %ld\n",    file_stats.st_blocks);
 printf(" time of last access: %ld : %s",        file_stats.st_atime, ctime(&file_stats.st_atime));
 printf(" time of last modification: %ld : %s",  file_stats.st_mtime, ctime(&file_stats.st_mtime));
 printf(" time of last change: %ld : %s",        file_stats.st_ctime, ctime(&file_stats.st_ctime));

 return 0;
}

有没有办法把目录改成文件?? C程序如何修改inode结构?

【问题讨论】:

  • 是否有特殊原因不只是删除文件然后创建目录?
  • 是的。我想通过将目录视为文件来对目录进行一些操作。
  • 一个目录已经是一个文件。你真正想做什么?
  • 以编程方式,我不确定,但是您可以检查一个叫做 debugfs 的东西。我刚刚读过它并且自己没有使用它,但这里有一个讨论 -> superuser.com/questions/153147/…。它通常用于检查和更改 ext* 文件系统类型的状态。是的,就像 Duck 说的,目录就是文件。那么通过更改 inode 中的属性可以实现什么?
  • @Nimit:您到底想对目录进行哪些操作而您不能?这个问题充其量是模糊的,最坏的情况是无法回答的。

标签: c linux filesystems inode file-structure


【解决方案1】:

要打开任何文件,您必须使用 open 系统调用。但是目前 open 系统调用不允许任何人打开目录进行写入。如果您使用目录调用 open 进行写入,它将返回错误 (-1) 并将 errno 设置为 EISDIR。

你还想做,你得重新实现Linux文件系统驱动的open系统调用。

【讨论】:

  • 有什么办法可以把目录转成文件吗?
  • 我不认为有这样的选择。但是您可以将内容复制到新文件中,并且可以将现有目录替换为新创建的文件。
猜你喜欢
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 2020-05-06
  • 2011-12-30
  • 2012-12-25
相关资源
最近更新 更多