int main(int argc, char *argv[]) { FILE *fp; int i, j; char buf[1024 * BLKSZ_1K]; int blksz = BLKSZ_1K; u32 *index; u8 *rb, *blk_end; int rlen, nlen; char *name; if(argc != 2) err_usr("wrong command\n"); fp = fopen(argv[1], "r"); if(fp == NULL) err_sys("fail to fopen"); fread(buf, sizeof(char), 1024 * BLKSZ_1K, fp); // 获取根目录inode管理结构中的index数组 index = root_index(buf, BLKSZ_1K); for(i = 0; i < 12; i++){ if(!index[i]) continue; //求index[i]号的盘块的地址 rb = (u8 *)blk_to_addr(buf, BLKSZ_1K, index[i]); blk_end = rb + BLKSZ_1K; //此块的末地址 while(rb < blk_end){ rlen = *((u16 *)(rb + 4));//根据存放根目录内容的盘块的地址,偏移到存放此条目录项长度的地址,并取出此目录项的长度. nlen = *((u8 *)(rb + 6));//求目录项中文件名的长度. name = (u8 *)(rb + 8);//求文件名首地址. for(j = 0; j < nlen; j++) putchar(name[j]); putchar('\n'); rb += rlen; } } fclose(fp); return 0; }
int main(int argc, char *argv[]) { int in, out; char *s, *d; int len, i; if(argc != 3) err_usr("usage: ./mycp src dst\n"); in = open(argv[1], O_RDONLY); if(in == -1) err_sys("open error"); out = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0644);/*如果是只写,则不能建立映射区*/ if(out == -1) err_sys("open error"); len = lseek(in, 0, SEEK_END); s = (char *)mmap(NULL, len, PROT_READ, MAP_PRIVATE, in, 0); if(s == MAP_FAILED) err_sys("mmap error"); lseek(out, len - 1, SEEK_SET); write(out, "a", 1); d = (char *)mmap(NULL, len, PROT_WRITE, MAP_SHARED, out, 0); if(d == MAP_FAILED) err_sys("mmap error"); close(in); close(out); for(i = 0; i < len; i++) d[i] = s[i]; munmap(d, len); munmap(s, len); return 0; }
int main(int argc, char *argv[]) { FILE *fp; int i, j; char buf[1024 * BLKSZ_1K]; int blksz = BLKSZ_1K; u32 *index; u8 *rb, *blk_end; int rlen, nlen; char *name; if(argc != 2) err_usr("wrong command\n"); fp = fopen(argv[1], "r"); if(fp == NULL) err_sys("fail to fopen"); fread(buf, sizeof(char), 1024 * BLKSZ_1K, fp); index = root_index(buf, BLKSZ_1K); for(i = 0; i < 12; i++){ if(!index[i]) continue; rb = (u8 *)blk_to_addr(buf, BLKSZ_1K, index[i]); blk_end = rb + BLKSZ_1K; while(rb < blk_end){ rlen = *((u16 *)(rb + 4)); nlen = *((u8 *)(rb + 6)); name = (u8 *)(rb + 8); for(j = 0; j < nlen; j++) putchar(name[j]); putchar('\n'); rb += rlen; } } fclose(fp); return 0; }
int argv_analy(int cfd, char *path) { int len, pos_con, pos_ip, pos_file = 0; char *download_file, buf[N], *upload_file, *file; int fp, n; len = strlen(path); pos_con = is_have_m('-', path, len); if(pos_con == -1)/*不含'-'*/ err_sys("is_have_m('-') error"); pos_ip = is_have_m(':', path, len); if(pos_ip == -1) err_sys("is_have_m(':') error"); if(pos_ip < pos_con){/*download, need to write*/ int len_downfile; if(strncmp(path,SERV_IP, strlen(SERV_IP))!=0) err_usr("ip wrong"); len_downfile = pos_con - pos_ip; download_file = malloc(sizeof(char) * len_downfile); strncpy(download_file, &path[pos_ip + 1], len_downfile - 1); printf("download_file : %s\n",download_file); printf("start download...\n"); fp = open(download_file, O_RDONLY); if(fp == -1) err_sys("open file error"); while((n = read(fp, buf, N)) != 0) write(cfd, buf, n); printf("download finish...\n"); close(fp); free(download_file); }else if(pos_ip > pos_con){/*upload, need to read*/ int len_upfile; if(strncmp(&path[pos_con + 1],SERV_IP, strlen(SERV_IP))!=0) err_usr("ip wrong"); len_upfile = pos_con; upload_file = malloc(sizeof(char) * (len_upfile + 1)); strncpy(upload_file, path, len_upfile); upload_file[len_upfile] = '\0'; printf("upload_file : %s\n",upload_file); while((pos_file = is_have_m('/', upload_file, len_upfile)) != -1){ len_upfile = strlen(upload_file); strncpy(upload_file, &upload_file[pos_file + 1], len_upfile - pos_file - 1); upload_file[len_upfile - pos_file -1] = '\0'; } file = malloc((len_upfile + strlen(&path[pos_ip + 1]) + 1) * sizeof(char)); bzero(file, len_upfile + strlen(&path[pos_ip + 1]) + 1); if(path[pos_ip + 1] != '.') sprintf(file,"%s%c%s", &path[pos_ip + 1], path[len - 1] == '/' ? ' ':'/', upload_file); else sprintf(file, "./%s",upload_file); printf("start upload...\n"); fp = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644); if(fp == -1){ printf("open file error in argv_analy\n"); return -1; } while((n = read(cfd, buf, N))!= 0) write(fp, buf, n); printf("upload finish...\n"); close(fp); free(upload_file); free(file); } return cfd; }