void iappend(uint inum, void *xp, int n) { char *p = (char *)xp; uint fbn, off, n1; struct dinode din; char buf[512]; uint indirect[NINDIRECT]; uint x; rinode(inum, &din); off = xint(din.size); while (n > 0) { fbn = off / 512; assert(fbn < MAXFILE); if (fbn < NDIRECT) { if (xint(din.addrs[fbn]) == 0) { din.addrs[fbn] = xint(freeblock++); usedblocks++; } x = xint(din.addrs[fbn]); } else { if (xint(din.addrs[NDIRECT]) == 0) { // printf("allocate indirect block\n"); din.addrs[NDIRECT] = xint(freeblock++); usedblocks++; } // printf("read indirect block\n"); rsect(xint(din.addrs[NDIRECT]), (char *)indirect); if (indirect[fbn - NDIRECT] == 0) { indirect[fbn - NDIRECT] = xint(freeblock++); usedblocks++; wsect(xint(din.addrs[NDIRECT]), (char *)indirect); } x = xint(indirect[fbn - NDIRECT]); } n1 = min(n, (fbn + 1) * 512 - off); rsect(x, buf); memcpy(buf + off - (fbn * 512), p, n1); wsect(x, buf); n -= n1; off += n1; p += n1; } din.size = xint(off); winode(inum, &din); }
void iappend(uint inum, void *xp, int n) { char *p = (char*)xp; uint fbn, off, n1; struct dinode din; char buf[BSIZE]; uint indirect[NINDIRECT]; uint x; rinode(inum, &din); off = xint(din.size); // printf("append inum %d at off %d sz %d\n", inum, off, n); while(n > 0){ fbn = off / BSIZE; assert(fbn < MAXFILE); if(fbn < NDIRECT){ if(xint(din.addrs[fbn]) == 0){ din.addrs[fbn] = xint(freeblock++); } x = xint(din.addrs[fbn]); } else { if(xint(din.addrs[NDIRECT]) == 0){ din.addrs[NDIRECT] = xint(freeblock++); } rsect(xint(din.addrs[NDIRECT]), (char*)indirect); if(indirect[fbn - NDIRECT] == 0){ indirect[fbn - NDIRECT] = xint(freeblock++); wsect(xint(din.addrs[NDIRECT]), (char*)indirect); } x = xint(indirect[fbn-NDIRECT]); } n1 = min(n, (fbn + 1) * BSIZE - off); rsect(x, buf); bcopy(p, buf + off - (fbn * BSIZE), n1); wsect(x, buf); n -= n1; off += n1; p += n1; } din.size = xint(off); winode(inum, &din); }
void rinode(uint inum, struct dinode *ip) { char buf[512]; uint bn; struct dinode *dip; bn = i2b(inum); rsect(bn, buf); dip = ((struct dinode *)buf) + (inum % IPB); *ip = *dip; }
void rinode(uint inum, struct dinode *ip) { char buf[BSIZE]; uint bn; struct dinode *dip; bn = IBLOCK(inum, sb); rsect(bn, buf); dip = ((struct dinode*)buf) + (inum % IPB); *ip = *dip; }
void winode(uint inum, struct dinode *ip) { char buf[BSIZE]; uint bn; struct dinode *dip; bn = IBLOCK(inum); printf("winode %d\n", bn); rsect(bn, buf); dip = ((struct dinode*)buf) + (inum % IPB); *dip = *ip; wsect(bn, buf); }
int main(int argc, char *argv[]) { // 局部变量 char buf[SECSIZE]; // FAT表表头标记 uchar fatStartContent[8] = {0xf8,0xff,0xff,0x0f,0xff,0xff,0xff,0xff}; struct direntry dire; uint dirClusNum; // 为文件分配的簇号 uint wClusNum; // 根目录簇号 uint fileSize; // 文件大小 uint cc, fd; // cc一次读取文件的字节数,打开文件的fd int rootWSize = 0; // 已向根目录写入的字节数 int fileWSize = 0; // 已向文件中写入的字节数 char filename[FNSIZE]; // 文件名 int i; static_assert(sizeof(int) == 4, "Integers must be 4 bytes!"); if(argc < 2){ fprintf(stderr, "Usage: mkfs fs.img files...\n"); exit(1); } initDat(); // 是否有需要断言 // ? fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666); if(fsfd < 0){ perror(argv[1]); exit(1); } printf("DBR : %d, retain sectors: %d, FAT sector: %d, 2 FATs, data sectors: %d, clusters: %d", nDBR, nRetain, nFAT, nData, nDataClus); // 清零fs for(i = 0; i < FSSIZE; i++) szero(i); // 向第0扇区写入DBR initDBR(); memset(buf, 0, sizeof(buf)); memmove(buf, &fatDBR, sizeof(fatDBR)); wsect(0, buf); // 写入FAT表头标记 rsect4bytes(fatFstSec, 0, fatStartContent); rsect4bytes(fatFstSec, 1, fatStartContent + 4); // 写入根目录 dire = mkFCB(T_DIR, "/", sizeof(DIR)*argc, &dirClusNum); dirClusNum = appendBuf(dirClusNum, &dire, sizeof(DIR), rootWSize); rootWSize += sizeof(DIR); wClusNum = dirClusNum; dire = mkFCB(T_DIR, ".", sizeof(DIR)*argc, &dirClusNum); wClusNum = appendBuf(wClusNum, &dire, sizeof(DIR), rootWSize); rootWSize += sizeof(DIR); dire = mkFCB(T_DIR, "..", 0, &dirClusNum); wClusNum = appendBuf(wClusNum, &dire, sizeof(DIR), rootWSize); rootWSize += sizeof(DIR); // 写入其他文件 for(i = 2; i < argc; i++){ fileWSize = 0; assert(index(argv[i], '/') == 0); fileSize = retFileSize(argv[i]); if((fd = open(argv[i], 0)) < 0){ perror(argv[i]); exit(1); } if(argv[i][0] == '_') ++argv[i]; // 写到目录区 strncpy(filename, argv[i], FNSIZE); dire = mkFCB(T_FILE, filename, fileSize, &dirClusNum); wClusNum = appendBuf(wClusNum, &dire, sizeof(DIR), rootWSize); rootWSize += sizeof(DIR); // 写入文件 while((cc = read(fd, buf, sizeof(buf))) > 0) { dirClusNum = appendBuf(dirClusNum, buf, cc, fileWSize); fileWSize += cc; } close(fd); } // 向第1扇区写入FSI initFSI(); memset(buf, 0, sizeof(buf)); memmove(buf, &fsi, sizeof(fsi)); wsect(1, buf); // 向备份FAT中写入相同的内容 for(i = fatFstSec; i < fatFstSec + nFAT; i++) { rsect(i, buf); wsect((i+nFAT), buf); } // // fix size of root inode dir // rinode(rootino, &din); // off = xint(din.size); // off = ((off/BSIZE) + 1) * BSIZE; // din.size = xint(off); // winode(rootino, &din); // balloc(freeblock); exit(0); }
void iappend(uint inum, void *xp, int n) { char *p = (char*)xp; uint fbn, off, n1; struct dinode din; char buf[512]; uint indirect[512]; uint x; rinode(inum, &din); int temp_fbn, d1, d2, k1, k2, k3, a1, a2, a3; off = xint(din.size); while(n > 0){ //printf("%d\n", n); fbn = off / 512; //assert(fbn < MAXFILE); if(fbn < NDIRECT){ //printf("hjw 1 %d\n", n); if(xint(din.addrs[fbn]) == 0){ din.addrs[fbn] = xint(freeblock++); usedblocks++; } x = xint(din.addrs[fbn]); } else { temp_fbn = fbn; temp_fbn -= NDIRECT; d1 = temp_fbn / 128; k1 = temp_fbn % 128; d2 = d1 / 128; k2 = d1 % 128; //d3 = d2 / 128; k3 = d2 % 128; if((a1 = din.addrs[NDIRECT]) == 0) { a1 = din.addrs[NDIRECT] = xint(freeblock++); usedblocks ++; } rsect(xint(a1), (char*)indirect); if((a2 = indirect[k3]) == 0) { a2 = indirect[k3] = xint(freeblock++); usedblocks ++; wsect(xint(a1), (char*)indirect); } rsect(xint(a2), (char*)indirect); if((a3 = indirect[k2]) == 0) { a3 = indirect[k2] = xint(freeblock++); usedblocks ++; wsect(xint(a2), (char*)indirect); } rsect(xint(a3), (char*)indirect); if((x = indirect[k1]) == 0) { x = indirect[k1] = xint(freeblock++); usedblocks ++; wsect(xint(a3), (char*)indirect); } x = xint(x); //printf("%d--", fbn); } //printf("%d\n", x); n1 = min(n, (fbn + 1) * 512 - off); rsect(x, buf); //printf("hjw 2 %d %d %d %d\n", n, off - (fbn * 512), n1, sizeof(p)); bcopy(p, buf + off - (fbn * 512), n1); // printf("hjw 3 %d\n", n); wsect(x, buf); n -= n1; off += n1; p += n1; } din.size = xint(off); winode(inum, &din); }