1

I want to change a directory into a file, I did some research. In Linux, inode structure stores the metadata about the file and directory. I want to change the file protection mode from Directory to file,

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;
}

Is there any way to change the directory into a file?? How to modify inode structure by C program?

Nimit
  • 1,714
  • 3
  • 22
  • 33
  • 3
    Is there a particular reason to not just delete the file and then create a directory? – Corbin Mar 06 '12 at 07:09
  • ya. I want to do some manipulation on Directory by treating as file. – Nimit Mar 06 '12 at 07:11
  • A directory already is a file. What do you really want to do? – Duck Mar 06 '12 at 07:33
  • Programatically, I am not sure, but there is something called debugfs that you can check. I have just read about it and haven't used it myself but there is a discussion around it here -> http://superuser.com/questions/153147/access-file-by-its-id-and-change-file-attributes. It is used usually to examine and change the states of ext* filesystem types. And yes like Duck said, a directory is a file. So what is it that you would achieve by changing the attribute in the inode? – Gargi Srinivas Mar 06 '12 at 07:38
  • @Nimit: exactly what manipulation do you want to do to the directory that you cannot? This question is vague at best, unanswerable at worst. – tbert Mar 06 '12 at 07:57
  • Do you just want to change the data in memory, or on disk? In general though, changing inode types is a very bad idea! And probably very hard since the underlying filesystem may not even _have_ inodes. – Some programmer dude Mar 06 '12 at 07:58
  • @JoachimPileborg: I want to change data in disk only. – Nimit Mar 06 '12 at 09:14

1 Answers1

1

To open any file, you have to use open system call. But currently open system call doesn't allow anyone to open a directory for writing. In case you call open with a directory for writing, it will return error (-1) and set errno to EISDIR.

Still you want to do it, you have to re-implement the open system call of Linux filesystem driver.

theB
  • 2,048
  • 2
  • 20
  • 29
  • Is there any way to convert directory into file ? – Nimit Mar 06 '12 at 09:13
  • 2
    I don't think there is such an option. but you can just copy the content to a new file and can replace the existing directory with the newly created file. – theB Mar 06 '12 at 10:04