// Check for existence of file bool FileSpecifier::Exists() { // Check whether the file is readable err = 0; if (access(GetPath(), R_OK) < 0) err = errno; #ifdef HAVE_ZZIP if (err) { // Check whether zzip can open the file (slow!) ZZIP_FILE* file = zzip_open(unix_path_separators(GetPath()).c_str(), R_OK); if (file) { zzip_close(file); return true; } else { return false; } } #endif return (err == 0); }
//// read a file contents from either a archive of real directory void b_zzip_read(task *tsk, pntr *argstack) { char *fileName; pntr p = argstack[0]; int badtype; CHECK_ARG(0, CELL_CONS); if((badtype = array_to_string(p, &fileName)) >= 0){ set_error(tsk, "error1: argument is not a string (contains non-char: %s)", cell_types[badtype]); return; } ZZIP_FILE* fp = zzip_open (fileName, O_RDONLY|O_BINARY); if (! fp){ perror (fileName); } int bufSize = 2, blockSize = 1024, numBlocks = 1; char buf[bufSize]; int n, counter = 0; char *contents = (char *)calloc(blockSize, sizeof(char)); /* read chunks of bufSize bytes into buf and concatenate them into previous string */ while( (n = (zzip_read(fp, buf, bufSize-1))) > 0){ counter++; if(counter == 1){ // strcpy(contents, buf); strncat(contents, buf, bufSize-1); bufSize = 21; }else{ int originalSize = strlen(contents); if( ((blockSize*numBlocks) - (originalSize + 1)) < bufSize){ numBlocks++; contents = string_mkroom(contents, blockSize*numBlocks); } strncat(contents, buf, bufSize-1); // printf("%s\n\n\n", contents); } buf[n] = '\0'; } argstack[0] = string_to_array(tsk, contents); zzip_close(fp); }
/** start next file entry in a zip archive * * This function will create a new file within a zzip archive, the * one given as the primary argument and additionally to the posix * creat(2) - just like zzip_mkdir has an additional argument over * the posix mkdir(2) spec. For this function the primary parameter * can be null as well thereby creating a real file instead of a new * one inside the zip-archive otherwise given. If the primary parameter is * not null but wraps a real directory then all new files are also real. * * This function is not yet implemented, check for #def ZZIP_NO_CREAT * * Returns NULL on an error setting errno, and opening a file _within_ * a zip archive using O_RDONLY (and similar stuff) will surely lead to * an error. */ ZZIP_FILE * zzip_file_creat(ZZIP_DIR * dir, zzip_char_t * name, int o_mode) { if (! dir) return zzip_open(name, o_mode); if (! _ZZIP_TRY) { /* not implemented */ errno = EROFS; return 0; } else { errno = EROFS; return 0; } }
/** ** Generate a filename into library. ** ** Try current directory, user home directory, global directory. ** This supports .gz, .bz2 and .zip. ** ** @param file Filename to open. ** @param buffer Allocated buffer for generated filename. ** ** @return Pointer to buffer. */ global char* LibraryFileName(const char* file,char* buffer) { #ifdef USE_ZZIPLIB ZZIP_FILE* zp; #endif char* s; // // Absolute path or in current directory. // strcpy(buffer,file); if( *buffer=='/' || !access(buffer,R_OK) ) { return buffer; } #ifdef USE_ZLIB // gzip or bzip2 in current directory sprintf(buffer,"%s.gz",file); if( !access(buffer,R_OK) ) { return buffer; } #endif #ifdef USE_BZ2LIB sprintf(buffer,"%s.bz2",file); if( !access(buffer,R_OK) ) { return buffer; } #endif #ifdef USE_ZZIPLIB strcpy(buffer,file); if( (zp=zzip_open(buffer,O_RDONLY|O_BINARY)) ) { zzip_close(zp); return buffer; } #endif // USE_ZZIPLIB // // Try in map directory // if( *CurrentMapPath ) { DebugLevel3Fn("Map path: %s\n" _C_ CurrentMapPath); if( *CurrentMapPath=='.' || *CurrentMapPath=='/' ) { strcpy(buffer,CurrentMapPath); if( (s=strrchr(buffer,'/')) ) { s[1]='\0'; } strcat(buffer,file); } else { strcpy(buffer,FreeCraftLibPath); if( *buffer ) { strcat(buffer,"/"); } strcat(buffer,CurrentMapPath); if( (s=strrchr(buffer,'/')) ) { s[1]='\0'; } strcat(buffer,file); } if( !access(buffer,R_OK) ) { return buffer; } #ifdef USE_ZLIB // gzip or bzip2 in map directory directory strcat(buffer,".gz"); if( !access(buffer,R_OK) ) { return buffer; } *strrchr(buffer,'.')='\0'; #endif #ifdef USE_BZ2LIB strcat(buffer,".bz2"); if( !access(buffer,R_OK) ) { return buffer; } *strrchr(buffer,'.')='\0'; #endif #ifdef USE_ZZIPLIB if( (zp=zzip_open(buffer,O_RDONLY|O_BINARY)) ) { zzip_close(zp); return buffer; } #endif // USE_ZZIPLIB } if( (s = getenv("HOME")) ) { // // In user home directory // sprintf(buffer,"%s/%s/%s",s,FREECRAFT_HOME_PATH,file); if( !access(buffer,R_OK) ) { return buffer; } #ifdef USE_ZLIB // gzip or bzip2 in user home directory sprintf(buffer,"%s/%s/%s.gz",s,FREECRAFT_HOME_PATH,file); if( !access(buffer,R_OK) ) { return buffer; } #endif #ifdef USE_BZ2LIB sprintf(buffer,"%s/%s/%s.bz2",s,FREECRAFT_HOME_PATH,file); if( !access(buffer,R_OK) ) { return buffer; } #endif #ifdef USE_ZZIPLIB sprintf(buffer,"%s/%s/%s",s,FREECRAFT_HOME_PATH,file); if( (zp=zzip_open(buffer,O_RDONLY|O_BINARY)) ) { zzip_close(zp); return buffer; } #endif // USE_ZZIPLIB } // // In global shared directory // sprintf(buffer,"%s/%s",FreeCraftLibPath,file); if( !access(buffer,R_OK) ) { return buffer; } #ifdef USE_ZLIB // gzip or bzip2 in global shared directory sprintf(buffer,"%s/%s.gz",FreeCraftLibPath,file); if( !access(buffer,R_OK) ) { return buffer; } #endif #ifdef USE_BZ2LIB sprintf(buffer,"%s/%s.bz2",FreeCraftLibPath,file); if( !access(buffer,R_OK) ) { return buffer; } #endif #ifdef USE_ZZIPLIB sprintf(buffer,"%s/%s",FreeCraftLibPath,file); if( (zp=zzip_open(buffer,O_RDONLY|O_BINARY)) ) { zzip_close(zp); return buffer; } #endif // USE_ZZIPLIB DebugLevel0Fn("File `%s' not found\n" _C_ file); strcpy(buffer,file); return buffer; }
/** ** CLopen Library file open ** ** @param fn File name. */ global CLFile *CLopen(const char *fn) { CLFile input, *clf; char buf[512]; input.cl_type = CLF_TYPE_INVALID; if (!(input.cl_plain = fopen(fn, "rb"))) { // try plain first #ifdef USE_ZLIB if ((input.cl_gz = gzopen(strcat(strcpy(buf,fn),".gz"), "rb"))) { input.cl_type = CLF_TYPE_GZIP; } else #endif #ifdef USE_BZ2LIB if ((input.cl_bz = bzopen(strcat(strcpy(buf,fn),".bz2"), "rb"))) { input.cl_type = CLF_TYPE_BZIP2; } else #endif #ifdef USE_ZZIPLIB if ((input.cl_zz = zzip_open(strcpy(buf,fn),O_RDONLY|O_BINARY) )) { input.cl_type = CLF_TYPE_ZZIP; } else #endif { } } else { input.cl_type = CLF_TYPE_PLAIN; // Hmm, plain worked, but nevertheless the file may be compressed! if (fread(buf, 2, 1, input.cl_plain) == 1) { #ifdef USE_BZ2LIB if (buf[0] == 'B' && buf[1] == 'Z') { fclose(input.cl_plain); if ((input.cl_bz = bzopen(fn, "rb"))) { input.cl_type = CLF_TYPE_BZIP2; } else { if(!(input.cl_plain = fopen(fn, "rb"))) { input.cl_type = CLF_TYPE_INVALID; } } } #endif // USE_BZ2LIB #ifdef USE_ZLIB if (buf[0] == 0x1f) { // don't check for buf[1] == 0x8b, so that old compress also works! fclose(input.cl_plain); if ((input.cl_gz = gzopen(fn, "rb"))) { input.cl_type = CLF_TYPE_GZIP; } else { if(!(input.cl_plain = fopen(fn, "rb"))) { input.cl_type = CLF_TYPE_INVALID; } } } #endif // USE_ZLIB } if (input.cl_type == CLF_TYPE_PLAIN) { // ok, it is not compressed rewind(input.cl_plain); } } if (input.cl_type == CLF_TYPE_INVALID) { //fprintf(stderr,"%s in ", buf); return NULL; } // ok, here we go clf = (CLFile *)malloc(sizeof(CLFile)); if (clf) { *clf = input; } return clf; }
static int zip_openfile (lua_State *L) { ZZIP_FILE** inf; const char * ext2[LUAZIP_MAX_EXTENSIONS+1]; zzip_strings_t *ext = ext2; const char *filename = luaL_checkstring(L, 1); /*const char *mode = luaL_optstring(L, 2, "r");*/ inf = newinternalfile(L); if (lua_isstring(L, 2)) { /* creates a table with the string as the first and only (numerical) element */ lua_newtable(L); lua_pushvalue(L, 2); lua_rawseti(L, -2, 1); /* replaces the string by the table with the string inside */ lua_replace(L, 2); } if (lua_istable(L, 2)) { unsigned i, m, n; /* how many extension were specified? */ #if LUA_VERSION_NUM < 501 n = luaL_getn(L, 2); #else n = lua_rawlen(L, 2); #endif if (n > LUAZIP_MAX_EXTENSIONS) { luaL_error(L, "too many extensions specified"); } for (i = 0, m = 0; i < n; i++) { lua_rawgeti(L, 2, i+1); if (lua_isstring(L, -1)) { /* luazip specifies "zip" as the extension, but zziplib expects ".zip" */ lua_pushstring(L, "."); lua_insert(L, -2); lua_concat(L, 2); ext2[m] = lua_tostring(L, -1); m++; } lua_pop(L, 1); } ext2[m] = 0; *inf = zzip_open_ext_io(filename, 0, 0664, ext, 0); } else { *inf = zzip_open(filename, 0); } if (*inf) return 1; lua_pushnil(L); lua_pushfstring(L, "could not open file `%s'", filename); return 2; }