2012-03-06 5 views
1

디렉토리를 파일로 변경하고 싶습니다. 몇 가지 연구를했습니다. Linux에서 inode 구조는 파일과 디렉토리에 대한 메타 데이터를 저장합니다. 파일 보호 모드를 디렉토리에서 파일로 변경하고 싶습니다.C 프로그램에서 inode 구조를 사용하여 Linux에서 파일을 디렉토리로 수정

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 구조를 수정 하는가?

+3

파일을 삭제 한 다음 디렉토리를 만드는 특별한 이유가 있습니까? – Corbin

+0

예. 파일로 취급하여 디렉토리에 대한 조작을하고 싶습니다. – Nimit

+0

디렉토리가 이미 파일입니다. 당신은 정말로 무엇을하고 싶습니까? – Duck

답변

1

파일을 열려면 open system call을 사용해야합니다. 하지만 현재 오픈 시스템 콜은 누군가가 글쓰기를 위해 디렉토리를 열도록 허용하지 않습니다. 쓰기 위해 디렉토리로 open을 호출하면 에러 (-1)를 반환하고 errno를 EISDIR로 설정합니다.

아직도 당신이 원한다면 리눅스 파일 시스템 드라이버의 오픈 시스템 호출을 다시 구현해야합니다.

+0

디렉토리를 파일로 변환 할 수있는 방법이 있습니까? – Nimit

+2

그런 옵션이 없다고 생각합니다. 컨텐츠를 새 파일에 복사하고 기존 디렉토리를 새로 작성된 파일로 바꿀 수 있습니다. – theB