Ps2WriteFile::~Ps2WriteFile(void) {
	if ((_fd >= 0) && (_bytesInCache)) {
		fio.write(_fd, _cacheBuf, _bytesInCache);
		int wrRes = fio.sync(_fd);
		if (wrRes != (int)_bytesInCache) // too late to return an error
			printf("Cache flush on fclose(): Unable to write %d cached bytes to mc, only %d bytes written\n", _bytesInCache, wrRes);
	}
	if (_fd >= 0)
		fio.close(_fd);
	free(_cacheBuf);
}
Beispiel #2
0
Ps2File::~Ps2File() {
	uint32 w;
	if (_fd >= 0) {

		if (_mode != O_RDONLY) {
			fio.seek(_fd, 0, SEEK_SET);
			fio.write(_fd, _cacheBuf, _filePos);
			w = fio.sync(_fd);
			dbg_printf("flushed wbuf: %x of %x\n", w, _filePos);
		}

		fio.close(_fd);
		uint32 r = fio.sync(_fd);
		dbg_printf("close [%d] - sync'd = %d\n", _fd, r);
	}

	free(_cacheBuf);

#ifdef __PS2_FILE_SEMA__
	DeleteSema(_sema);
#endif
}
uint32 Ps2WriteFile::write(const void *src, uint32 len) {
	uint32 size = len;
	uint8 *srcBuf = (uint8*)src;
	while (size) {
		uint32 doCpy = (len > CACHE_SIZE - _bytesInCache) ? (CACHE_SIZE - _bytesInCache) : len;
		if (doCpy) {
			memcpy(_cacheBuf + _bytesInCache, srcBuf, doCpy);
			_bytesInCache += doCpy;
			srcBuf += doCpy;
			size -= doCpy;
		}

		if (_bytesInCache == CACHE_SIZE) {
			fio.write(_fd, _cacheBuf, _bytesInCache);
			if (fio.sync(_fd) != (int)_bytesInCache) {
				printf("Unable to flush %d cached bytes to memory card!\n", _bytesInCache);
				return 0;
			}
			_filePos += _bytesInCache;
			_bytesInCache = 0;
		}
	}
	return len;
}