{"id":103125,"date":"2020-05-21T22:12:03","date_gmt":"2020-05-21T14:12:03","guid":{"rendered":"http:\/\/4563.org\/?p=103125"},"modified":"2020-05-21T22:12:03","modified_gmt":"2020-05-21T14:12:03","slug":"%e4%b8%80%e8%b5%b7%e5%86%99%e4%b8%80%e4%b8%aa-linux-%e4%b8%8b%e7%9a%84%e6%89%93%e5%8c%85%e8%bd%af%e4%bb%b6-tar-md","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=103125","title":{"rendered":"\u4e00\u8d77\u5199\u4e00\u4e2a Linux \u4e0b\u7684\u6253\u5305\u8f6f\u4ef6 tar.md"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e00\u8d77\u5199\u4e00\u4e2a Linux \u4e0b\u7684\u6253\u5305\u8f6f\u4ef6 tar.md               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ChristopherWu <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h3>\u6559\u4f60\u5199\u4e00\u4e2a linux \u4e0b\u7684\u6253\u5305\u8f6f\u4ef6 tar.md<\/h3>\n<p>\u76f8\u4fe1\u4f60\u5bf9 linux \u7684 <code>.tar.gz<\/code> \u6709\u70b9\u719f\u6089\uff0c\u8fd9\u5c31\u662f\u5148 tar \u6253\u5305\uff08<code>.tar \u540e\u7f00<\/code>\uff09\uff0c\u518d\u5bf9\u6b64 <code>tar<\/code> \u6587\u4ef6\u7528 <code>gzip<\/code>\u538b\u7f29(<code>.tar.gz<\/code>)\u7684\u540e\u7f00\u540d\u3002<\/p>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c <code>tar<\/code>\u4e0d\u662f\u538b\u7f29\u8f6f\u4ef6\uff0c\u5b83\u53ea\u505a\u628a\u4e00\u5806\u6587\u4ef6 \/\u6587\u4ef6\u5939\u6253\u5305\u5230\u4e00\u4e2a\u6587\u4ef6\uff08 tar \u6587\u4ef6\uff09\u91cc\u7684\u4e8b\u60c5\uff0c\u800c\u6587\u4ef6\u8054\u7cfb\uff0c\u6587\u4ef6\u6743\u9650\uff0c\u76f8\u5bf9\u7684\u8def\u5f84\u7b49\u90fd\u4f1a\u7ed9\u4f60\u4fdd\u5b58\u597d\u3002<\/p>\n<p>\u4e00\u5f00\u59cb\u8bbe\u8ba1\u662f <code>tar<\/code> \u8ddf <code>gzip<\/code>\u53ea\u505a\u4e00\u4ef6\u4e8b\u60c5\uff0c\u5404\u53f8\u5176\u4e8b\uff0c\u540e\u6765\u53d1\u73b0\u592a\u9ebb\u70e6\u4e86\uff0c\u4e8e\u662f\u5c31\u628a\u538b\u7f29\u529f\u80fd\u6574\u5408\u5230 tar \u91cc\u4e86\u3002<\/p>\n<pre><code>- Create a gzipped archive:     tar czf target.tar.gz file1 file2 file3 <\/code><\/pre>\n<p>\u6700\u8fd1\u5b66\u4e60 OS \u65f6\u5199\u4e86\u4e00\u4e2a\u7c7b\u4f3c<code>tar<\/code>\u7684\u9879\u76ee\uff0c\u90a3\u4e48\u4eca\u5929\u5c31\u8d81\u70ed\u6253\u94c1\u7b80\u5355\u8bf4\u4e00\u4e0b\u5982\u4f55\u5199\u4e00\u4e2a\u6253\u5305\u8f6f\u4ef6\uff0c\u8fd9\u4e2a\u8f6f\u4ef6\u4f1a\u5c06\u91cd\u590d\u7684\u6587\u4ef6\u5185\u5bb9\u901a\u8fc7 md5 \u6bd4\u8f83\uff0c\u590d\u7528\u65e7\u7684\u5185\u5bb9\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200526_5ecc9bb719a73.png\" alt=\"\u4e00\u8d77\u5199\u4e00\u4e2a Linux \u4e0b\u7684\u6253\u5305\u8f6f\u4ef6 tar.md\" \/><\/p>\n<h4>\u57fa\u672c\u5355\u4f4d block<\/h4>\n<p><code>block<\/code> \u53ef\u4ee5\u7406\u89e3\u4e3a\u6587\u4ef6\u7cfb\u7edf\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u5206\u522b\u6709\u4ee5\u4e0b\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li>directory block\uff0c\u6587\u4ef6\u5939 block\uff0c\u5b58\u50a8\u6587\u4ef6\u5939 meta \u4fe1\u606f\uff1b<\/li>\n<li>file block\uff0c\u6587\u4ef6 block\uff0c\u5b58\u50a8\u6587\u4ef6 meta \u4fe1\u606f\uff1b<\/li>\n<li>data block\uff0c\u53ea\u7528\u6765\u5b58\u6587\u4ef6\u5185\u5bb9\uff1b<\/li>\n<\/ul>\n<p>Directory block\uff0c\u6ce8\u610f\u7684\u662f entry \u91cc\u8981\u6709 fileindex \u6765\u5b58\u50a8\u91cd\u590d\u6587\u4ef6\u7684 name \u7684\u4e0b\u6807\u3002<\/p>\n<p>\u540c\u65f6\uff0c\u7ed9 \u9879\u76ee\u4e00\u4e2a root dir \u3002<\/p>\n<pre><code>typedef struct {     char  name[SIFS_MAX_NAME_LENGTH]; \/\/ name of the directory     time_t  modtime; \/\/ time last modified &lt;- time()      uint32_t  nentries;\/\/ \u6587\u4ef6\u5939\u5185\u7684\u6587\u4ef6 \/\u6587\u4ef6\u5939\u6570\u91cf     struct {         SIFS_BLOCKID blockID; \/\/ subdirectory \u6216\u8005 file \u7684 blockID         uint32_t fileindex; \/\/ \u91cd\u590d\u6587\u4ef6\u7684\u4e0d\u540c\u540d\u5b57     } entries[SIFS_MAX_ENTRIES]; } SIFS_DIRBLOCK; <\/code><\/pre>\n<p>\u6587\u4ef6 Block\uff0c<code>length<\/code> \u5c31\u662f\u6709\u591a\u5c11 bytes \u7684\u6587\u4ef6\u5185\u5bb9\uff0c\u4e4b\u540e\u7528\u6765\u7b97\u6709\u591a\u5c11\u4e2a <code>data block<\/code>\uff0c<code>firstblockID<\/code>\u8bb0\u5f55\u7b2c\u4e00\u4e2a\u6570\u636e block \u7684 id\uff0c<code>nfiles<\/code> \u8bb0\u5f55\u6709\u591a\u5c11\u91cd\u590d\u5185\u5bb9\u7684\u6587\u4ef6\u6570\u91cf\u4e86\uff0c<code>filenames<\/code> \u5c31\u662f\u91cd\u590d\u6b64\u6587\u4ef6 block \u7684\u6587\u4ef6\u5185\u5bb9\u7684\u6587\u4ef6\u540d\u5b57\u3002<\/p>\n<pre><code>typedef struct {     time_t  modtime; \/\/ time first file added &lt;- time()     size_t  length;  \/\/ length of files' contents in bytes      unsigned char md5[MD5_BYTELEN];\/\/the MD5 cryptographic digest (a summary) of the files' contents     SIFS_BLOCKID firstblockID;\/\/ the block number (blockID) of the files' first data-block      uint32_t  nfiles;  \/\/ n files with identical contents     char  filenames[SIFS_MAX_ENTRIES][SIFS_MAX_NAME_LENGTH];\/\/ an array of each same file's name and its modification time. } SIFS_FILEBLOCK; <\/code><\/pre>\n<p>bitmaps \u6570\u7ec4\uff0c\u8bb0\u5f55\u4e86\u6bcf\u4e2a block \u7684\u7c7b\u578b\uff0c\u6709\uff1a\u6587\u4ef6\u3001\u6587\u4ef6\u5939\u4ee5\u53ca data block \u4e09\u79cd\u7c7b\u578b\u3002<\/p>\n<h4>\u901a\u7528\u51fd\u6570<\/h4>\n<p>\u5c31\u8ba9\u5927\u5bb6\u770b\u770b\u5173\u952e\u51fd\u6570\u597d\u4e86\uff1a<\/p>\n<p>\u8bfb tar \u540e\u7684\u6587\u4ef6\u7684 meta \u5934\uff0c\u8bb0\u5f55\u4e86 block \u7684\u5927\u5c0f\uff08 blocksize \uff09 \u4ee5\u53ca\u591a\u5c11\u4e2a blocks \u3002<\/p>\n<pre><code>void read_vol_header(FILE *vol, SIFS_VOLUME_HEADER *header) {     fread(header, sizeof(SIFS_VOLUME_HEADER), 1, vol);     printf(\"header-&gt;blocksize %zu, header-&gt;nblocks %un\", header-&gt;blocksize , header-&gt;nblocks); } <\/code><\/pre>\n<p>bitmap\uff0c\u6bcf\u6b21\u64cd\u4f5c tar \u6587\u4ef6\u90fd\u8981\u8bfb\u7684\u3002<\/p>\n<pre><code>void read_bitmap(FILE *vol, SIFS_BIT *bitmap, int nblocks) {     int size = nblocks * sizeof(SIFS_BIT);     fread(bitmap, size, 1, vol); } <\/code><\/pre>\n<p><code>root_block<\/code>\u540c\u7406\uff0c\u8bfb\u548c\u5199\u5565\u4e1c\u897f\u90fd\u8981\u4ece root block \u3001root dir \u51fa\u53d1\u3002<\/p>\n<pre><code>void read_root_block(FILE *vol, SIFS_DIRBLOCK *dirblock){     fread(dirblock, sizeof(SIFS_DIRBLOCK), 1, vol);     printf(\"read_root_block finish, dirblock.name: %s, dirblock.entrieds: %d, dirblock.modtime %ldn\", dirblock-&gt;name, dirblock-&gt;nentries,dirblock-&gt;modtime); } <\/code><\/pre>\n<p>\u8def\u5f84\u561b\uff0c\u4f60\u61c2\u7684\uff0c<code>.\/sifs_put volumn ~\/res.txt \/dirB\/subdirB\/subsubdir\/newfileB<\/code>\uff0c\u8981\u8bfb\u7684\u5185\u5bb9\u53ef\u4ee5\u9760 read \u51fd\u6570\u89e3\u51b3\uff0c\u4f46\u662f\u5199\u5230 tar \u6587\u4ef6\u91cc\u7684\u5c31\u8981\u624b\u52a8\u89e3\u6790\u9012\u5f52\u67e5\u8def\u5f84\u4e86\u3002<\/p>\n<pre><code>void read_route_names(char* pathname, char** route_names, int *route_cnt) {     char *dir;     char *pathname_to_split = copyStr(pathname);     strcpy(pathname_to_split, pathname);     while ((dir = strsep(&amp;pathname_to_split, \"\/\")) != NULL) {         route_names[*route_cnt] = copyStr(dir);         (*route_cnt)++;     } } <\/code><\/pre>\n<p>\u4ee5\u4e0a\u51e0\u4e4e\u662f<code>mkdir\uff0crmdir\uff0cwritefile\uff0creadfile\uff0cputfile<\/code>\u7b49\u7b49\u64cd\u4f5c\u90fd\u8981\u505a\u7684\u3002<\/p>\n<h4>\u5b9e\u73b0<\/h4>\n<p>\u7136\u540e\uff0c\u5e94\u8be5\u4e3e\u4e00\u4e2a readfile \u7684\u4f8b\u5b50\u5c31\u53ef\u4ee5\u505a\u4ee3\u8868\u4e86\u3002<\/p>\n<pre><code>int recursive_dirinfo(SIFS_DIRBLOCK *cur_dir_block, char **route_names, int route_name_p, int route_cnt); <\/code><\/pre>\n<p>\u5b9e\u73b0\uff1a<\/p>\n<pre><code>int recursive_dirinfo(SIFS_DIRBLOCK *cur_dir_block, char **route_names, int route_name_p, int route_cnt) {     for(int i=0; i&lt;cur_dir_block-&gt;nentries ; i++) {         int blockid = cur_dir_block-&gt;entries[i].blockID;         if(bitmap[blockid]==SIFS_DIR) {             SIFS_DIRBLOCK dirblock;             int start = sizeof(SIFS_VOLUME_HEADER) + header.nblocks*sizeof(SIFS_BIT);             read_dir_block(vol, &amp;dirblock, blockid * blocksize, start);             if(strcmp(dirblock.name, route_names[route_name_p]) == 0) {                 if(route_name_p+2 == route_cnt) {                     return do_read_file(cur_dir_block, route_names[route_name_p+1], blockid);                 }                 return recursive_dirinfo(&amp;dirblock, route_names, route_name_p+1, route_cnt);             }         }     }     return 1; } <\/code><\/pre>\n<p>\u4ee5&#8220;.\/sifs_put volumn ~\/res.txt \/dirB\/subdirB\/subsubdir\/newfileB<code>\u4e3a\u4f8b\u5b50\uff0c\u5982\u679c\u9012\u5f52\u627e\u5230<\/code>subsubdir`\u8fd9\u4e2a\u6587\u4ef6\u5939 block\uff0c\u8fdb\u884c\u76f8\u5e94\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u5199\u6587\u4ef6\u5c31\u5f80 bitmap \u4e00\u76f4\u627e\u6ca1\u6709\u7528\u8fc7\u7684 block\uff0c\u591f\u5199\u6587\u4ef6\u5c31\u5199\u8fdb\u53bb\uff0c\u6587\u4ef6\u5939\u66f4\u65b0\u4e00\u4e0b\u4fe1\u606f\u3002<\/li>\n<li>\u8bfb\u6587\u4ef6\u5c31\u662f\u6839\u636e\u6b64\u6587\u4ef6\u5939 block\uff0c\u627e\u91cc\u9762\u7684 <code>newfileB<\/code><\/li>\n<\/ul>\n<pre><code>int do_read_file(SIFS_DIRBLOCK *parent_dir, char *filename,  int parent_dir_block) {     printf(\"do_find_file_info, filename %sn\", filename);     for(int i=1; i&lt;header.nblocks ; i++) {         SIFS_FILEBLOCK fileblock;         if(bitmap[i]==SIFS_FILE) {             int start = sizeof(SIFS_VOLUME_HEADER) + header.nblocks*sizeof(SIFS_BIT);             read_file_block(vol, &amp;fileblock, i * blocksize, start);             *nbytes = fileblock.length;             int need_data_blocks = *nbytes \/ header.blocksize;             if(strcmp(fileblock.filenames[0],  filename) == 0) {                 for(int d_block_id = fileblock.firstblockID; d_block_id - i -1 &lt; need_data_blocks; d_block_id++) {                     read_data_block(vol, (char*)(*data)+(d_block_id - i -1), blocksize, d_block_id * header.blocksize, start);                 }                 return 0;             }          }     }     return 1; } <\/code><\/pre>\n<p>\u800c\u771f\u5b9e\u7684 tar \u81ea\u7136\u66f4\u590d\u6742\uff0c\u8fd8\u8981\u8bb0\u5f55\u7528\u6237\u6743\u9650\u3001\u7528\u6237\u3001group \u6587\u4ef6\u7b49\u7b49\uff1a<\/p>\n<pre><code>struct posix_header {                       \/* byte offset *\/   char name[100];       \/*   0 *\/   \u6587\u4ef6\u540d   char mode[8];         \/* 100 *\/   \u7528\u6237\u6743\u9650   char uid[8];          \/* 108 *\/   user id   char gid[8];          \/* 116 *\/   group id   char size[12];        \/* 124 *\/   \u6587\u4ef6\u5927\u5c0f   char mtime[12];       \/* 136 *\/   \u4fee\u6539\u65f6\u95f4   char chksum[8];       \/* 148 *\/   \u6821\u9a8c\u503c   char typeflag;        \/* 156 *\/   \u6587\u4ef6\u7c7b\u578b\u6807\u5fd7   char linkname[100];   \/* 157 *\/   \u7b26\u53f7\u94fe\u63a5\u6307\u5411   char magic[6];        \/* 257 *\/      char version[2];      \/* 263 *\/   char uname[32];       \/* 265 *\/   user name   char gname[32];       \/* 297 *\/   group name   char devmajor[8];     \/* 329 *\/   \u8bbe\u5907\u6587\u4ef6 major   char devminor[8];     \/* 337 *\/   \u8bbe\u5907\u6587\u4ef6 minor   char prefix[155];     \/* 345 *\/                         \/* 500 *\/ };  \u6587\u4ef6\u7c7b\u578b\u6807\u5fd7\u5b9a\u4e49\uff0c\u5305\u542b\u4e86\u6240\u6709 Unix \u7cfb\u7edf\u4e2d\u7684\u6587\u4ef6\u7c7b\u578b  #define REGTYPE  '0'            \/* regular file *\/ #define LNKTYPE  '1'            \/* link *\/ #define SYMTYPE  '2'            \/* reserved *\/ #define CHRTYPE  '3'            \/* character special *\/ #define BLKTYPE  '4'            \/* block special *\/ #define DIRTYPE  '5'            \/* directory *\/ #define FIFOTYPE '6'            \/* FIFO special *\/ #define CONTTYPE '7'            \/* reserved *\/ <\/code><\/pre>\n<p>\u6982\u89c8\u5982\u6b64\uff0c\u5199\u8d77\u6765\u5176\u5b9e\u6709\u70b9\u70e6 &#8211; = -\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u5199\u5199\u3002<\/p>\n<h1>\u770b\u5230\u6700\u540e\u7684\u4f60<\/h1>\n<p> <\/p>\n<p>\u672c\u6587\u53ea\u662f\u629b\u7816\u5f15\u7389\uff1b P<\/p>\n<p>\u5728\u4e0b\u5728 Shopee \u5de5\u4f5c\uff0c\u89c9\u5f97\u6c34\u6df1\u706b\u70ed\u4e0d\u559c\u6b22\u52a0\u73ed\u7684\u540c\u5b66\u53ef\u4ee5\u8003\u8651\u4e00\u4e0b<\/p>\n<p>\u62d2\u7edd 996\uff0c\u90a3\u5c31\u6765 shopee\uff0c\u5f85\u9047 work life balance \u4e24\u4e0d\uff1a https:\/\/www.v2ex.com\/t\/672561#reply1<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1702451\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : alalida <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u770b\u8d77\u6765\u4f3c\u4e4e\u5f88\u5389\u5bb3                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u8d77\u5199\u4e00\u4e2a Linux \u4e0b\u7684\u6253\u5305\u8f6f&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/103125"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=103125"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/103125\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=103125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=103125"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=103125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}