Annotation of researchv10dc/cmd/fcopy.c, revision 1.1

1.1     ! root        1: struct  INODE   {
        !             2:         int     MODE;
        !             3:         char    NLINKS;
        !             4:         char    UID;
        !             5:         char    GID;
        !             6:         char    FILL;
        !             7:         long    SIZE;
        !             8:         struct  {
        !             9:                 unsigned        STBLK;
        !            10:                 unsigned        NCBLKS;
        !            11:         }       EXTENTS[27];
        !            12:         int     ACTIME[2];
        !            13:         int     MODTIME[2];
        !            14:         int     CHKSUM;
        !            15: };
        !            16: 
        !            17: struct  DIRECT  {
        !            18:         int     INUM;
        !            19:         char    NAME[14];
        !            20: };
        !            21: 
        !            22: 
        !            23: int     dev;
        !            24: long    offset = 1802240l;
        !            25: 
        !            26: 
        !            27: char    fname[1000];
        !            28: main(argc,argv)
        !            29: int     argc;
        !            30: char    **argv;
        !            31: {
        !            32:         int     inum;
        !            33:         char    *s;
        !            34: 
        !            35:         if(argc != 4)   {
        !            36:                 printf("fcopy: device inode directory\n");
        !            37:                 return(1);
        !            38:         }
        !            39:         if((dev = open(argv[1],0)) < 0) {
        !            40:                 printf("can't open device %s\n",argv[1]);
        !            41:                 return(1);
        !            42:         }
        !            43:         for(s = fname ; *argv[3] ; *s++ = *argv[3]++);
        !            44:         *s = 0;
        !            45:         inum = atoi(argv[2]);
        !            46:         copy(inum);
        !            47: }
        !            48: 
        !            49: 
        !            50: copy(inum)
        !            51: {
        !            52:         struct  INODE   ino;
        !            53:         struct  DIRECT  entry;
        !            54:         static  char    block[512];
        !            55: 
        !            56:         if(lseek(dev, ((long)(inum+7)*128)+offset, 0) < 0 || read(dev, &ino, 128) != 128)       {
        !            57:                 message("Cant read inode",inum,fname);
        !            58:                 return;
        !            59:         }
        !            60:         if(!(ino.MODE & 0100000))       {
        !            61:                 message("unallocated",inum,fname);
        !            62:                 return;
        !            63:         }
        !            64:         switch(ino.MODE & 070000)       {
        !            65:             case 020000:
        !            66:                 message("character special file",inum,fname);
        !            67:                 return;
        !            68:             case 060000:
        !            69:                 message("block special file",inum,fname);
        !            70:                 return;
        !            71:             case 070000:
        !            72:                 message("record special file",inum,fname);
        !            73:                 return;
        !            74:             case 000000:
        !            75:             case 010000:
        !            76:             {
        !            77:                 /* copy the regular file to fname */
        !            78:                 int     e,i;
        !            79:                 int     ofile;
        !            80: /*
        !            81: message("copying file",inum,fname);
        !            82: */
        !            83:                 if((ofile = creat(fname, 0777)) < 0)    {
        !            84:                         message("cant creat file",inum,fname);
        !            85:                         return;
        !            86:                 }
        !            87:                 if(ino.NLINKS != 1)     {
        !            88:                         message("dropping %d links",inum,fname,ino.NLINKS-1);
        !            89:                 }
        !            90:                 for(e = 0 ; ino.SIZE > 0 && e < 27 ; )  {
        !            91:                         if(ino.EXTENTS[e].NCBLKS-- > 0) {
        !            92:                                 if(lseek(dev, (long)(ino.EXTENTS[e].STBLK++)*512 + offset, 0) < 0)      {
        !            93:                                         message("Cant seek",inum,fname);
        !            94:                                         return;
        !            95:                                 }
        !            96:                                 if(read(dev, block, 512) != 512)        {
        !            97:                                         message("cant read",inum,fname);
        !            98:                                         return;
        !            99:                                 }
        !           100:                                 i = (ino.SIZE >= 512) ? 512 : ino.SIZE;
        !           101:                                 if(write(ofile, block, i) != i) {
        !           102:                                         message("bad size",inum,fname);
        !           103:                                         close(ofile);
        !           104:                                         return;
        !           105:                                 }
        !           106:                                 ino.SIZE -= i;
        !           107:                         }
        !           108:                         else
        !           109:                                 e++;
        !           110:                 }
        !           111:                 close(ofile);
        !           112:                 break;
        !           113:             }
        !           114:             case 040000:
        !           115:             case 050000:
        !           116:             {
        !           117:                 /* copy each directory entry */
        !           118:                 int     e,i,j;
        !           119:                 long    b;
        !           120:                 int     flag;
        !           121:                 int     status;
        !           122: /*
        !           123:         message("directory",inum,fname);
        !           124: */
        !           125:                 if(fork() == 0) {
        !           126:                         execl("/bin/mkdir","mkdir",fname,0);
        !           127:                         exit(1);
        !           128:                 }
        !           129:                 wait(&status);
        !           130:                 if(status)      {
        !           131:                         message("can't creat directory",inum,fname);
        !           132:                 }
        !           133:                 flag = 1;
        !           134:                 for(e = 0 ; ino.SIZE > 0 && e < 27 ; )  {
        !           135:                         if(ino.EXTENTS[e].NCBLKS-- > 0) {
        !           136:                                 i = (ino.SIZE >= 512) ? 512 : ino.SIZE;
        !           137:                                 ino.SIZE -= i;
        !           138:                                 b = (long)(ino.EXTENTS[e].STBLK++);
        !           139:                                 for(j = (flag?32:0) ; j < i ; j += 16)  {
        !           140:                                         if(lseek(dev, (b*512)+j+offset, 0) < 0) {
        !           141:                                                 message("cant seek",inum,fname);
        !           142:                                                 return;
        !           143:                                         }
        !           144:                                         read(dev, &entry, 16);
        !           145:                                         if(entry.INUM != 0 && entry.NAME[0])    {
        !           146:                                                 addname(entry.NAME);
        !           147:                                                 copy(entry.INUM);
        !           148:                                                 subname();
        !           149:                                         }
        !           150:                                 }
        !           151:                                 flag = 0;
        !           152:                         }
        !           153:                         else
        !           154:                                 e++;
        !           155:                 }
        !           156:                 break;
        !           157:             }
        !           158:             default:
        !           159:                 message("funny file type (%o)",inum,fname,ino.MODE);
        !           160:                 return;
        !           161:         }
        !           162:         /* change the mode and owner of fname */
        !           163:         if(ino.MODE & 07000)    {
        !           164:                 message("funny file mode (%o)",inum,fname,ino.MODE);
        !           165:         }
        !           166:         chmod(fname, ino.MODE & 0777);
        !           167:         chown(fname, ino.UID & 0377, ino.GID & 0377);
        !           168: }
        !           169: 
        !           170: 
        !           171: addname(s)
        !           172: char    *s;
        !           173: {
        !           174:         char    *f;
        !           175:         int     i;
        !           176: 
        !           177:         f = fname;
        !           178:         while(*f++);
        !           179:         f--;
        !           180:         *f++ = '/';
        !           181:         for(i = 14 ; *s && i-- ; *f++ = *s++);
        !           182:         *f = 0;
        !           183: }
        !           184: 
        !           185: 
        !           186: subname()
        !           187: {
        !           188:         char    *f;
        !           189: 
        !           190:         f = fname;
        !           191:         while(*f++);
        !           192:         while(*--f != '/')
        !           193:                 *f = 0;
        !           194:         *f = 0;
        !           195: }
        !           196: 
        !           197: 
        !           198: message(s,i,f,x)
        !           199: char    *s,*f;
        !           200: {
        !           201: 
        !           202:         printf("[%s] (%d) : ",f,i);
        !           203:         printf(s,x);
        !           204:         printf("\n");
        !           205: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.