Ch03: File Management

Source: Internet
Author: User
Tags lstat

Third, the file management mode, the conventional file pipeline (IPC): a mechanism for inter-process communication in the Linux system, FIFO files, there are two pipelines: an unnamed MPs queue and a named MPs queue directory file: Device Files are operated in the form of files. Most physical devices are represented by files. There are two Device Files: block device and character device symbol connection: contains a special path to the definition file, similar to the shortcut socket in Windows: The mechanism of inter-process communication basic file operations inode (information node) kernel information node (In-core inode ): the kernel information nodes of different types of files are the same disk information nodes (on-disk inode): different file system types have different disk information nodes. When a process opens files on the disk, the disk information node is converted to the kernel information node. If the kernel information node is modified, the node information is returned to disk information node 1. file mode: The file mode defines a minimum of 12 bits, indicating access permissions. The modifier used to manage access to files and file permissions is written into six Octal numbers. Example: 041777 100755 File Access Limited to 3 access control bits. chmod example-rwx ------ 1 Root 218 May 22 20:49 forkd. c [root @ localhost ch02] # chmod 777 forkd. c-rwxrwxrwx 1 Root 218 May 22 20:49 forkd. the C File Permission modifier and type are a bitmask, indicating the setuid, setgid, and stiky file type sys/STAT. umask # include <sys/STAT. h> int umask (INT newmask ); example [root @ localhost TMP] # Touch ex1 [root @ localhost TMP] # ls-L-RW-r -- 1 Root 0 May 22 21:56 ex1 [root @ localhost TMP] # umask 077 [root @ l Ocalhost TMP] # Touch ex2-rw-rw-r -- 1 Root 0 May 22 21:56 ex1-rw ------- 1 Root 0 May 22 21:58 ex2 2. basic file operation file descriptor stdin, stdout, stderr # include <unistd. h> stdin_fileno, stdout_fileno, stderr_fileno operation file parameters close file int close (int fd); open file: # include <fcntl. h> int open (char * pathname, int flags, mode_t mode); int create (char * pathname, mode_t mode); flags: o_rdonly, o_rdwr, o_wronly, o_create, o_ex CL, o_noctty, o_trunc, o_append, o_nonblock, o_sync open (pathname, o_creat | o_wronly | o_trunc, mode) file read/write and position pointer movement # include <unistd. h> size_t read (int fd, void * Buf, size_t length); size_t write (int fd, const void * Buf, size_t length ); example // Write File # include <errno. h> # include <fcntl. h> # include <stdio. h> # include <stdlib. h> # include <unistd. h> int main (void) {int FD; If (FD = open ("rwexm", o_trunc | o_creat | o_wron Ly, 0644) <0) {perror ("open"); _ exit (1);} If (write (FD, "Hello World/N", 13 )! = 13) {perror ("write"); _ exit (1) ;}close (FD); Return 0 ;}// read the file # include <errno. h> # include <fcntl. h> # include <stdio. h> # include <stdlib. h> # include <unistd. h> int main (void) {int size = 13; char buffer [size]; int FD; If (FD = open ("rwexm", o_rdonly, 0644 )) <0) {perror ("open"); _ exit (1) ;}if (read (FD, buffer, sizeof (buffer) <0) {perror ("read"); _ exit (1);} printf (buffer); close (FD); Return 0;} int Lseek (int fd, off_t offset, int whence); seek_set, seek_cur, seek_end local read/write instance simulation implementation CAT system command # include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <errno. h> # include <fcntl. h> int main (INT argc, char ** argv) {int FD, Len; char Buf [1024]; If (argc <2) {printf ("at least 1 Para/N"); Return 0;} If (FD = open (argv [1], o_rdonly, 0666) <0) {perror ("open"); _ exit (1);} while (LEN = read (FD, bu F, sizeof (BUF)> 0) {If (write (1, Buf, Len )! = Len) {perror ("write"); close (FD); _ exit (1) ;}} if (LEN <0) {perror ("read "); close (FD); _ exit (1);} Close (FD); Return 0;} // shorten the int truncate (const char * pathname, size_t length) of the file ); int ftruncate (int fd, size_t length); // synchronization: After this function is executed, data is immediately written back to the hardware int fsync (int fd) int fdatasync (int fd) query and modify inode Information Query inode information # include <sys/STAT. h> int Stat (const char * pathname, struct stat * statbuf); int lstat (const Char * pathname, struct stat * statbuf); int fstat (int fd, struct stat * statbuf ); struct stat member dev_t st_dev unsigned long st_block ino_t st_ino time_t 1_st_mode time_t 1_1_time_t 1_st_uid unsigned long 1_1_1_dev_t 1_off_t st_size example # include <errno. h> # include <stdio. h> # include <string. h> # include <sys/STAT. h> # include <sys/sysmacr OS. h> # include <time. h> # include <unistd. h> # define time_string_buf 50 char * timestring (time_t T, char * BUF) {struct TM * local; Local = localtime (& T); strftime (BUF, time_string_buf, "% C", local); Return Buf;} int statfile (const char * file) {struct stat statbuf; char timebuf [time_string_buf]; If (lstat (file, & statbuf) {fprintf (stderr, "cocould not lstat % s: % s/n", file, strerror (errno); return 1;} printf ("Filename: % s/n", file); printf ("On Device: Major % d/minor % d inod number: % LD/N", Major (statbuf. st_dev), minor (statbuf. st_dev), statbuf. st_ino); printf ("Size: % x-10ld type: % 07o permissions % 05o/N", statbuf. st_size, statbuf. st_mode & s_ifmt, statbuf. st_mode &~ (S_ifmt); printf ("Owner: % d group: % d number of links: % d/N", statbuf. st_uid, statbuf. st_gid, statbuf. st_nlink); printf ("chnge time: % s/n", timestring (statbuf. st_ctime, timebuf); printf ("modified time: % s/n", timestring (statbuf. st_mtime, timebuf); printf ("Access time: % s/n", timestring (statbuf. st_atime, timebuf); Return 0;} int main (INT argc, const char ** argv) {int I; int rc = 0; for (I = 1; I <argc; I ++ ){ RC | = statfile (argv [I]); If (argc-I)> 1) {printf ("/N") ;}} return RC ;} # include <unistd. h> int access (const char * pathname, int mode); 0 is returned correctly, and eaccess # include <sys/STAT is returned if an error occurs. h> int chmod (const char * pathname, mode_t mode); int fchmod (int fd, mode _ T mode); eperm # include <unistd. h> int chown (const char * pathname, uid_t owner, gid_t group); int fchown (int fd, uid_t owner, gid_t group); # include <utim E. h> int utime (const char * pathname, strcut utimbuf * BUF); # include <sys/time. h> int utimes (const char * pathname, struct timeval * TVP); struct utimbuf {time_t actime; time_t modtime;} struct timeval {long TV _sec; long TV _usec ;} ext3 extension attribute ext3_append_flext3_immutable_flext3_nodump_flext3_sync_fl # include <sys/IOCTL. h ># include <Linux/ext3_fs.h> int IOCTL (int fd, int request, void * Arg); ext3_ioc_getflags, ext3 _ Ioc_setflags: Operation directory item # include <fcntl. h> # include <unistd. h> int mknod (const char * pathname, mode_t mode, dev_t Dev); s_ififo, s_ifblk, s_ifchr <sys/sysmacros. h> makedev (Major, minor), major (), minor ()/* Create a hard link */# include <unistd. h> int Link (const char * origpath, const char * newpath);/* Create a symbolic connection */# include <unistd. h> int sysmlink (const char * origpath, const char * newpath); int readlink (const char * pathname, Cha R * Buf, size_t bufsize); chown (), lstat (), readlink (), rename (), unlink ()/* delete file */# include <unistd. h> int unlink (char * pathname); int Rename (const char * oldpath, const char * newpath); Operation file descriptor and create unknown pipeline # include <fcntl. h> int fcntl (int fd, int command, long Arg); fcntl (FD, f_setfl, fcntl (FD, f_getfl, 0) | o_rdonly); fcntl (FD, f_setfl, fcntl (FD, f_getfl, 0) | o_append); fcntl (FD, f_setfl, fcntl (FD, f_getfl, 0) &~ O_append);/* copy file descriptor */int dup (INT oldfd); int dup2 (INT oldfd, int newfd); int pipe (int fds [2]); example # include <errno. h> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <sys/STAT. h> # include <sys/sysmacros. h> # include <unistd. h> # include <fcntl. h> void usage (void) {fprintf (stderr, "Usage: mkmmodexa <path> [B | c | n | p] <major> <minor>/N "); _ exit (1);} int main (INT argc, char ** argv) {int major = 0, Minor = 0; const char * path; int mode = 0666; char * end; int ARGs; If (argc <3) {usage ();} path = argv [1]; If (! Strcmp (argv [2], "B") {mode | = s_ifblk; ARGs = 5;} else if (! Strcmp (argv [2], "C") |! Strcmp (argv [2], "M") {mode | = s_ifchr; ARGs = 5;} else if (! Strcmp (argv [2], "P") {mode | = s_ififo; ARGs = 3;} else {sprintf (stderr, "unknown node type % s/n ", argv [2]); return 1 ;}if (ARGs = 5) {major = strtol (argv [3], & End, 0); If (* end) {fprintf (stderr, "Bad major Number % s/n", argv [3]); return 1 ;}minor = strtol (argv [4], & End, 0 ); if (* End) {fprintf (stderr, "Bad major Number % s/n", argv [4]); return 1 ;}} if (mknod (path, mode, makedev (Major, minor) {fprintf (stderr, "mkmod failed: % s/n", strerror (errno); return 1;} return 0 ;}

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.