From 41d8db6f819353da0615f4674df723a7c3388737 Mon Sep 17 00:00:00 2001 From: janwas Date: Fri, 9 Jul 2004 02:17:37 +0000 Subject: [PATCH] add vfs_exists, uncached_io, and change IO interface to value-return buffer param (void**) This was SVN commit r671. --- source/lib/res/file.cpp | 10 +++++--- source/lib/res/file.h | 2 +- source/lib/res/vfs.cpp | 53 +++++++++++++++++++++++++++++++++++------ source/lib/res/vfs.h | 14 +++++++++-- 4 files changed, 66 insertions(+), 13 deletions(-) diff --git a/source/lib/res/file.cpp b/source/lib/res/file.cpp index a9af1881a4..e7a2357a32 100755 --- a/source/lib/res/file.cpp +++ b/source/lib/res/file.cpp @@ -1328,14 +1328,18 @@ debug_out("file_io fd=%d size=%d ofs=%d\n", f->fd, raw_size, raw_ofs); } -int file_uncached_io(File* f, size_t size, void* p) +int file_uncached_io(File* f, off_t ofs, size_t size, void* p) { CHECK_FILE(f); + int fd = f->fd; + + lseek(fd, ofs, SEEK_SET); + if(f->flags & FILE_WRITE) - return write(f->fd, p, size); + return write(fd, p, size); else - return read(f->fd, p, size); + return read(fd, p, size); } diff --git a/source/lib/res/file.h b/source/lib/res/file.h index cf0ddaa100..318419fd60 100755 --- a/source/lib/res/file.h +++ b/source/lib/res/file.h @@ -143,7 +143,7 @@ typedef ssize_t(*FILE_IO_CB)(uintptr_t ctx, void* p, size_t size); extern ssize_t file_io(File* f, off_t ofs, size_t size, void** p, FILE_IO_CB cb = 0, uintptr_t ctx = 0); -extern int file_uncached_io(File* f, size_t size, void* p); +extern int file_uncached_io(File* f, off_t ofs, size_t size, void* p); #endif // #ifndef FILE_H diff --git a/source/lib/res/vfs.cpp b/source/lib/res/vfs.cpp index 606c562a1d..9663ef96e5 100755 --- a/source/lib/res/vfs.cpp +++ b/source/lib/res/vfs.cpp @@ -1081,6 +1081,15 @@ int vfs_realpath(const char* fn, char* full_path) } +// does the specified file exist? return false on error. +// useful because a "file not found" warning is not raised, unlike vfs_stat. +bool vfs_exists(const char* fn) +{ + const Loc* loc; + return (tree_lookup(fn, &loc) == 0); +} + + // return information about the specified file as in stat(2), // most notably size. stat buffer is undefined on error. int vfs_stat(const char* fn, struct stat* s) @@ -1122,6 +1131,8 @@ struct VFile // (can't just use pointer - may be freed behind our back) Handle hm; + off_t ofs; + union { File f; @@ -1249,18 +1260,21 @@ debug_out("vfs_close %I64x\n", h); } -// try to transfer bytes, starting at , to/from the given file. +// try to transfer the next bytes to/from the given file. // (read or write access was chosen at file-open time). // return bytes of actual data transferred, or a negative error code. // TODO: buffer types -ssize_t vfs_io(const Handle hf, const off_t ofs, const size_t size, void*& p) +ssize_t vfs_io(const Handle hf, const size_t size, void** p) { #ifdef PARANOIA -debug_out("vfs_io ofs=%d size=%d\n", ofs, size); +debug_out("vfs_io size=%d\n", size); #endif H_DEREF(hf, VFile, vf); + off_t ofs = vf->ofs; + vf->ofs += (off_t)size; + // (vfs_open makes sure it's not opened for writing if zip) if(vf_flags(vf) & VF_ZIP) return zip_read(&vf->zf, ofs, size, p); @@ -1268,7 +1282,32 @@ debug_out("vfs_io ofs=%d size=%d\n", ofs, size); // normal file: // let file_io alloc the buffer if the caller didn't (i.e. p = 0), // because it knows about alignment / padding requirements - return file_io(&vf->f, ofs, size, &p); + return file_io(&vf->f, ofs, size, p); +} + + +// try to transfer the next bytes to/from the given file. +// (read or write access was chosen at file-open time). +// return bytes of actual data transferred, or a negative error code. +ssize_t vfs_uncached_io(const Handle hf, const size_t size, void** p) +{ +#ifdef PARANOIA + debug_out("vfs_uncached_io size=%d\n", size); +#endif + + H_DEREF(hf, VFile, vf); + + off_t ofs = vf->ofs; + vf->ofs += (off_t)size; + + // (vfs_open makes sure it's not opened for writing if zip) + if(vf_flags(vf) & VF_ZIP) + return zip_read(&vf->zf, ofs, size, p); + + // normal file: + // let file_io alloc the buffer if the caller didn't (i.e. p = 0), + // because it knows about alignment / padding requirements + return file_uncached_io(&vf->f, ofs, size, *p); } @@ -1308,7 +1347,7 @@ debug_out("vfs_load fn=%s\n", fn); } { // VC6 goto fix - ssize_t nread = vfs_io(hf, 0, size, p); + ssize_t nread = vfs_io(hf, size, &p); if(nread > 0) hm = mem_assign(p, size); } @@ -1335,7 +1374,7 @@ int vfs_store(const char* const fn, void* p, const size_t size) if(hf <= 0) return (int)hf; // error code H_DEREF(hf, VFile, vf); - const int ret = vfs_io(hf, 0, size, p); + const int ret = vfs_io(hf, size, &p); vfs_close(hf); return ret; } @@ -1349,7 +1388,7 @@ int vfs_uncached_store(const char* const fn, void* p, const size_t size) if(hf <= 0) return (int)hf; // error code H_DEREF(hf, VFile, vf); - const int ret = file_uncached_io(&vf->f, size, p); + const int ret = file_uncached_io(&vf->f, 0, size, p); vfs_close(hf); return ret; } diff --git a/source/lib/res/vfs.h b/source/lib/res/vfs.h index efeb5f182d..b04526cd86 100755 --- a/source/lib/res/vfs.h +++ b/source/lib/res/vfs.h @@ -101,6 +101,10 @@ extern int vfs_next_dirent(Handle hd, vfsDirEnt* ent, const char* filter); // "/fn" or "/fn". extern int vfs_realpath(const char* fn, char* realpath); +// does the specified file exist? return false on error. +// useful because a "file not found" warning is not raised, unlike vfs_stat. +extern bool vfs_exists(const char* fn); + // return information about the specified file as in stat(2), // most notably size. stat buffer is undefined on error. extern int vfs_stat(const char* fn, struct stat*); @@ -156,11 +160,17 @@ extern int vfs_discard_io(Handle& hio); // synchronous I/O // -// try to transfer bytes, starting at . +// try to transfer the next bytes to/from the given file. // (read or write access was chosen at file-open time). // return bytes of actual data transferred, or a negative error code. // TODO: buffer types -extern ssize_t vfs_io(Handle hf, off_t ofs, size_t size, void*& p); +extern ssize_t vfs_io(Handle hf, size_t size, void** p); + +// try to transfer the next bytes to/from the given file. +// (read or write access was chosen at file-open time). +// return bytes of actual data transferred, or a negative error code. +extern ssize_t vfs_uncached_io(const Handle hf, const size_t size, void** p); + // load the entire file into memory; return a memory handle to the // buffer and its address/size. output parameters are zeroed on failure.