{"id":959,"date":"2021-12-19T12:07:27","date_gmt":"2021-12-19T04:07:27","guid":{"rendered":"https:\/\/blog.cauchyschwarz.com\/?p=959"},"modified":"2021-12-19T12:09:38","modified_gmt":"2021-12-19T04:09:38","slug":"apiue%e8%af%bb%e4%b9%a6%e7%ac%94%e8%ae%b0chapter4","status":"publish","type":"post","link":"https:\/\/blog.cauchyschwarz.com\/?p=959","title":{"rendered":"APIUE\u8bfb\u4e66\u7b14\u8bb0chapter4"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fc4587f053a\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fc4587f053a\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#stat%E3%80%81fstat%E3%80%81fstat%E5%92%8Clstat%E5%87%BD%E6%95%B0\" >stat\u3001fstat\u3001fstat\u548clstat\u51fd\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#File_Types\" >File Types<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#Set-User-ID%E5%92%8CSet-Group-ID\" >Set-User-ID\u548cSet-Group-ID<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#File_Access_Permissions\" >File Access Permissions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#Ownership_of_New_Files_and_Directories\" >Ownership of New Files and Directories<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#access%E4%B8%8Efaccessat%E5%87%BD%E6%95%B0\" >access\u4e0efaccessat\u51fd\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#umask%E5%87%BD%E6%95%B0%E8%AE%BE%E7%BD%AE%E6%8E%A9%E7%A0%81\" >umask\u51fd\u6570(\u8bbe\u7f6e\u63a9\u7801)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#chmod%E3%80%81fchmod%E3%80%81fchmodat%E5%87%BD%E6%95%B0\" >chmod\u3001fchmod\u3001fchmodat\u51fd\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#Sticky_Bit\" >Sticky Bit<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#chown%E3%80%81fchown%E3%80%81fchownat%E5%92%8Clchown%E5%87%BD%E6%95%B0\" >chown\u3001fchown\u3001fchownat\u548clchown\u51fd\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#File_Size\" >File Size<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#File_Truncation\" >File Truncation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#File_Systems\" >File Systems<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#linklinkatunlinkunlinkat%E5%92%8Cremove%E5%87%BD%E6%95%B0\" >link,linkat,unlink,unlinkat\u548cremove\u51fd\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#rename%E5%92%8Crenameat%E5%87%BD%E6%95%B0\" >rename\u548crenameat\u51fd\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#Symbolic_Links\" >Symbolic Links<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#Creating_and_Reading_Symbol_Links\" >Creating and Reading Symbol Links<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#File_Times\" >File Times<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#futimens_utimensat_and_utimes_functions\" >futimens, utimensat, and utimes functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#mkdirmkdirat_and_rmdir_functions\" >mkdir,mkdirat and rmdir functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#Reading_Directories\" >Reading Directories<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#chdir%E3%80%81fchdir%E3%80%81getcwd%E5%87%BD%E6%95%B0\" >chdir\u3001fchdir\u3001getcwd\u51fd\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#Device_Special_Files\" >Device Special Files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=959\/#Summary_of_File_Access_Permission_Bits\" >Summary of File Access Permission Bits<\/a><\/li><\/ul><\/nav><\/div>\n<h1 class=\"wp-block-heading\" id=\"statfstatfstat%E5%92%8Clstat%E5%87%BD%E6%95%B0\"><span class=\"ez-toc-section\" id=\"stat%E3%80%81fstat%E3%80%81fstat%E5%92%8Clstat%E5%87%BD%E6%95%B0\"><\/span>stat\u3001fstat\u3001fstat\u548clstat\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;sys\/stat.h&gt;\n\nint stat(const char *restrict pathname, struct stat *restrict buf);\n\nint fstat(int fd, struct stat *buf);\n\nint lstat(const char *restrict pathname, struct stat *restrict buf);\n\nint fstatat(int fd, const char *restrict pathname,\n\t\tstruct stat *restrict buf, int flag);\n\nstruct stat {\n\tmode_t \tst_mode;\t<em>\/* file type &amp; mode (permissions) *\/<\/em>\n\tino_t\tst_ino;\t\t<em>\/* i-node number (serial number) *\/<\/em>\n\tdev_t\tst_dev;\t\t<em>\/* device number (file system) *\/<\/em>\n\tdev_t \tst_rdev;\t<em>\/* device number for special files *\/<\/em>\n\tnlink_t\tst_nlink;\t<em>\/* number of links *\/<\/em>\n\tuid_t \tst_uid;\t\t<em>\/* user ID of owner *\/<\/em>\n\tgid_t\tst_gid;\t\t<em>\/* group ID of owner *\/<\/em>\n\toff_t\tst_size;\t<em>\/* size in bytes, for regular files *\/<\/em>\n\tstruct timespec st_atim;\t<em>\/* time of last access *\/<\/em>\n\tstruct timespec st_mtim;\t<em>\/* time of last modification *\/<\/em>\n\tstruct timespec st_ctim;\t<em>\/* time of last file status change *\/<\/em>\n\tblksize_t\tst_blksize;\t<em>\/* best I\/O block size *\/<\/em>\n\tblkcnt_t\tst_blocks;\t<em>\/* number of disk blocks allocated *\/<\/em>\n};\n<\/code><\/pre>\n\n\n\n<p>stat\u8fd4\u56de\u6307\u5b9a\u6587\u4ef6\u7684\u4fe1\u606f\uff0cfstat\u8fd4\u56de\u6587\u4ef6\u63cf\u8ff0\u7b26\u6307\u5411\u7684\u6587\u4ef6\u7684\u4fe1\u606f\uff0clstat\u4e0estat\u7c7b\u4f3c\uff0c\u4f46\u662f\u5728\u9047\u5230\u7b26\u53f7\u94fe\u63a5\u6587\u4ef6\u65f6\uff0clstat\u4e0d\u4f1a\u53bb\u627e\u7b26\u53f7\u94fe\u63a5\u5f15\u7528\u7684\u6587\u4ef6\uff0c\u800c\u662f\u76f4\u63a5\u8fd4\u56de\u8be5\u7b26\u53f7\u94fe\u63a5\u7684\u4fe1\u606f\u3002fstatat\u51fd\u6570\u5219\u5728fd\u4ee3\u8868\u7684\u76ee\u5f55\u4e0b\u53bb\u627epathname\u4ee3\u8868\u7684\u6587\u4ef6(\u5982\u679cpathname\u4e3a\u7edd\u5bf9\u8def\u5f84\u5c31\u5ffd\u7565fd\uff0c\u5982\u679cflag\u5e26\u4e86AT_FDCWD\u6807\u5fd7\uff0c\u5219\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u627epathname)\uff0cflag\u53c2\u6570\u5219\u5305\u542b\u4e86\u662f\u5426\u8981follow symbol link\u7684\u9009\u9879(\u9ed8\u8ba4\u662f\u8981follow symbol link\u7684)\u3002<\/p>\n\n\n\n<p>timespec\u81f3\u5c11\u6709\u4e24\u4e2a\u57df\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">time_t tv_sec;\nlong tv_nsec;\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"file-types\"><span class=\"ez-toc-section\" id=\"File_Types\"><\/span>File Types<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<ol class=\"wp-block-list\"><li>Regular file.<\/li><li>Directory file.<\/li><li>Block special file. A type of file providing buffered I\/O access in fixed-size units to devices such as disk drivers.<\/li><li>Character special file. A type of file providing unbuffered I\/O access in variable-sized units to devices. All devices on a system are either block special files or character special files.<\/li><li>FIFO(named pipe).<\/li><li>Socket.<\/li><li>Symbol link. A type of file that points to another file.<\/li><\/ol>\n\n\n\n<p>\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\u9700\u8981\u7528st_mode\u6210\u5458\u53d8\u91cf\u548c\u4e0b\u9762\u7684\u5b8f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Macro<\/strong><\/th><th><strong>Type of file<\/strong><\/th><\/tr><\/thead><tbody><tr><td>S_ISREG()<\/td><td>regular file<\/td><\/tr><tr><td>S_ISDIR()<\/td><td>directory file<\/td><\/tr><tr><td>S_ISCHR()<\/td><td>character special file<\/td><\/tr><tr><td>S_ISBLK()<\/td><td>block special file<\/td><\/tr><tr><td>S_ISFIFO()<\/td><td>pipe or FIFO<\/td><\/tr><tr><td>S_LINK()<\/td><td>symbol link<\/td><\/tr><tr><td>S_ISSOCK()<\/td><td>socket<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7531\u4e8ePOSIX.1\u5141\u8bb8IPC\u5bf9\u8c61(\u5982message queue\u3001semaphore\u7b49)\u7528\u6587\u4ef6\u7684\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u5b8f\u5224\u65ad\u6587\u4ef6\u662f\u5426\u662fIPC\u5bf9\u8c61\uff0c\u53ea\u662f\u4e0b\u9762\u7684\u5b8f\u4f20\u5165\u53c2\u6570\u662fstat\u7ed3\u6784\u4f53\u6307\u9488\u800c\u4e0d\u662fst_mode\u6210\u5458\u53d8\u91cf\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Macro<\/strong><\/th><th><strong>Type of object<\/strong><\/th><\/tr><\/thead><tbody><tr><td>S_TYPEISMQ()<\/td><td>message queue<\/td><\/tr><tr><td>S_TYPEISSEM()<\/td><td>semaphore<\/td><\/tr><tr><td>S_TYPEISSHM()<\/td><td>shared memory object<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u65e9\u8d77UNIX\u7cfb\u7edf\u6ca1\u6709\u63d0\u4f9bS_ISxxx\u8fd9\u6837\u7684\u5b8f\uff0c\u800c\u4f7f\u7528st_mode\u4e0eS_IFMT\u76f8\u4e0e\u7684\u7ed3\u679c\u53bb\u4e0eS_IFxxx\u7684\u5e38\u91cf\u503c\u8fdb\u884c\u6bd4\u8f83\u6765\u5224\u65ad\u6587\u4ef6\u7684\u7c7b\u578b\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"set-user-id%E5%92%8Cset-group-id\"><span class=\"ez-toc-section\" id=\"Set-User-ID%E5%92%8CSet-Group-ID\"><\/span>Set-User-ID\u548cSet-Group-ID<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>\u6bcf\u4e2a\u8fdb\u7a0b\u81f3\u5c11\u67096\u4e2a\u5173\u8054\u7684ID\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>id type<\/th><th>explanation<\/th><\/tr><\/thead><tbody><tr><td>real user ID<\/td><td>who we really are<\/td><\/tr><tr><td>real group ID<\/td><td>~<\/td><\/tr><tr><td>effective user ID<\/td><td>used for file access permission checks<\/td><\/tr><tr><td>effective group ID<\/td><td>~<\/td><\/tr><tr><td>supplementary group IDs<\/td><td>~<\/td><\/tr><tr><td>saved set-user-ID<\/td><td>saved by exec functions<\/td><\/tr><tr><td>saved set-group-ID<\/td><td>~<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6267\u884c\u4e00\u4e2a\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u65b0\u8d77\u7684\u8fdb\u7a0b\u7684effective user ID\u548ceffective group ID\u7ecf\u5e38\u5c31\u662freal user ID\u548creal group ID\u3002\u4f46\u662f\u5982\u679c\u8be5\u7a0b\u5e8f\u6587\u4ef6\u7684st_mode\u91cc\u8bbe\u7f6e\u4e86set-user-ID\u4f4d\u548cset-group-ID\u4f4d\uff0c\u90a3\u4e48\u65b0\u8d77\u7684\u8fdb\u7a0b\u5c31\u4f1a\u628aeffective user ID\u8bbe\u4e3a\u6587\u4ef6\u7684owner\uff0c\u628aeffective group ID\u8bbe\u4e3a\u6587\u4ef6\u7684group owner\u3002<br>\u68c0\u6d4bst_mode\u91cc\u7684set-user-ID\u4f4d\u548cset-group-ID\u4f4d\u53ef\u4ee5\u7528S_ISUID\u5b8f\u548cS_ISGID\u5b8f\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"file-access-permissions\"><span class=\"ez-toc-section\" id=\"File_Access_Permissions\"><\/span>File Access Permissions<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p><code>&lt;sys\/stat.h&gt;<\/code>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>st_mode mask<\/strong><\/th><th><strong>Meaning<\/strong><\/th><\/tr><\/thead><tbody><tr><td>S_IRUSR<\/td><td>user-read<\/td><\/tr><tr><td>S_IWUSR<\/td><td>user-write<\/td><\/tr><tr><td>S_IXUSR<\/td><td>user-execute<\/td><\/tr><tr><td>:&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/td><td>:&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/td><\/tr><tr><td>S_IRGRP<\/td><td>group-read<\/td><\/tr><tr><td>S_IWGRP<\/td><td>group-write<\/td><\/tr><tr><td>S_IXGRP<\/td><td>group-execute<\/td><\/tr><tr><td>:&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/td><td>:&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/td><\/tr><tr><td>S_IROTH<\/td><td>other-read<\/td><\/tr><tr><td>S_IWOTH<\/td><td>other-write<\/td><\/tr><tr><td>S_IXOTH<\/td><td>other-execute<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5173\u4e8e\u6587\u4ef6\u6743\u9650\u7684\u4e00\u4e9b\u89c4\u5219\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4efb\u4f55\u65f6\u5019\u6211\u4eec\u60f3\u8981\u901a\u8fc7\u8def\u5f84\u540d\u6253\u5f00(open)\u67d0\u4e2a\u6587\u4ef6\u65f6\uff0c\u6211\u4eec\u90fd\u9700\u8981\u5177\u6709\u8be5\u8def\u5f84\u540d\u91cc\u63d0\u5230\u7684\u6bcf\u4e2a\u76ee\u5f55\u7684\u6267\u884c\u6743\u9650\u3002\u6240\u4ee5\u76ee\u5f55\u7684execution permission bit\u4e5f\u5e38\u88ab\u79f0\u4f5csearch bit\u3002\u5982\u679c\u662f\u6253\u5f00\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\uff0c\u90a3\u4e48\u4e5f\u540c\u6837\u9700\u8981\u5f53\u524d\u76ee\u5f55\u7684\u6267\u884c\u6743\u9650\u3002<\/li><li>\u4f7f\u7528O_TRUNC\u6807\u5fd7\u9700\u8981\u5199\u6743\u9650\u3002<\/li><li>\u5728\u6307\u5b9a\u76ee\u5f55\u521b\u5efa\u6587\u4ef6\u9700\u8981\u5177\u6709\u8be5\u76ee\u5f55\u7684\u5199\u6743\u9650\u548c\u6267\u884c\u6743\u9650\u3002<\/li><li>\u5220\u9664\u6307\u5b9a\u76ee\u5f55\u91cc\u7684\u67d0\u4e2a\u6587\u4ef6\u9700\u8981\u5177\u6709\u8be5\u76ee\u5f55\u7684\u5199\u6743\u9650\u548c\u6267\u884c\u6743\u9650\u3002\u4e0d\u9700\u8981\u8be5\u6587\u4ef6\u7684\u8bfb\u6743\u9650\u4e5f\u4e0d\u9700\u8981\u8be5\u6587\u4ef6\u7684\u5199\u6743\u9650\u3002<\/li><li>\u5bf9\u4e8e\u60f3\u7528exec\u51fd\u6570\u6267\u884c\u7684\u6587\u4ef6\uff0c\u9700\u8981\u5177\u6709\u8be5\u6587\u4ef6\u7684\u6267\u884c\u6743\u9650\u4ee5\u53ca\u8be5\u6587\u4ef6\u5fc5\u987b\u662fregular file\u3002<\/li><\/ul>\n\n\n\n<p>\u8fdb\u7a0b\u8bbf\u95ee\u6587\u4ef6\u65f6(open,creat,delete)\uff0c\u5224\u5b9a\u6743\u9650\u662f\u5426\u5141\u8bb8\u7684\u89c4\u5219(\u5185\u6838\u6309\u987a\u5e8f\u6267\u884c\u5224\u65ad)\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5982\u679c\u8fdb\u7a0b\u7684effective user ID\u662f0(\u8d85\u7ea7\u7528\u6237)\uff0c\u90a3\u4e48\u5141\u8bb8\u6b64\u6b21file access\u3002<\/li><li>\u5982\u679c\u8fdb\u7a0b\u7684effective user ID\u7b49\u4e8e\u6587\u4ef6\u7684owner ID\uff0c\u90a3\u4e48\u6839\u636e\u8be5\u6587\u4ef6\u4e3aowner\u8bbe\u7f6e\u7684\u6743\u9650\u4f4d\u5224\u65ad\u8be5\u8fdb\u7a0b\u7684\u6b64\u6b21file access\u662f\u5426\u88ab\u5141\u8bb8\uff1b<\/li><li>\u5982\u679c\u8fdb\u7a0b\u7684effective group ID\u6216\u8005\u8fdb\u7a0b\u7684supplementary group ID\u4e4b\u4e00\u7b49\u4e8e\u8be5\u6587\u4ef6\u7684group ID\uff0c\u90a3\u4e48\u6839\u636e\u8be5\u6587\u4ef6\u4e3agroup\u8bbe\u7f6e\u7684\u6743\u9650\u4f4d\u5224\u65ad\u6b64\u6b21file access\u662f\u5426\u88ab\u5141\u8bb8\uff1b<\/li><li>\u5982\u679c\u6709\u5176\u4ed6\u7279\u6b8aaccess bit\u88ab\u8bbe\u7f6e\u4e3a\u5141\u8bb8\u6b64\u6b21file access\uff0c\u90a3\u4e48access is allowed\u3002\u5426\u5219\u8fdb\u7a0b\u7684\u6b64\u6b21\u8bbf\u95ee\u88ab\u62d2\u7edd\u3002<\/li><\/ol>\n\n\n\n<p>\u5176\u4e2d\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u8fdb\u7a0beffective user ID\u6b63\u597d\u7b49\u4e8e\u6587\u4ef6owner ID\uff0c\u90a3\u4e48\u5c31\u53ea\u4f1a\u6309owner\u7684\u6743\u9650\u53bb\u5224\u65ad\u4e0d\u4f1a\u518d\u6309\u7167group\u7684\u6743\u9650\u53bb\u5224\u65ad\u3002\u5982\u679c\u8fdb\u7a0b\u6b63\u597d\u4e0d\u62e5\u6709\u8be5\u6587\u4ef6\u4f46\u662f\u5c5e\u4e8e\u8be5\u6587\u4ef6\u7684\u7528\u6237\u7ec4\uff0c\u90a3\u4e48\u4e5f\u662f\u53ea\u6309\u7167\u8be5\u6587\u4ef6\u7684\u7528\u6237\u7ec4\u6743\u9650\u8fdb\u884c\u5224\u65ad\uff0c\u800c\u4e0d\u518d\u53c2\u8003\u5176\u4ed6\u7684\u6743\u9650\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"ownership-of-new-files-and-directories\"><span class=\"ez-toc-section\" id=\"Ownership_of_New_Files_and_Directories\"><\/span>Ownership of New Files and Directories<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>The user ID of a new file is set to the effective user ID of the process. POSIX.1 allows an implementation to choose one of the following options to determine the group ID of a new file:<\/p><\/blockquote>\n\n\n\n<ol class=\"wp-block-list\"><li>The group ID of a new file can be the effective group ID of the process.<\/li><li>The group ID of a new file can be the group ID of the directory in which the file is being created.<br>It depends on the implementation.<\/li><\/ol>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"access%E4%B8%8Efaccessat%E5%87%BD%E6%95%B0\"><span class=\"ez-toc-section\" id=\"access%E4%B8%8Efaccessat%E5%87%BD%E6%95%B0\"><\/span>access\u4e0efaccessat\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;unistd.h&gt;\n\nint access(const char *pathname, int mode);\n\nint faccessat(int fd, const char *pathname, int mode, int flag);\n<\/code><\/pre>\n\n\n\n<p>\u7528real user ID\u548creal group ID\u8fdb\u884cfile access test\uff0c\u800c\u4e0d\u662f\u4f7f\u7528effective IDs\u3002<\/p>\n\n\n\n<p>mode\u7684\u503c\u53ef\u4ee5\u53d6F_OK(\u7528\u4e8e\u68c0\u6d4b\u4e00\u4e2a\u6587\u4ef6\u662f\u5426\u5b58\u5728)\u6216\u8005\u4e0b\u9762\u7684\u503c\u7684\u6216\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>mode<\/strong><\/th><th><strong>Description<\/strong><\/th><\/tr><\/thead><tbody><tr><td>R_OK<\/td><td>test for read permission<\/td><\/tr><tr><td>W_OK<\/td><td>test for write permission<\/td><\/tr><tr><td>X_OK<\/td><td>test for execute permission<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>flag\u7684\u503c\u7528\u4e8e\u8bbe\u7f6efaccessat\u51fd\u6570\u7684\u884c\u4e3a\u3002\u5982\u679cflag\u8bbe\u4e3aAT_EACCESS\uff0c\u90a3\u4e48\u5c31\u7528effective user ID\u548ceffective group ID\u8fdb\u884cfile access\u7684\u6743\u9650\u68c0\u67e5\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"umask%E5%87%BD%E6%95%B0%E8%AE%BE%E7%BD%AE%E6%8E%A9%E7%A0%81\"><span class=\"ez-toc-section\" id=\"umask%E5%87%BD%E6%95%B0%E8%AE%BE%E7%BD%AE%E6%8E%A9%E7%A0%81\"><\/span>umask\u51fd\u6570(\u8bbe\u7f6e\u63a9\u7801)<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;sys\/stat.h&gt;\n\nmode_t umask(mode_t cmask);\n<\/code><\/pre>\n\n\n\n<p>Any bits that are on in the file mode creation mask are turned off in the file&#8217;s mode.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"chmodfchmodfchmodat%E5%87%BD%E6%95%B0\"><span class=\"ez-toc-section\" id=\"chmod%E3%80%81fchmod%E3%80%81fchmodat%E5%87%BD%E6%95%B0\"><\/span>chmod\u3001fchmod\u3001fchmodat\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;sys\/stat.h&gt;\n\nint chmod(const char *pathname, mode_t mode);\n\nint fchmod(int fd, mode_t mode);\n\nint fchmodat(int fd, const char *pathname, mode_t mode, int flag);\n<\/code><\/pre>\n\n\n\n<p>\u5404\u51fd\u6570\u7684\u610f\u4e49\u57fa\u672c\u53ef\u4ee5\u987e\u540d\u601d\u4e49\uff0c\u61d2\u5f97\u8d58\u8ff0\u3002\u5176\u4e2dfchmodat\u51fd\u6570\u7684flag\u7528\u4e8e\u63a7\u5236\u662f\u5426follow symbol link\uff1a\u53d6\u503cAT_SYMLINK_NOFOLLOW\u65f6\uff0cfchmodat\u4e0d\u4f1afollow symbol link\u3002<br>\u8981\u6539\u53d8\u6587\u4ef6\u7684\u6743\u9650\u4f4d\u9700\u8981\u8fdb\u7a0b\u7684effective user ID\u7b49\u4e8e\u6587\u4ef6\u7684owner ID\u6216\u8005\u8fdb\u7a0b\u62e5\u6709\u8d85\u7ea7\u7528\u6237\u6743\u9650\u3002<br>mode\u7684\u503c\u9664\u4e86\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u4e4b\u524d\u8bb2\u76849\u79cd\u503c\u7684\u6216\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u6216\u4e0aS_IRWXU\u3001S_IRWXG\u3001S_IRWXO\u4ee5\u53caS_ISUID(set-user-ID on execution)\u3001S_ISGID(set-group-ID on execution)\u3001S_ISVTX(saved-text, sticky bit)\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"sticky-bit\"><span class=\"ez-toc-section\" id=\"Sticky_Bit\"><\/span>Sticky Bit<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>Sticky bit\u6216\u8005saved-text bit\uff0cS_ISVTX\uff0c\u5728\u73b0\u4ee3\u7cfb\u7edf\u4e2d\u5982\u679c\u67d0\u4e2a\u76ee\u5f55\u8bbe\u7f6e\u4e86\u8fd9\u4e2a\u6807\u5fd7\uff0c\u90a3\u4e48\u8be5\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u53ea\u6709\u5728\u4ee5\u4e0b\u60c5\u5f62\u4e4b\u4e00\u624d\u53ef\u4ee5\u88ab\u7528\u6237\u5220\u9664\u6216\u8005\u91cd\u547d\u540d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7528\u6237\u62e5\u6709\u8be5\u6587\u4ef6<\/li><li>\u7528\u6237\u62e5\u6709\u8be5\u76ee\u5f55<\/li><li>\u7528\u6237\u662f\u8d85\u7ea7\u7528\u6237<\/li><\/ul>\n\n\n\n<p>\u901a\u5e38\u4f7f\u7528\u8fd9\u4e2a\u6807\u5fd7\u7684\u76ee\u5f55\u6709\/tmp\u548c\/var\/tmp\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"chownfchownfchownat%E5%92%8Clchown%E5%87%BD%E6%95%B0\"><span class=\"ez-toc-section\" id=\"chown%E3%80%81fchown%E3%80%81fchownat%E5%92%8Clchown%E5%87%BD%E6%95%B0\"><\/span>chown\u3001fchown\u3001fchownat\u548clchown\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;unistd.h&gt;\n\nint chown(const char *pathname, uid_t owner, gid_t group);\nint fchown(int fd, uid_t owner, gid_t group);\nint fchownat(int fd, const char *pathname, uid_t owner, gid_t group, int flag);\nint lchown(const char *pathname, uid_t owner, gid_t group);\n<\/code><\/pre>\n\n\n\n<p>\u9664\u4e86lchown\u548cfchownat(\u8bbe\u7f6e\u4e86AT_SYMLINK_NOFOLLOW\u60c5\u51b5\u4e0b)\u662f\u6539\u53d8symbol link\u6587\u4ef6\u672c\u8eab\u800c\u975e\u6307\u5411\u6587\u4ef6\u7684owner\uff0c\u8fd9\u51e0\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u6ca1\u4ec0\u4e48\u5dee\u522b\u3002<br>\u7531\u4e8efchown\u51fd\u6570\u662f\u5bf9\u5df2\u7ecf\u6253\u5f00\u7684\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\uff0c\u56e0\u6b64fchown\u4e0d\u80fd\u6539\u53d8symbol link\u7684owner\uff0c\u800c\u53ea\u80fd\u6539\u53d8symbol link\u6307\u5411\u6587\u4ef6\u7684owner\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"file-size\"><span class=\"ez-toc-section\" id=\"File_Size\"><\/span>File Size<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>stat\u7ed3\u6784\u4f53\u4e2d\u7684st_size\u6210\u5458\u8868\u793a\u6587\u4ef6\u5927\u5c0f\uff0c\u6b64\u6210\u5458\u4ec5\u5bf9\u666e\u901a\u6587\u4ef6\u3001\u76ee\u5f55\u548csymbol link\u6709\u6548\u3002<br>\u5bf9\u4e8esymbol link\uff0cst_size\u8868\u793a\u7684\u662f\u6307\u5411\u6587\u4ef6\u7684\u8def\u5f84\u540d\u957f\u5ea6(\u4e0d\u5305\u542b\u7ed3\u5c3e\u7684null byte)\u3002<br>\u5927\u591a\u6570\u73b0\u4ee3UNIX\u7cfb\u7edf\u5b9e\u73b0\u5728stat\u7ed3\u6784\u4f53\u4e2d\u63d0\u4f9b\u4e86st_blocks\u548cst_blksize\u6210\u5458\uff0c\u7528\u4ee5\u8868\u793a\u4e3a\u6b64\u6587\u4ef6\u5f00\u8f9f\u7684block\u6570\uff0c\u4ee5\u53ca\u7528\u4e8eI\/O\u7684preferred block size\u3002<br>\u7528ls\u547d\u4ee4\u663e\u793a\u7684\u662f\u6587\u4ef6\u5927\u5c0f\uff0c\u800c\u7528du\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u6587\u4ef6\u5b9e\u9645\u5360\u636e\u7684\u78c1\u76d8\u5927\u5c0f\uff0c\u4ece\u800c\u68c0\u9a8c\u6587\u4ef6\u4e2d\u662f\u5426\u5b58\u5728holes\u3002\u7528<code>cat file &gt; newfile<\/code>\u65b9\u5f0f\u751f\u6210\u7684\u65b0\u6587\u4ef6\u4e0d\u4f1a\u6709holes\uff0c\u800c\u7528<code>cp<\/code>\u547d\u4ee4\u5219\u4f1a\u751f\u6210\u548c\u539f\u6587\u4ef6\u4e00\u6837\u5177\u6709holes\u7684\u65b0\u6587\u4ef6\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"file-truncation\"><span class=\"ez-toc-section\" id=\"File_Truncation\"><\/span>File Truncation<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;unistd.h&gt;\n\nint truncate(const char *pathname, off_t length);\n\nint ftruncate(int fd, off_t length);\n<\/code><\/pre>\n\n\n\n<p>\u9664\u4e86open\u65f6\u4f7f\u7528O_TRUNC\u6807\u5fd7\u5c06\u6587\u4ef6\u5168\u90e8\u622a\u65ad\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528truncate\u3001ftruncate\u51fd\u6570\u5c06\u6587\u4ef6\u622a\u65ad\u4e3a\u6307\u5b9a\u957f\u5ea6\uff0c\u800c\u4e14\u7f29\u5c0f\u3001\u6269\u5c55\u64cd\u4f5c\u90fd\u662f\u5141\u8bb8\u7684\u3002\u6269\u5c55\u65f6\u53ef\u80fd\u4f1a\u4ea7\u751fhole\uff0c\u4e14\u5bf9\u6587\u4ef6\u6269\u5c55\u65f6\uff0c\u8d85\u51fa\u6587\u4ef6\u539f\u6709size\u7684\u90e8\u5206\u5728\u8fdb\u884cread\u64cd\u4f5c\u65f6\u90fd\u4f1a\u5f53\u505a0\u8fd4\u56de\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"file-systems\"><span class=\"ez-toc-section\" id=\"File_Systems\"><\/span>File Systems<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"988\" height=\"618\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/apiue_file-system1.png\" alt=\"\" class=\"wp-image-961\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/apiue_file-system1.png 988w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/apiue_file-system1-300x188.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/apiue_file-system1-768x480.png 768w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/apiue_file-system1-670x419.png 670w\" sizes=\"auto, (max-width: 988px) 100vw, 988px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"858\" height=\"444\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/apiue_file-system2.png\" alt=\"\" class=\"wp-image-963\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/apiue_file-system2.png 858w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/apiue_file-system2-300x155.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/apiue_file-system2-768x397.png 768w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/apiue_file-system2-670x347.png 670w\" sizes=\"auto, (max-width: 858px) 100vw, 858px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u591a\u4e2a\u6587\u4ef6\u76ee\u5f55\u9879\u53ef\u4ee5\u6307\u5411\u540c\u4e00\u4e2ainode\uff0cstat\u7ed3\u6784\u4f53\u4e2d\u7684st_nlink\u4fdd\u5b58\u4e86\u8be5inode\u7684link count\u3002<\/li><li>\u5bf9\u4e8esymbol link\uff0c\u5176\u6587\u4ef6\u7c7b\u578b\u4e3aS_IFLNK\u3002\u8be5\u6587\u4ef6\u7684\u5185\u5bb9(data blocks)\u4fdd\u5b58\u7684\u662f\u5176\u6307\u5411\u6587\u4ef6\u7684\u540d\u5b57\u3002<br><strong>\u6240\u4ee5\u8fd9\u91cc\u5c31\u6e05\u695a\u4e86\uff0clink\u6216\u8005\u6240\u8c13\u7684hard link\u662f\u76f4\u63a5\u6307\u5411\u540c\u4e00\u4e2ainode\uff0c\u800csymbol link\u6216\u8005soft link\u5219\u662f\u6709\u81ea\u5df1\u7684inode\uff0c\u53ea\u4e0d\u8fc7inode\u6307\u5411\u7684data blocks\u5b58\u7684\u662f\u8be5symbol link\u6307\u5411\u7684\u6587\u4ef6\u7684\u8def\u5f84\u540d\u4e14\u8be5symbol link\u7684inode\u91cc\u6587\u4ef6\u7c7b\u578b\u7684\u503c\u662fS_IFLINK\uff0c\u901a\u8fc7\u8fd9\u4e2a\u503c\u6765\u544a\u8bc9\u7cfb\u7edf\u5b83\u662f\u4e00\u4e2asymbol link\u3002<\/strong><\/li><li>inode\u4e2d\u5305\u542b\u4e86\u5173\u4e8e\u8be5\u6587\u4ef6\u7684\u5927\u591a\u6570\u4fe1\u606f\uff0cstat\u7ed3\u6784\u4f53\u4e2d\u7684\u4fe1\u606f\u57fa\u672c\u4e0a\u662f\u4eceinode\u4e2d\u83b7\u53d6\u7684\uff0c\u9664\u4e86\u4e24\u9879\u6570\u636e\uff1a\u6587\u4ef6\u540d\u6587\u4ef6\u540d\u548cinode number\u662f\u4ece\u6587\u4ef6\u76ee\u5f55\u9879\u4e2d\u83b7\u5f97\u3002<\/li><li>\u56e0\u4e3a\u6587\u4ef6\u76ee\u5f55\u9879\u4e2d\u7684inode number\u53ea\u80fd\u6307\u5411\u540c\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684inode\uff0c\u6240\u4ee5\u786c\u94fe\u63a5\u4e0d\u80fd\u8de8\u6587\u4ef6\u7cfb\u7edf\u3002<\/li><li>\u91cd\u547d\u540d\u65f6\u53ea\u8981\u6587\u4ef6\u7cfb\u7edf\u4e0d\u6539\u53d8\u5c31\u4e0d\u4f1a\u53d1\u751f\u6570\u636e\u7684\u62f7\u8d1d\uff0c\u53ea\u662f\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6307\u5411\u539finode\u7684\u6587\u4ef6\u76ee\u5f55\u9879\u7136\u540e\u5220\u9664\u539f\u6765\u7684\u6587\u4ef6\u76ee\u5f55\u9879\u5373\u53ef\u3002mv\u547d\u4ee4\u5c31\u91c7\u53d6\u4e86\u8fd9\u79cd\u5de5\u4f5c\u65b9\u5f0f\u3002<\/li><li>\u76ee\u5f55\u7684link count\u81f3\u5c11\u662f\u4e8c\uff0c\u56e0\u4e3a\u76ee\u5f55\u7684\u7236\u76ee\u5f55\u7684directory block\u4e2d\u4f1a\u6709\u4e00\u4e2a\u8be5\u76ee\u5f55\u7684\u76ee\u5f55\u9879\u6307\u5411\u8be5\u76ee\u5f55\u7684inode\uff0c\u7136\u540e\u8be5\u76ee\u5f55\u7684directory block\u4e2d\u4f1a\u6709\u4e00\u4e2a<code>.<\/code>\u76ee\u5f55\u9879\u6307\u5411\u8be5\u76ee\u5f55\u7684inode\u3002\u5982\u679c\u8be5\u76ee\u5f55\u4e2d\u6709\u5b50\u76ee\u5f55\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5b50\u76ee\u5f55\u7684directory blocks\u4e2d\u7684<code>..<\/code>\u76ee\u5f55\u9879\u8fd8\u4f1a\u6307\u5411\u8fd9\u4e2a\u76ee\u5f55\u7684inode\u3002\u7b80\u8a00\u4e4b\uff0c\u4f4d\u4e8e&#8221;\u53f6\u5b50\u8282\u70b9&#8221;\u7684\u76ee\u5f55\u7684link count\u4e3a2\u3002<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"linklinkatunlinkunlinkat%E5%92%8Cremove%E5%87%BD%E6%95%B0\"><span class=\"ez-toc-section\" id=\"linklinkatunlinkunlinkat%E5%92%8Cremove%E5%87%BD%E6%95%B0\"><\/span>link,linkat,unlink,unlinkat\u548cremove\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>\u4e00\u4e2a\u6587\u4ef6\u53ef\u4ee5\u6709\u591a\u4e2a\u76ee\u5f55\u9879\u6307\u5411\u5176i-node\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;unistd.h&gt;\nint link(const char *existingpath, const char *newpath);\n\nint linkat(int efd, const char *existingpath, int nfd, const char *newpath, int flag);\n\nint unlink(const char *pathname);\n\nint unlinkat(int fd, const char *pathname, int flag);\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e9b\u51fd\u6570\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3anewpath\u7684\u65b0\u76ee\u5f55\u9879\u6307\u5411existingpath\u3002\u5982\u679cnewpath\u5df2\u7ecf\u5b58\u5728\u5219\u8fd4\u56de\u9519\u8bef\u3002\u53ea\u4f1a\u521b\u5efanewpath\u4e2d\u7684\u6700\u540e\u4e00\u90e8\u5206(last component)\uff0c\u524d\u9762\u7684\u8def\u5f84\u5fc5\u987b\u662f\u5df2\u7ecf\u5b58\u5728\u7684\u3002<br>\u5982\u679cexistingpath\u662f\u4e00\u4e2a\u7b26\u53f7\u94fe\u63a5\uff0c\u90a3\u4e48flag\u53c2\u6570\u7528\u4e8e\u6307\u793a\u662f\u5426follow symbol link\u3002\u5982\u679cflag\u4e3aAT_SYMLINK_FOLLOW\uff0c\u5219\u6307\u5411existingpath\u6307\u5411\u7684\u6587\u4ef6\uff0c\u5426\u5219\u6307\u5411existingpath\u672c\u8eab\u3002<br>\u65b0\u76ee\u5f55\u9879\u7684\u521b\u5efa\u548c\u6587\u4ef6\u7684link count\u7684\u589e\u52a0\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\u3002<br>\u5927\u591a\u6570\u5b9e\u73b0\u8981\u6c42newpath\u548cexistingpath\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf\u4e2d\u3002\u5982\u679c\u67d0\u4e2a\u5b9e\u73b0\u652f\u6301\u521b\u5efa\u6307\u5411\u76ee\u5f55\u7684\u786c\u94fe\u63a5\uff0c\u90a3\u4e48\u53ea\u6709\u8d85\u7ea7\u7528\u6237\u624d\u80fd\u6709\u6b64\u6743\u9650\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u79cd\u6307\u5411\u76ee\u5f55\u7684\u786c\u94fe\u63a5\u4f1a\u9020\u6210\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u56de\u8def\uff0c\u800c\u5927\u591a\u6570\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u7684\u5de5\u5177\u5904\u7406\u4e0d\u4e86\u8fd9\u79cd\u56de\u8def\u3002<br>\u5f53\u6587\u4ef6\u7684link\u6570\u964d\u52300\u65f6\uff0c\u8be5\u6587\u4ef6\u4f1a\u88ab\u5220\u9664\uff0c\u9664\u975e\u8be5\u6587\u4ef6\u5728\u67d0\u8fdb\u7a0b\u4e2d\u8fd8\u5904\u4e8eopen\u72b6\u6001\u3002\u5f53\u8be5\u6587\u4ef6\u88ab\u5173\u95ed\u65f6\uff0c\u5185\u6838\u5148\u7edf\u8ba1\u6253\u5f00\u6b64\u6587\u4ef6\u7684\u8fdb\u7a0b\u6570\uff0c\u5982\u679c\u4e3a0\u518d\u7edf\u8ba1link\u6570\uff0c\u5982\u679c\u4e5f\u662f0\uff0c\u90a3\u4e48\u8be5\u6587\u4ef6\u5c31\u5c06\u88ab\u5220\u6389\u3002\u6709\u4e9b\u7a0b\u5e8f\u5c31\u5229\u7528\u4e86unlink\u7684\u8fd9\u79cd\u7279\u6027\uff0c\u5148\u7528open\u6216creat\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\u5e76\u6253\u5f00\uff0c\u7136\u540e\u7acb\u9a6cunlink\u8be5\u6587\u4ef6\uff0c\u8fd9\u6837\u521b\u5efa\u7684\u4e34\u65f6\u6587\u4ef6\u4e0d\u4f1a\u88ab\u5220\u9664\uff0c\u4f46\u662f\u5f53\u7a0b\u5e8fclose\u8be5\u6587\u4ef6\u6216\u8005\u7a0b\u5e8f\u7ed3\u675f(\u6b63\u5e38\u7ed3\u675f\u6216\u8005\u5f02\u5e38\u7ec8\u6b62)\u65f6\u7cfb\u7edfclose\u8be5\u6587\u4ef6\u65f6\uff0c\u8be5\u6587\u4ef6\u5c31\u4f1a\u88ab\u81ea\u52a8\u5220\u9664\u3002<br>\u5728\u7528unlinkat\u65f6\uff0cflag\u63d0\u4f9b\u4e86unlink\u6587\u4ef6\u76ee\u5f55\u7684\u9009\u9879\uff0c\u53ea\u8981\u5c06\u5176\u8bbe\u7f6e\u4e3aAT_REMOVEDIR\u3002<br>\u5982\u679cunlink\u63a5\u53d7\u7684pathname\u53c2\u6570\u662f\u4e00\u4e2asymbol link\uff0c\u90a3\u4e48unlink\u53ea\u4f1a\u79fb\u9664\u8be5symbol link\u800c\u4e0d\u662f\u8be5link\u6307\u5411\u7684\u6587\u4ef6\u3002<br>\u5982\u679c\u7cfb\u7edf\u652f\u6301\uff0c\u8d85\u7ea7\u7528\u6237\u53ef\u4ee5unlink\u4e00\u4e2a\u76ee\u5f55\uff0c\u4f46\u662f\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5176\u5b9e\u5e94\u8be5\u4f7f\u7528rmdir\u51fd\u6570\u3002<br>\u4e5f\u53ef\u4ee5\u4f7f\u7528remove\u51fd\u6570\u6765unlink\u4e00\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\u3002\u5bf9\u4e8e\u6587\u4ef6\u800c\u8a00\uff0cremove\u7b49\u4ef7\u4e8eunlink\uff1b\u5bf9\u4e8e\u76ee\u5f55\u800c\u8a00\uff0cremove\u7b49\u4ef7\u4e8ermdir\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;stdio.h&gt;\n\nint remove(const char *pathname);\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"rename%E5%92%8Crenameat%E5%87%BD%E6%95%B0\"><span class=\"ez-toc-section\" id=\"rename%E5%92%8Crenameat%E5%87%BD%E6%95%B0\"><\/span>rename\u548crenameat\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;stdio.h&gt;\nint rename(const char *oldname, const char *newname);\nint renameat(int oldfd, const char *oldname, int newfd, const char *newname);\n<\/code><\/pre>\n\n\n\n<p>\u9700\u8981\u7279\u6b8a\u5904\u7406\u7684\u51e0\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5982\u679coldname\u6307\u5b9a\u7684\u662f\u4e00\u4e2a\u6587\u4ef6\u800c\u4e0d\u662f\u76ee\u5f55\uff0c\u90a3\u4e48\u6211\u4eec\u5728\u91cd\u547d\u540d\u6587\u4ef6\u6216\u8005\u8f6f\u94fe\u63a5\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0bnewname\u4e0d\u80fd\u548c\u5df2\u7ecf\u5b58\u5728\u7684\u76ee\u5f55\u540c\u540d\u3002\u5982\u679cnewname\u548c\u67d0\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u6587\u4ef6\u91cd\u540d\uff0c\u90a3\u4e48\u5148\u5220\u9664\u8be5\u6587\u4ef6\u518d\u5c06oldname\u6587\u4ef6\/\u8f6f\u94fe\u63a5\u91cd\u547d\u540d\u4e3anewname\u6587\u4ef6\/\u8f6f\u94fe\u63a5\u3002\u8fd9\u4e2a\u64cd\u4f5c\u9700\u8981oldname\u548cnewname\u6240\u5904\u7684\u76ee\u5f55\u7684\u5199\u6743\u9650\u3002<\/li><li>\u5982\u679coldname\u662f\u76ee\u5f55\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u662f\u5728\u91cd\u547d\u540d\u76ee\u5f55\u3002\u6b64\u65f6newname\u4e0d\u80fd\u548c\u5df2\u7ecf\u5b58\u5728\u7684\u6587\u4ef6\/\u7b26\u53f7\u94fe\u63a5\u540c\u540d\uff0c\u53ef\u4ee5\u548c\u5df2\u7ecf\u5b58\u5728\u7684\u76ee\u5f55\u540c\u540d\uff0c\u4e14\u8be5\u76ee\u5f55\u5fc5\u987b\u662f\u4e00\u4e2a\u7a7a\u76ee\u5f55\u3002\u5982\u679c\u5b58\u5728\u548cnewname\u540c\u540d\u7684\u7a7a\u76ee\u5f55\uff0c\u90a3\u4e48\u9996\u5148\u5c06\u8be5\u76ee\u5f55\u5220\u53bb\uff0c\u7136\u540e\u5c06oldname\u91cd\u547d\u540d\u4e3anewname\u3002\u53e6\u5916\uff0c\u5f53\u91cd\u547d\u540d\u76ee\u5f55\u65f6\uff0cnewname\u4e0d\u80fd\u4ee5oldname\u4f5c\u4e3a\u8def\u5f84\u524d\u7f00(\u5982\u5c06\/usr\/foo\u91cd\u547d\u540d\u4e3a\/usr\/foo\/testdir\u662f\u5141\u8bb8\u7684)\u3002<\/li><li>\u5982\u679coldname\u6216newname\u662fsymbolic link\uff0c\u90a3\u4e48\u53ea\u6709\u8be5symolic link\u88ab\u91cd\u547d\u540d\u6216\u5220\u6389\uff0c\u548c\u8be5symbolic link\u6307\u5411\u7684\u6587\u4ef6\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002<\/li><li>\u4e0d\u80fd\u91cd\u547d\u540d<code>.<\/code>\u6216<code>..<\/code>\u3002\u66f4\u51c6\u786e\u5730\u8bf4\uff0c\u4e0d\u80fd\u8ba9<code>.<\/code>\u6216<code>..<\/code>\u4f5c\u4e3aoldname\u6216newname\u7684\u6700\u540e\u4e00\u90e8\u5206\u3002<\/li><li>\u5982\u679coldname\u548cnewname\u6307\u5411\u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\u90a3\u4e48\u51fd\u6570\u4ec0\u4e48\u90fd\u4e0d\u505a\u6210\u529f\u8fd4\u56de\u3002<\/li><\/ol>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"symbolic-links\"><span class=\"ez-toc-section\" id=\"Symbolic_Links\"><\/span>Symbolic Links<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>\u8f6f\u94fe\u63a5\u4e0e\u786c\u94fe\u63a5\u7684\u533a\u522b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u786c\u94fe\u63a5\u8981\u6c42\u8be5\u786c\u94fe\u63a5\u548c\u6307\u5411\u7684\u6587\u4ef6\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf<\/li><li>\u53ea\u6709\u8d85\u7ea7\u7528\u6237\u53ef\u4ee5\u521b\u5efa\u6307\u5411\u76ee\u5f55\u7684\u786c\u94fe\u63a5<\/li><\/ul>\n\n\n\n<p>\u5bf9\u4e8e\u662f\u5426follow symbolic link\u7684\u51fd\u6570\u7684\u5206\u7c7b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Function<\/strong><\/th><th><strong>Does not follow symbolic link<\/strong><\/th><th><strong>Follows symbolic link<\/strong><\/th><\/tr><\/thead><tbody><tr><td>access<\/td><td><\/td><td>y<\/td><\/tr><tr><td>chdir<\/td><td><\/td><td>y<\/td><\/tr><tr><td>chmod<\/td><td><\/td><td>y<\/td><\/tr><tr><td>chown<\/td><td><\/td><td>y<\/td><\/tr><tr><td>creat<\/td><td><\/td><td>y<\/td><\/tr><tr><td>exec<\/td><td><\/td><td>y<\/td><\/tr><tr><td>lchown<\/td><td>y<\/td><td><\/td><\/tr><tr><td>link<\/td><td><\/td><td>y<\/td><\/tr><tr><td>lstat<\/td><td>y<\/td><td><\/td><\/tr><tr><td>open<\/td><td><\/td><td>y<\/td><\/tr><tr><td>opendir<\/td><td><\/td><td>y<\/td><\/tr><tr><td>pathconf<\/td><td><\/td><td>y<\/td><\/tr><tr><td>readlink<\/td><td>y<\/td><td><\/td><\/tr><tr><td>remove<\/td><td>y<\/td><td><\/td><\/tr><tr><td>rename<\/td><td>y<\/td><td><\/td><\/tr><tr><td>stat<\/td><td><\/td><td>y<\/td><\/tr><tr><td>truncate<\/td><td><\/td><td>y<\/td><\/tr><tr><td>unlink<\/td><td>y<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>mkdir,mkfifo,mknod\u548crmdir\u51fd\u6570\u4e0d\u80fd\u63a5\u53d7symbol link\u4f5c\u4e3a\u53c2\u6570\u3002<br>\u4e00\u4e2a\u7279\u4f8b\uff1a\u5f53open\u51fd\u6570\u4ee5O_CREAT\u548cO_EXCL\u6807\u5fd7\u88ab\u8c03\u7528\u65f6\uff0c\u5982\u679c\u4f20\u5165\u7684\u8def\u5f84\u540d\u662f\u4e00\u4e2asymbol link\uff0c\u90a3\u4e48open\u4f1a\u5931\u8d25\uff0c\u4e14\u5c06\u9519\u8bef\u7801\u8bbe\u4e3aEEXIST\u3002\u5982\u679c\u4e0d\u8fd9\u6837\u505a\u90a3\u4e48\u7cfb\u7edf\u5c06\u4f1a\u6709\u5b89\u5168\u9690\u60a3\u3002<\/p>\n\n\n\n<p>cat\u547d\u4ee4\u4f1afollow symbol link\u3002ls\u547d\u4ee4\u7684<code>-F<\/code>\u9009\u9879\u4f1a\u4e3asymbol link\u6587\u4ef6\u52a0\u4e0a\u4e00\u4e2a<code>@<\/code>\u7b26\u53f7\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"creating-and-reading-symbol-links\"><span class=\"ez-toc-section\" id=\"Creating_and_Reading_Symbol_Links\"><\/span>Creating and Reading Symbol Links<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;unistd.h&gt;\n\nint symlink(const char *actualpath, const char *sympath);\nint symlink(const char *actualpath, int fd, const char *sympath);\n<\/code><\/pre>\n\n\n\n<p>\u521b\u5efasymbol link\u65f6\uff0c\u4e0d\u9700\u8981actualpath\u5bf9\u5e94\u7684\u6587\u4ef6\u4e00\u5b9a\u5b58\u5728\u3002<\/p>\n\n\n\n<p>\u8bfb\u53d6symbol link\u4fe1\u606f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;unistd.h&gt;\nssize_t readlink(const char* restrict pathname, char *restrict buf, size_t bufsize);\nssize_t readlinkat(int fd, const char* restrict pathname, char *restrict buf, size_t bufsize);\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"file-times\"><span class=\"ez-toc-section\" id=\"File_Times\"><\/span>File Times<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Field<\/strong><\/th><th><strong>Description<\/strong><\/th><th><strong>Example<\/strong><\/th><th><strong>ls(1)option<\/strong><\/th><\/tr><\/thead><tbody><tr><td>st_atim<\/td><td>last-access time of file data<\/td><td>read<\/td><td><code>-u<\/code><\/td><\/tr><tr><td>st_mtim<\/td><td>last-modification time of file data<\/td><td>write<\/td><td>default<\/td><\/tr><tr><td>st_ctim<\/td><td>last-change time of i-node status<\/td><td>chmod, chown<\/td><td><code>-c<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"futimens-utimensat-and-utimes-functions\"><span class=\"ez-toc-section\" id=\"futimens_utimensat_and_utimes_functions\"><\/span>futimens, utimensat, and utimes functions<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>\u4fee\u6539access time\u548cmodification time\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;sys\/stat.h&gt;\n\nint futimeens(int fd, const struct timespec times[2]);\n\nint utimensat(int fd, const char *path, const struct timespec times[2], int flag);\n\n#include &lt;sys\/time.h&gt;\nint utimes(const char *pathname, const struct timeval times[2]);\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;sys\/stat.h&gt;\n\nint futimens(int fd, const struct timespec times[2]);\n\nint utimensat(int fd, const char *path, const struct timespec times[2], int flag);\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"mkdirmkdirat-and-rmdir-functions\"><span class=\"ez-toc-section\" id=\"mkdirmkdirat_and_rmdir_functions\"><\/span>mkdir,mkdirat and rmdir functions<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;sys\/stat.h&gt;\n\nint mkdir(const char *pathname, mode_t mode);\n\nint mkdirat(int fd, const char *pathname, mode_t mode);\n\nint rmdir(const char *pathname);\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"reading-directories\"><span class=\"ez-toc-section\" id=\"Reading_Directories\"><\/span>Reading Directories<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>\u76ee\u5f55\u6587\u4ef6\u672c\u8eab\u53ea\u6709\u5185\u6838\u624d\u53ef\u5199\u5165\u3002\u76ee\u5f55\u6587\u4ef6\u7684\u5199\u6743\u9650\u4f4d\u548c\u6267\u884c\u6743\u9650\u4f4d\u53ea\u662f\u51b3\u5b9a\u4e86\u53ef\u4ee5\u5728\u8be5\u76ee\u5f55\u4e0b\u521b\u5efa\u6587\u4ef6\u548c\u5220\u9664\u6587\u4ef6\uff0c\u4f46\u662f\u8fd9\u4e9b\u6743\u9650\u4f4d\u5e76\u6ca1\u6709\u7ed9\u4e88\u5199\u76ee\u5f55\u6587\u4ef6\u7684\u6743\u9650\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;dirent.h&gt;\n\nDIR *opendir(const char *pathname);\nDIR *fdopendir(int fd);\nstruct dirent *readdir(DIR *dp);\nvoid rewinddir(DIR *dp);\nint closedir(DIR *dp);\nlong telldir(DIR *dp);\nvoid seekdir(DIR *dp, long loc);\n<\/code><\/pre>\n\n\n\n<p>dirent\u81f3\u5c11\u5305\u542b\u4e24\u4e2a\u57df\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">ino_t d_ino;\t\t<em>\/* i-node number *\/<\/em>\nchar d_name[];\t\t<em>\/* null-terminated filename *\/<\/em>\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"chdirfchdirgetcwd%E5%87%BD%E6%95%B0\"><span class=\"ez-toc-section\" id=\"chdir%E3%80%81fchdir%E3%80%81getcwd%E5%87%BD%E6%95%B0\"><\/span>chdir\u3001fchdir\u3001getcwd\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>current working directory\u662f\u4e00\u4e2a\u8fdb\u7a0b\u7684\u5c5e\u6027\u3002\u6539\u53d8\u5f53\u524d\u8fdb\u7a0b\u7684current working directory\uff0c\u4e0d\u5f71\u54cd\u7236\u8fdb\u7a0b\u7684current working directory\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">#include &lt;unistd.h&gt;\n\nint chdir(const char *pathname);\nint fchdir(int fd);\nchar *getcwd(char *buf, size_t size);\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"device-special-files\"><span class=\"ez-toc-section\" id=\"Device_Special_Files\"><\/span>Device Special Files<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>\u5173\u4e8est_dev\u548cst_rdev\u4e24\u4e2a\u57df\u7684\u4f5c\u7528\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6bcf\u4e2a\u6587\u4ef6\u7cfb\u7edf\u90fd\u6709\u4e00\u4e2a\u4e3b\u8bbe\u5907\u53f7\u548c\u4e00\u4e2a\u4ece\u8bbe\u5907\u53f7\uff0c\u88ab\u7f16\u7801\u5728\u4e00\u4e2adev_t\u7c7b\u578b\u91cc<\/li><li>\u53ef\u4ee5\u7528major\u548cminor\u5b8f(&lt;sys\/types.h&gt;)\u4ecedev_t\u53d8\u91cf\u91cc\u83b7\u53d6\u8fd9\u4e24\u4e2a\u8bbe\u5907\u53f7<\/li><li>st_dev\u57df\u91cc\u5305\u542b\u4e86\u5bf9\u5e94\u6587\u4ef6\u6240\u5904\u7684\u6587\u4ef6\u7cfb\u7edf\u7684\u8bbe\u5907\u53f7\u548c\u76f8\u5e94\u7684i-node<\/li><li>\u53ea\u6709character special files\u548cblock special files\u6709st_rdev\u3002\u8be5\u503c\u5305\u542b\u4e86\u5bf9\u5e94\u8bbe\u5907\u7684\u8bbe\u5907\u53f7\u3002<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"summary-of-file-access-permission-bits\"><span class=\"ez-toc-section\" id=\"Summary_of_File_Access_Permission_Bits\"><\/span>Summary of File Access Permission Bits<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Constant<\/strong><\/th><th><strong>Description<\/strong><\/th><th><strong>Effect on regular file<\/strong><\/th><th><strong>Effect on directory<\/strong><\/th><\/tr><\/thead><tbody><tr><td>S_ISUID<\/td><td>set-user-ID<\/td><td>set effective user ID on execution<\/td><td>(not used<\/td><\/tr><tr><td>S_ISGID<\/td><td>set-group-ID<\/td><td>if group-execute set, then set<br>effective group ID on execution;<br>otherwise, enable mandatory<br>record locking(if supported)<\/td><td>set group ID of new files created in directory to group ID of directory<\/td><\/tr><tr><td>S_ISVTX<\/td><td>sticky bit<\/td><td>control caching of file contents(if supported)<\/td><td>restrict removal and renaming of files in drectory<\/td><\/tr><tr><td>S_IRUSR<\/td><td>user-read<\/td><td>user permission to read file<\/td><td>user permission to read directory entries<\/td><\/tr><tr><td>S_IWUSR<\/td><td>user-write<\/td><td>user permission to write file<\/td><td>user permission to remove and create files in directory<\/td><\/tr><tr><td>S_IXUSR<\/td><td>user-execute<\/td><td>user permission to execute file<\/td><td>user permission to search for given pathname in directory<\/td><\/tr><tr><td>S_IRGRP<\/td><td>group-read<\/td><td>group permission to read file<\/td><td>group permission to read directory entries<\/td><\/tr><tr><td>S_IWGRP<\/td><td>group-write<\/td><td>group permission to write file<\/td><td>group permission to remove and create files in directory<\/td><\/tr><tr><td>S_IXGRP<\/td><td>group-execute<\/td><td>group permission to execute file<\/td><td>group permission to search for given pathname in directory<\/td><\/tr><tr><td>S_IROTH<\/td><td>other-read<\/td><td>other permission to read file<\/td><td>other permission to read directory entries<\/td><\/tr><tr><td>S_IWOTH<\/td><td>other-write<\/td><td>other permission to write file<\/td><td>other permission to remove and create files in directory<\/td><\/tr><tr><td>S_IXOTH<\/td><td>other-execute<\/td><td>other permission to execute file<\/td><td>other permission to serach for given pathname in directory<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>stat\u3001fstat\u3001fstat\u548clstat\u51fd\u6570 stat\u8fd4\u56de\u6307\u5b9a\u6587\u4ef6\u7684\u4fe1\u606f\uff0cfstat\u8fd4\u56de\u6587\u4ef6\u63cf\u8ff0\u7b26\u6307\u5411\u7684\u6587\u4ef6\u7684\u4fe1\u606f\uff0clstat\u4e0estat\u7c7b\u4f3c\uff0c\u4f46\u662f\u5728\u9047\u5230\u7b26\u53f7\u94fe\u63a5\u6587\u4ef6\u65f6\uff0clstat\u4e0d\u4f1a\u53bb\u627e\u7b26\u53f7\u94fe\u63a5\u5f15&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,6],"tags":[109],"class_list":["post-959","post","type-post","status-publish","format-standard","hentry","category-10","category-6","tag-apiue"],"_links":{"self":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=959"}],"version-history":[{"count":2,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/959\/revisions"}],"predecessor-version":[{"id":964,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/959\/revisions\/964"}],"wp:attachment":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}