void extractTree(struct Volume *vol, struct List* tree, char *path, unsigned char *extbuf, BOOL pflag, BOOL qflag) { struct Entry* entry; char *buf; char sysbuf[200]; while(tree) { entry = (struct Entry*)tree->content; if (entry->type==ST_DIR) { buf = NULL; if (strlen(path)>0) { buf=(char*)malloc(strlen(path)+1+strlen(entry->name)+1); if (!buf) return; sprintf(buf,"%s%c%s",path,DIRSEP,entry->name); sprintf(sysbuf,"%s %s",MKDIR,buf); if (!qflag) printf("x - %s%c\n",buf,DIRSEP); } else { sprintf(sysbuf,"%s %s",MKDIR,entry->name); if (!qflag) printf("x - %s%c\n",entry->name,DIRSEP); } if (!pflag) system(sysbuf); if (tree->subdir!=NULL) { if (adfChangeDir(vol,entry->name)==RC_OK) { if (buf!=NULL) extractTree(vol,tree->subdir,buf,extbuf, pflag, qflag); else extractTree(vol,tree->subdir,entry->name,extbuf, pflag, qflag); adfParentDir(vol); } else { if (strlen(path)>0) fprintf(stderr,"ExtractTree : dir \"%s/%s\" not found.\n",path,entry->name); else fprintf(stderr,"ExtractTree : dir \"%s\" not found.\n",entry->name); } } if (buf!=NULL) free(buf); } else if (entry->type==ST_FILE) { extractFile(vol,entry->name,path,extbuf, pflag, qflag); } tree = tree->next; } }
static int changedir(FSMNG * fsmng,char * path,SECTNUM * curdir,int dir) { int i,ret; char tmppath[512]; char * tmpptr; struct bEntryBlock entry; struct Volume * adfvolume; adfvolume = (struct Volume *)fsmng->volume; if(strlen(path)) { i = 0; if(path[i]=='/') { ret = adfToRootDir(adfvolume); i++; } while(path[i]) { tmpptr = strchr(&path[i],'/'); memset(tmppath,0,sizeof(tmppath)); if(tmpptr) { strncpy(tmppath,&path[i],tmpptr-&path[i]); i = i + (tmpptr-&path[i]) + 1; } else { strcpy(tmppath,&path[i]); i = i + strlen(&path[i]); } if(strlen(tmppath)) ret = adfChangeDir(adfvolume,tmppath); else break; } } if(!ret) { ret = adfReadEntryBlock(adfvolume,adfvolume->curDirPtr,&entry); if(!ret) { if(dir) { if(entry.secType == ST_DIR || entry.secType == ST_ROOT) { if(curdir) *curdir = adfvolume->curDirPtr; return 0; } else return 1; } else { if(entry.secType == ST_FILE || entry.secType == ST_LFILE) { if(curdir) *curdir = adfvolume->curDirPtr; return 0; } else return 1; } } } return ret; }
int main(int argc, char *argv[]) { struct Device *hd; struct Volume *vol; struct List *list; SECTNUM nSect; adfEnvInitDefault(); /* mount existing device */ hd = adfMountDev( argv[1], FALSE ); if (!hd) { fprintf(stderr, "can't mount device\n"); adfEnvCleanUp(); exit(1); } vol = adfMount(hd, 0, FALSE); if (!vol) { adfUnMountDev(hd); fprintf(stderr, "can't mount volume\n"); adfEnvCleanUp(); exit(1); } adfVolumeInfo(vol); list = adfGetDirEnt(vol,vol->curDirPtr); while(list) { printEntry(list->content); adfFreeEntry(list->content); list = list->next; } freeList(list); putchar('\n'); adfCreateDir(vol,vol->curDirPtr,"newdir"); /* cd dir_2 */ nSect = adfChangeDir(vol, "same_hash"); list = adfGetDirEnt(vol,vol->curDirPtr); while(list) { printEntry(list->content); adfFreeEntry(list->content); list = list->next; } freeList(list); putchar('\n'); /* not empty */ adfRemoveEntry(vol, vol->curDirPtr, "mon.paradox"); /* first in same hash linked list */ adfRemoveEntry(vol, vol->curDirPtr, "file_3a"); /* second */ adfRemoveEntry(vol, vol->curDirPtr, "dir_3"); /* last */ adfRemoveEntry(vol, vol->curDirPtr, "dir_1a"); list = adfGetDirEnt(vol,vol->curDirPtr); while(list) { printEntry(list->content); adfFreeEntry(list->content); list = list->next; } freeList(list); putchar('\n'); adfParentDir(vol); adfRemoveEntry(vol, vol->curDirPtr, "mod.And.DistantCall"); list = adfGetDirEnt(vol,vol->curDirPtr); while(list) { printEntry(list->content); adfFreeEntry(list->content); list = list->next; } freeList(list); putchar('\n'); adfVolumeInfo(vol); adfUnMount(vol); adfUnMountDev(hd); adfEnvCleanUp(); return 0; }
int ScanFile(HXCFLOPPYEMULATOR* floppycontext,struct Volume * adfvolume,char * folder,char * file) { long hfindfile; filefoundinfo FindFileData; int bbool; int byte_written; FILE * ftemp; unsigned char tempbuffer[512]; struct File* adffile; unsigned char * fullpath;//,*fileimg; int size,filesize; RETCODE rc; hfindfile=find_first_file(folder,file, &FindFileData); if(hfindfile!=-1) { bbool=TRUE; while(hfindfile!=-1 && bbool) { if(FindFileData.isdirectory) { if(strcmp(".",FindFileData.filename)!=0 && strcmp("..",FindFileData.filename)!=0) { if(adfCountFreeBlocks(adfvolume)>4) { floppycontext->hxc_printf(MSG_INFO_1,"Adding directory %s",FindFileData.filename); rc=adfCreateDir(adfvolume,adfvolume->curDirPtr,FindFileData.filename); if(rc==RC_OK) { floppycontext->hxc_printf(MSG_INFO_1,"entering directory %s",FindFileData.filename); rc=adfChangeDir(adfvolume, FindFileData.filename); if(rc==RC_OK) { fullpath=malloc(strlen(FindFileData.filename)+strlen(folder)+2); sprintf(fullpath,"%s\\%s",folder,FindFileData.filename); if(ScanFile(floppycontext,adfvolume,fullpath,file)) { adfParentDir(adfvolume); free(fullpath); return 1; } floppycontext->hxc_printf(MSG_INFO_1,"Leaving directory %s",FindFileData.filename); free(fullpath); adfParentDir( adfvolume); } else { floppycontext->hxc_printf(MSG_ERROR,"Cannot enter to the directory %s !",FindFileData.filename); return 1; } } else { floppycontext->hxc_printf(MSG_ERROR,"Cannot Add the directory %s !",FindFileData.filename); return 1; } } else { floppycontext->hxc_printf(MSG_ERROR,"Cannot Add a directory ! : no more free block!!!"); return 1; } } } else { if(adfCountFreeBlocks(adfvolume)>4) { floppycontext->hxc_printf(MSG_INFO_1,"Adding file %s, %dB",FindFileData.filename,FindFileData.size); adffile = adfOpenFile(adfvolume, FindFileData.filename, "w"); if(adffile) { if(FindFileData.size) { fullpath=malloc(strlen(FindFileData.filename)+strlen(folder)+2); sprintf(fullpath,"%s\\%s",folder,FindFileData.filename); ftemp=fopen(fullpath,"rb"); if(ftemp) { fseek(ftemp,0,SEEK_END); filesize=ftell(ftemp); fseek(ftemp,0,SEEK_SET); do { if(filesize>=512) { size=512; } else { size=filesize; } fread(&tempbuffer,size,1,ftemp); byte_written=adfWriteFile(adffile, size, tempbuffer); if((byte_written!=size) || (adfCountFreeBlocks(adfvolume)<2) ) { floppycontext->hxc_printf(MSG_ERROR,"Error while writting the file %s. No more free block ?",FindFileData.filename); adfCloseFile(adffile); fclose(ftemp); free(fullpath); return 1; } filesize=filesize-512; }while( (filesize>0) && (byte_written==size)); /*fileimg=(unsigned char*)malloc(filesize); memset(fileimg,0,filesize); fread(fileimg,filesize,1,ftemp); adfWriteFile(adffile, filesize, fileimg); free(fileimg);*/ adfCloseFile(adffile); fclose(ftemp); free(fullpath); } else { floppycontext->hxc_printf(MSG_ERROR,"Error : Cannot open %s !!!",fullpath); free(fullpath); return 1; } } } else { floppycontext->hxc_printf(MSG_ERROR,"Error : Cannot create %s, %dB!!!",FindFileData.filename,FindFileData.size); return 1; } } else { floppycontext->hxc_printf(MSG_ERROR,"Error : Cannot add a file : no more free block"); return 1; } } bbool=find_next_file(hfindfile,folder,file,&FindFileData); } } else printf("Error FindFirstFile\n"); find_close(hfindfile); return 0; }
void processFile(struct Volume *vol, char* name, char* path, unsigned char *extbuf, BOOL pflag, BOOL qflag) { char *sepptr, *cdstr, *fullname, *filename; char *bigstr; FILE *tfile; adfToRootDir(vol); sepptr = strchr(name, '/'); if (sepptr==NULL) { extractFile(vol, name, path, extbuf, pflag, qflag); } else { /* the all-in-one string : to call system(), to find the filename, the convert dir sep char ... */ bigstr=(char*)malloc(strlen(MKDIR)+1+strlen(path)+1+strlen(name)+1); if (!bigstr) { fprintf(stderr,"processFile : malloc"); return; } /* to build to extract path */ if (strlen(path)>0) { sprintf(bigstr,"%s %s%c%s",MKDIR,path,DIRSEP,name); cdstr = bigstr+strlen(MKDIR)+1+strlen(path)+1; } else { sprintf(bigstr,"%s %s",MKDIR,name); cdstr = bigstr+strlen(MKDIR)+1; } /* the directory in which the file will be extracted */ fullname = bigstr+strlen(MKDIR)+1; /* finds the filename, and separates it from the path */ filename = strrchr(bigstr,'/')+1; filename[-1]='\0'; sepptr = cdstr; /* find the end of the first dir to create */ while(sepptr[0]!='/' && sepptr[0]!='\0') sepptr++; while(strlen(cdstr)>0) { if (sepptr[0]=='/') { /* not the last one */ sepptr[0]='\0'; if (adfChangeDir(vol,cdstr)!=RC_OK) return; tfile = fopen(fullname,"r"); /* the only portable way to test if the dir exists */ if (tfile==NULL) { /* does't exist : create it */ if (!pflag) system(bigstr); if (!qflag) printf("x - %s%c\n",fullname,DIRSEP); } else fclose(tfile); sepptr[0] = DIRSEP; /* converts the '/' to '/' or '\' */ cdstr = sepptr+1; /* next beginning of the next dir to create */ /* to find the end of the next dir */ sepptr++; while(sepptr[0]!='/' && sepptr[0]!='\0') sepptr++; } else { /* the last one */ if (adfChangeDir(vol,cdstr)!=RC_OK) return; tfile = fopen(fullname,"r"); if (tfile==NULL) { if (!pflag) system(bigstr); if (!qflag) printf("x - %s%c\n",fullname,DIRSEP); } else fclose(tfile); cdstr = cdstr+strlen(cdstr); /* at the end, ends the while loop */ } } extractFile(vol, filename, fullname, extbuf, pflag, qflag); free(bigstr); } }
int main(int argc, char *argv[]) { struct Device *hd; struct Volume *vol; struct List *list, *cell; SECTNUM nSect; adfEnvInitDefault(); // adfSetEnvFct(0,0,MyVer,0); /* mount existing device */ /* testffs.adf */ hd = adfMountDev( argv[1],FALSE ); if (!hd) { fprintf(stderr, "can't mount device\n"); adfEnvCleanUp(); exit(1); } vol = adfMount(hd, 0, FALSE); if (!vol) { adfUnMountDev(hd); fprintf(stderr, "can't mount volume\n"); adfEnvCleanUp(); exit(1); } adfVolumeInfo(vol); cell = list = adfGetDirEnt(vol,vol->curDirPtr); while(cell) { printEntry(cell->content); cell = cell->next; } adfFreeDirList(list); putchar('\n'); /* cd dir_2 */ nSect = adfChangeDir(vol, "dir_2"); cell = list = adfGetDirEnt(vol,vol->curDirPtr); while(cell) { printEntry(cell->content); cell = cell->next; } adfFreeDirList(list); putchar('\n'); /* cd .. */ adfParentDir(vol); list = adfGetDirEnt(vol,vol->curDirPtr); while(list) { printEntry(list->content); adfFreeEntry(list->content); list = list->next; } freeList(list); adfUnMount(vol); adfUnMountDev(hd); adfEnvCleanUp(); return 0; }