C language Implementation pwd-about Linux file system

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>#include<dirent.h>#include<sys/types.h>#include<sys/stat.h>#include<string.h>#include<unistd.h>/*    Dir *opendir (const char *pathname), which is the open file directory, returns a pointer to the dir struct __dirstream {void *__fd;    Char *__data;            implement int __entry_data;            Char *__ptr;            int __entry_ptr;            size_t __allocation;            size_t __size;     __libc_lock_define (, __lock)};      typedef struct __DIRSTREAM DIR;    int stat (const char *file_name, struct stat *buf); is to get the details of the file named D_name, which is stored in the stat structure.       The following is the definition of the stat structure: struct stat {mode_t st_mode;       File access rights ino_t St_ino;        Index node number dev_t St_dev;       The file uses the device number dev_t St_rdev;      Device file device number nlink_t St_nlink;        The number of hard connections of the file uid_t St_uid;        Owner user identification number gid_t St_gid;       Group identification number off_t st_size;      The file capacity in bytes time_t st_atime;      Time of last access to the file time_t st_mtime;  The last time the file was modified time_t st_ctime;    The last time the file state was changed blksize_t st_blksize;     The size of the disk block containing the file blkcnt_t st_blocks;   The disk block that the file occupies};*//*get files by file name Inode-number*/ino_t Get_ino_byname (Char*filename) {    structstat File_stat; if(0! = stat (filename, &File_stat)) {Perror ("Stat"); Exit (-1); }    returnFile_stat.st_ino;}/*according to Inode-number, find the corresponding file name in the current directory*/Char*Find_name_byino (ino_t ino) {DIR*DP =NULL; structDirent *dptr =NULL; Char*filename =NULL; if(NULL = = (DP = Opendir (".")) {fprintf (stderr,"Can not open current directory\n"); Exit (-1); } Else {         while(NULL! = (Dptr =Readdir (DP))) {            if(Dptr->d_ino = =ino) {filename= StrDup (dptr->d_name);  Break;    }} closedir (DP); }    returnfilename;}/*limit the maximum directory depth*/#defineMax_dir_depth (256)intMainintargcChar*argv[]) {    /*stack of record directory names*/    Char*Dir_stack[max_dir_depth]; unsigned current_depth=0;  for(;;) {        /*1. Through a special filename "." Gets the inode-number of the current directory*/ino_t Current_ino= Get_ino_byname ("."); /*2. Through a special filename "..." Gets the inode-number of the parent directory of the current directory*/ino_t Parent_ino= Get_ino_byname (".."); /*3. Determine whether the current directory and the parent directory are inode-number the same*/        if(Current_ino = =Parent_ino) Break;/*4. If two inode-number the same as the arrival root directory*/        /*5. If two inode-number are different*/        /*switch to the parent directory, search for the corresponding file name in the parent directory, and log it back to step 1, based on the Inode-number obtained in step 1 .*/ChDir (".."); Dir_stack[current_depth++] =Find_name_byino (Current_ino); if(current_depth>=max_dir_depth) {/*path name too deep*/fprintf (stderr,"Directory tree is too deep.\n"); Exit (-1); }    }    /*output full path name*/    inti = current_depth-1;  for(i = current_depth-1; i>=0; i--) {fprintf (stdout,"/%s", Dir_stack[i]); } fprintf (stdout,"%s\n", current_depth==0?"/":""); return 0;}

In a filesystem on Linux, File =n (n>=1) inode +m (m>=1) data blocks.

Data blocks, which hold the contents of the file, and the number of data blocks depends on the size of the file content.

The inode is called the information node, which has two functions: 1, storing property information related to the file, such as modification time, owner, file type and file length, note that there is no file name in this information, 2. Stores pointer information pointing to the file content data block.

In a file system, an inode represents a file and uses an integer value to flag the inode, called Inode-number, which is unique to a file system where its corresponding inode can be found. Typically, a file has only one inode information to describe it.

C language Implementation pwd-about Linux file system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.