static int FILE_recv_binary(FILE *in, FILE *out) { size_t n; char *buf; time_t then = time(0) - 1; time_t now; ftp_set_close_handler(); if(foo_hookf) foo_hookf(&ftp->ti); ftp->ti.begin = false; clearerr(in); clearerr(out); buf = (char *)xmalloc(FTP_BUFSIZ); while(!feof(in)) { if(wait_for_input() != 0) { ftp_trace("wait_for_input() returned non-zero\n"); break; } #ifdef SECFTP n = sec_read(fileno(in), buf, FTP_BUFSIZ); #else n = fread(buf, sizeof(char), FTP_BUFSIZ, in); #endif if(n <= 0) break; if(ftp_sigints() > 0) { ftp_trace("break due to sigint\n"); break; } if(fwrite(buf, sizeof(char), n, out) != n) break; ftp->ti.size += n; if(foo_hookf) { now = time(0); if(now > then) { foo_hookf(&ftp->ti); then = now; } } } free(buf); ftp_set_close_handler(); return maybe_abort(in, out); }
int sec_getc(FILE * F) { if (ftp->sec_complete && ftp->data_prot) { char c; if (sec_read(fileno(F), &c, 1) <= 0) return EOF; return c; } else return getc(F); }
int main(int argc, char *argv[]){ FILE *file; if (argc < 2) { /* read input */ /* give error message and exit */ fprintf(stderr, "Must pass an argument!\n"); exit(1); } file = fopen(argv[1],"rb"); // open file //FILE *file = fopen("fat_volume.dat","rb"); if (file==NULL) { printf("No such a file"); exit(0); } printf("File found "); fseek(file, 0L, SEEK_END); long buff_size = ftell(file); fseek(file, 0L, SEEK_SET); char *buf = malloc(buff_size+1);// create a buffer fread(buf,buff_size,1,file); long boot_off = 0; char* sec_buf = malloc(512); sec_read(file, boot_off,sec_buf); // read boot and store it in boot_sect_t boot_sect_t* boot = boot_read(sec_buf); free(sec_buf); ssize = (boot->ssize[1]<<8|boot->ssize[0]); //its sector size. printf("Sector Size: %i\n",ssize); csize = boot->csize; printf("cluster size in sectors: %i\n",boot->csize); int reserved = (boot->reserved[1]<<8|boot->reserved[0]);//the number of reserved sectors on the disk. printf("The number of reserved sctor: %i\n",reserved); printf("The number of FAT copies: %i\n",boot->numfat); int numroot = (boot->numroot[1] <<8|boot->numroot[0] ); //the number of entries in its root directory printf("The number of entries in its root directory: %i\n",numroot); int sectors16 = (boot->sectors16[1]<<8|boot->sectors16[0]);//Total number of sectors in the filesystem //printf("The total number of sectors in filesystem: %i\n",sectors16); int sectperfat16 = (boot->sectperfat16[1]<<8|boot->sectperfat16[0]); printf("The number of sectors in FAT: %i\n",sectperfat16); int sectpertrack = (boot->sectpertrack[1]<<8|boot->sectpertrack[0]); int heads = (boot->heads[1]<<8|boot->heads[0]); int num_hidden_sec = (boot->prevsect[1]<<8|boot->prevsect[0]); //Number of hidden sectors printf("The number of hidden sectors on the disk: %i\n",num_hidden_sec); sec_num_first_FAT = 0+reserved; printf("The sector number of the first copy of FAT: %i\n",sec_num_first_FAT); sec_num_first_root = reserved+(boot->numfat*sectperfat16); printf("The sector number of the first sector of root dictory: %i\n",sec_num_first_root); sec_num_first_data = ((numroot*32)/ssize)+sec_num_first_root; printf("The sector number of the first sector of data: %i\n",sec_num_first_data); // printf("Data from file:\n%u",buf[sec_num_first_data] ); /***************************************************************************/ //end of boot sec long cur_buf_pos = (sec_num_first_root)*ssize; //printf("%c\n",buf[cur_buf_pos]); directory_read(file,cur_buf_pos, 0); fclose(file); // close file //free(buf); }
void directory_read(FILE *file, long cur_buf_pos, int count_3){ // printf("current cur_buf_pos%d\n",cur_buf_pos); int DIRECTORY_SIZE = 32; int i; char* buf_ = malloc(512); sec_read(file, cur_buf_pos,buf_); int curpos = cur_buf_pos; // read 512 bytes each time int j; for (j = 0; j<16; j++) { cur_buf_pos = j * 32; // if first byte of directory is 0 means end of file if(buf_[cur_buf_pos] == 0){ free(buf_); return; } root_directory* rd = malloc(sizeof(root_directory)); rd->file_name[0] = buf_[cur_buf_pos]; rd->file_name[1] = buf_[cur_buf_pos+1]; rd->file_name[2] = buf_[cur_buf_pos+2]; rd->file_name[3] = buf_[cur_buf_pos+3]; rd->file_name[4] = buf_[cur_buf_pos+4]; rd->file_name[5] = buf_[cur_buf_pos+5]; rd->file_name[6] = buf_[cur_buf_pos+6]; rd->file_name[7] = buf_[cur_buf_pos+7]; rd->ext[0] = buf_[cur_buf_pos+8]; rd->ext[1] = buf_[cur_buf_pos+9]; rd->ext[2] = buf_[cur_buf_pos+10]; rd->attri = buf_[cur_buf_pos+11]; rd->start_cluster [0] = buf_[cur_buf_pos+26]; rd->start_cluster [1] = buf_[cur_buf_pos+27]; rd->file_size[0] = buf_[cur_buf_pos+28]; rd->file_size[1] = buf_[cur_buf_pos+29]; rd->file_size[2] = buf_[cur_buf_pos+30]; rd->file_size[3] = buf_[cur_buf_pos+31]; //cur_buf_pos = cur_buf_pos + DIRECTORY_SIZE; char first = rd->file_name[0]; int start_cluster; if(first!=229){ char * name_buf; start_cluster = print_directory(rd, name_buf); } else{ break; } uint8_t attri = rd->attri; free(rd); if ((attri & 0x10)&&(start_cluster>0)){ // if has subdirectory, go read FAT int* clusters = FAT_read(start_cluster, file);//FAT_read return an array of clusters //int count_2 = 0; count_3 = 0; for (count_3; count_3<10; count_3++) { // condition: if a valid cluster number and not subdirectory if ((clusters[count_3] >= 2)&&(clusters[count_3]<4095)&&first!='.'){ long new_pos = ((clusters[count_3] - 2) * csize )*ssize + (sec_num_first_data * ssize); directory_read(file, new_pos,count_3+1); }else{ } } }else{ } } // } }