BitmapData::Format BitmapData::checkImageFormat(const unsigned char *data, ssize_t size) { if (isPNG(data, size)) { return Format::PNG; } else if (isJPG(data, size)) { return Format::JPG; } else { return Format::UNKOWN; } return Format::UNKOWN; }
//Read all files in the input directory and copy all files to mini file system int ReadDir(const char *dir) { int file_count = 0; int temp = 0; struct dirent* ptr; DIR* srcdir = opendir(dir); pid_t childpid; if (srcdir == NULL) { perror("opendir"); return -1; } while((ptr = readdir(srcdir)) != NULL) // traverse done { char* d_name; d_name = ptr -> d_name; if (ptr -> d_type & DT_DIR) // check whether it is a directory { if(strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { char path[100]; sprintf(path, "%s/%s", dir, d_name); temp = ReadDir(path); file_count += temp; } }else{ printf("\n/////////////////////////START//////////////////////////\n%s/%s\n", dir, d_name); if (isJPG(d_name)){ //---------------Jia------------------ //fork childpid = fork(); //error handling if (childpid == -1) { perror("Failed to fork"); return 1; } //child code if (childpid == 0) { fprintf(stderr, "--------------I am the child----------------\n"); char filepath[100]; char newpath[100]; sprintf(filepath, "%s/%s", dir, d_name); sprintf(newpath, "%s%s", filepath, "encd"); fprintf(stderr,"filepath is %s \t newpath is %s\t\n", filepath, newpath); char cmd[200]; strcpy(cmd, "base64 "); strcat(cmd, filepath); strcat(cmd, " > "); strcat(cmd, newpath); execlp("/bin/bash", "bash", "-c", cmd, NULL); //execl("/usr/bin/base64", "base64", filepath, ">", newpath); }else{ //wait while(r_wait(NULL) > 0) { fprintf(stderr, "Parent Wait\n"); } fprintf(stderr, "--------------I am the parent----------------\n"); //---------------end jia--------------- //--------------------Create--------------------------- int f = Create_File(d_name); //printInode(Inode_List[Superblock.next_free_inode-1]); if(f == -3) { printf("File could not be added to Directory.\n"); continue; }else if (f == -2) { printf("File could not be written to the new inode.\n"); continue; }else if (f == -1) { printf("File already existed in directory.\n"); continue; }else{ //printf("ptr->d_name: %s\n", d_name); // ################# file_count++; //------------------------------Copy image to mini file system------------------------------------------- FILE *fp; int ch; //fprintf(stderr, "d_name is: %s\n", d_name); char filepath[100]; sprintf(filepath, "%s/%s", dir, d_name); //--------------jia-------------------- char newpath[100]; sprintf(newpath, "%s%s", filepath, "encd"); fprintf(stderr,"filepath is %s \t newpath is %s\t\n", filepath, newpath); fp = fopen(newpath, "r"); ///--------------end jia---------------- //fp = fopen(filepath, "r"); if (fp == NULL) { fprintf(stderr, "Cannot open %s/%s .....\n", dir, d_name); break; // what we should do if could not read file!!!!!!!!!!!!!! }else{ //------------------------------Copy file to mini_filesystem--------------------------------------- fseek(fp, 0L, SEEK_END); int sz = ftell(fp); fseek(fp, 0L, SEEK_SET); char* to_write = (char*) malloc(sizeof(char)*sz); int i = 0; while( (ch = fgetc(fp)) != EOF ) { to_write[i] = ch; //fprintf(stderr, "%c\t", ch); i++; } //fprintf(stderr, "\nto_write is: %s\n", to_write); Write_File(Search_Directory(d_name), 0, to_write, sz); fprintf(stderr,"---------------\n"); printInode(Inode_List[Superblock.next_free_inode-1]); fprintf(stderr,"---------------\n/////////////////////////END///////////////////////////\n"); } } } }else{ printf("The file is not JPG file, skipped it.\n"); } } } closedir(srcdir); return file_count; }