Linux System Programming Learning Notes (eight) file and directory management

Source: Internet
Author: User
Tags lstat

1. Files and meta-data

each file is referenced through the inode, each Inode index node has an inode number that is unique in the file system an inode index node is a physical object stored on disk media on a Linux file system, and is the metadata of the entity Inode store associated files that the Linux kernel represents through the data structure ls-i Command gets the inode number of the file
/*obtaining the metadata of a file*/#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>intStat (Const Char*path,structStat *buf);intFstat (intFdstructStat *buf);intLstat (Const Char*path,structStat *buf);

Note: The Lstat function can get the symbolic link's file metadata, Lstat () returns information about the link itself and not the target file

structStat {dev_t St_dev; /*ID of device containing file*/ino_t St_ino; /*inode Number*/mode_t St_mode; /*Permissions*/nlink_t St_nlink; /*Number of hard links*/uid_t St_uid; /*User ID of owner*/gid_t St_gid; /*group ID of owner*/dev_t St_rdev; /*Device ID (if special file)*/off_t st_size; /*Total size in bytes*/blksize_t st_blksize; /*BlockSize for filesystem I/O*/blkcnt_t st_blocks; /*Number of blocks allocated*/time_t st_atime; /*Last access time*/time_t st_mtime; /*Last modification time*/time_t st_ctime; /*Last status change Time*/};

2. Catalog A directory contains a list of filenames, each of the which maps to an inode number. Each name was called a directory entry, and each name-to-inode mapping is called a link when you open a file in a given directory, the kernel queries the appropriate inode number, and then the Inode is compiled File system, the file system uses Inode numbers to find the corresponding files stored on the physical media Reading a Directory ' s Contents:

/**/<sys/types.h> <dirent.h>DIR * opendir (const  Char *name);

/**/<sys/types.h> <dirent.h>struct dirent *  Readdir (DIR *dir);

/**/<sys/types.h><dirent.h>int closedir (DIR *dir);

/** find_file_in_dir-searches the directory ' path ' for a * file named ' file '. * Returns 0 if ' file ' exists in ' path ' and a nonzero * value otherwise. */intFind_file_in_dir (Const Char*path,Const Char*file) {        structDirent *entry; intRET =1; DIR*dir; Dir=opendir (path); errno=0;  while((Entry = Readdir (dir))! =NULL) {                if(strcmp (entry->d_name, file) = =0) {ret=0;  Break; }        }        if(errno &&!)entry) perror ("Readdir");        Closedir (dir); returnret;}

3. Link each name-to-inode mapping in a directory is called a link. Most files had a link count of 1,that is, they was pointed at by a single directory entry when a file's link count is reduced to 0 o'clock, the file is marked free if there is a process Still using this file, the file will remain in the file system in the Linux kernel using a link count and a usage count implementation, a file onlyits link count and usage count are 0 o'clockTo remove the hard link from the file system:
/**/<unistd.h>int link (constcharconst Char *newpath);
After successful invocation, OldPath and NewPath refer to the same file symbolic link (soft link): Soft link can cross-file system, link to any file
/**/<unistd.h>int symlink (constcharconst  Char *newpath);

Solution Chain:

#include <unistd.h>int unlink (constchar *pathname);
Once no process has the file open, it is deleted 4. Copying and Moving files UNIX does not provide system calls that can replicate files and directories directly Copying a file src to a file named DST:
1 ). Open src. 2 if if it does exist. 3 ). Read a chunk of SRC into memory. 4 ). Write the chunk to DST. 5 ). Continue until all of the SRC have been read and written to DST. 6 ). Close DST. 7). Close src.
5. Block device The null device lives at/dev/null. The kernel silently discards all write requests to the device. All read requests to the file return End-of-file (EOF).  The zero device lives at/dev/zero.  Read this device to return a null character, write this device is discarded by the full device lives at/dev/full. Reading this device returns a null character, and writing this device will trigger an error indicating that the device is full of the kernel ' s random number generators live at/dev/random.

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.