1
0
forked from 0ad/0ad

add vfs_exists, uncached_io, and change IO interface to value-return buffer param (void**)

This was SVN commit r671.
This commit is contained in:
janwas 2004-07-09 02:17:37 +00:00
parent 306053c994
commit 41d8db6f81
4 changed files with 66 additions and 13 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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 <size> bytes, starting at <ofs>, to/from the given file.
// try to transfer the next <size> 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 <size> 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;
}

View File

@ -101,6 +101,10 @@ extern int vfs_next_dirent(Handle hd, vfsDirEnt* ent, const char* filter);
// "<real_directory>/fn" or "<archive_name>/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 <size> bytes, starting at <ofs>.
// try to transfer the next <size> 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 <size> 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 <fn> into memory; return a memory handle to the
// buffer and its address/size. output parameters are zeroed on failure.