X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=pak.c;h=6fdd88b83a61b9e288b0849f2151c250ab288272;hb=d9572e3e303929e1b6d7267d43c0904dac3078f5;hp=abb389bc0a2fa30b245a42712b960125876e40e8;hpb=bee14a6df7100de5d68c1b53fc820c98eb9123cb;p=xonotic%2Fgmqcc.git diff --git a/pak.c b/pak.c index abb389b..6fdd88b 100644 --- a/pak.c +++ b/pak.c @@ -124,7 +124,7 @@ static void pak_tree_build(const char *entry) { typedef struct { pak_directory_t *directories; pak_header_t header; - FILE *handle; + fs_file_t *handle; bool insert; } pak_file_t; @@ -162,7 +162,7 @@ static pak_file_t *pak_open_read(const char *file) { * Time to read in the directory handles and prepare the directories * vector. We're going to be reading some the file inwards soon. */ - fs_file_seek(pak->handle, pak->header.diroff, SEEK_SET); + fs_file_seek(pak->handle, pak->header.diroff, FS_FILE_SEEK_SET); /* * Read in all directories from the PAK file. These are considered @@ -266,7 +266,7 @@ static bool pak_extract_one(pak_file_t *pak, const char *file, const char *outdi pak_directory_t *dir = NULL; unsigned char *dat = NULL; char *local = NULL; - FILE *out = NULL; + fs_file_t *out = NULL; if (!pak_exists(pak, file, &dir)) { return false; @@ -295,7 +295,7 @@ static bool pak_extract_one(pak_file_t *pak, const char *file, const char *outdi mem_d(local); /* read */ - if (fs_file_seek (pak->handle, dir->pos, SEEK_SET) != 0) + if (fs_file_seek (pak->handle, dir->pos, FS_FILE_SEEK_SET) != 0) goto err; fs_file_read (dat, 1, dir->len, pak->handle); @@ -304,7 +304,7 @@ static bool pak_extract_one(pak_file_t *pak, const char *file, const char *outdi mem_d(dat); return true; - + err: if (dat) mem_d(dat); if (out) fs_file_close(out); @@ -333,7 +333,7 @@ static bool pak_insert_one(pak_file_t *pak, const char *file) { pak_directory_t dir; unsigned char *dat; long len; - FILE *fp; + fs_file_t *fp; /* * We don't allow insertion on files that already exist within the @@ -351,11 +351,10 @@ static bool pak_insert_one(pak_file_t *pak, const char *file) { * the directory entry, and the actual contents of the file * to the PAK file itself. */ - if (fs_file_seek(fp, 0, SEEK_END) != 0 || ((len = fs_file_tell(fp)) < 0)) { - fs_file_close(fp); - return false; - } - fs_file_seek(fp, 0, SEEK_SET); + if (fs_file_seek(fp, 0, FS_FILE_SEEK_END) != 0 || ((len = fs_file_tell(fp)) < 0)) + goto err; + if (fs_file_seek(fp, 0, FS_FILE_SEEK_SET) != 0) + goto err; dir.len = len; dir.pos = fs_file_tell(pak->handle); @@ -364,10 +363,8 @@ static bool pak_insert_one(pak_file_t *pak, const char *file) { * We're limited to 56 bytes for a file name string, that INCLUDES * the directory and '/' seperators. */ - if (strlen(file) >= 56) { - fs_file_close(fp); - return false; - } + if (strlen(file) >= 56) + goto err; util_strncpy(dir.name, file, strlen(file)); @@ -375,10 +372,8 @@ static bool pak_insert_one(pak_file_t *pak, const char *file) { * Allocate some memory for loading in the data that will be * redirected into the PAK file. */ - if (!(dat = (unsigned char *)mem_a(dir.len))) { - fs_file_close(fp); - return false; - } + if (!(dat = (unsigned char *)mem_a(dir.len))) + goto err; fs_file_read (dat, dir.len, 1, fp); fs_file_close(fp); @@ -391,13 +386,18 @@ static bool pak_insert_one(pak_file_t *pak, const char *file) { vec_push(pak->directories, dir); return true; + +err: + fs_file_close(fp); + return false; } /* * Like pak_insert_one, except this collects files in all directories * from a root directory, and inserts them all. */ -bool pak_insert_all(pak_file_t *pak, const char *dir) { +#if 0 +static bool pak_insert_all(pak_file_t *pak, const char *dir) { DIR *dp; struct dirent *dirp; @@ -417,6 +417,7 @@ bool pak_insert_all(pak_file_t *pak, const char *dir) { fs_dir_close(dp); return true; } +#endif /*!if 0 renable when ready to use */ static bool pak_close(pak_file_t *pak) { size_t itr; @@ -437,18 +438,17 @@ static bool pak_close(pak_file_t *pak) { pak->header.diroff = tell; /* patch header */ - if (fs_file_seek (pak->handle, 0, SEEK_SET) != 0) + if (fs_file_seek (pak->handle, 0, FS_FILE_SEEK_SET) != 0) goto err; fs_file_write(&(pak->header), sizeof(pak_header_t), 1, pak->handle); /* write directories */ - if (fs_file_seek (pak->handle, pak->header.diroff, SEEK_SET) != 0) + if (fs_file_seek (pak->handle, pak->header.diroff, FS_FILE_SEEK_SET) != 0) goto err; - for (itr = 0; itr < vec_size(pak->directories); itr++) { + for (itr = 0; itr < vec_size(pak->directories); itr++) fs_file_write(&(pak->directories[itr]), sizeof(pak_directory_t), 1, pak->handle); - } } vec_free (pak->directories); @@ -495,6 +495,7 @@ static bool parsecmd(const char *optname, int *argc_, char ***argv_, char **out, return true; } +#include int main(int argc, char **argv) { bool extract = true; char *redirout = (char*)stdout;