Exemplo n.º 1
0
size_t abstract_fstream::size() const {
	return (size_t)_filelengthi64(_fileno(FH));
}
Exemplo n.º 2
0
 static TtakBool size(void *cookie, TtakInt64 *len)
 {
     int fd = static_cast<int>(reinterpret_cast<intptr_t>(cookie));
     *len = _filelengthi64(fd);
     return *len >= 0LL;
 }
__ptr_t
__mmap64 (__ptr_t addr, size_t len, int prot, int flags, int fd,
		__off64_t offset)
{
	__ptr_t map = (__ptr_t) NULL;
	caddr_t gran_addr = (caddr_t) addr;
	HANDLE handle = INVALID_HANDLE_VALUE;
	DWORD cfm_flags = 0, mvf_flags = 0, sysgran = getgranularity ();
	__off64_t gran_offset = offset, filelen = _filelengthi64(fd);
	int mmlen = len;
	
	switch (prot) {
		case PROT_READ | PROT_WRITE | PROT_EXEC:
		case PROT_WRITE | PROT_EXEC:
			cfm_flags = PAGE_EXECUTE_READWRITE;
			mvf_flags = FILE_MAP_ALL_ACCESS;
			break;
		case PROT_READ | PROT_WRITE:
			cfm_flags = PAGE_READWRITE;
			mvf_flags = FILE_MAP_ALL_ACCESS;
			break;
		case PROT_WRITE:
			cfm_flags = PAGE_READWRITE;
			mvf_flags = FILE_MAP_WRITE;
			break;
		case PROT_READ:
			cfm_flags = PAGE_READONLY;
			mvf_flags = FILE_MAP_READ;
			break;
		case PROT_NONE:
			cfm_flags = PAGE_NOACCESS;
			mvf_flags = FILE_MAP_READ;
			break;
		case PROT_EXEC:
			cfm_flags = PAGE_EXECUTE;
			mvf_flags = FILE_MAP_READ;
			break;
	}
	if (flags & MAP_PRIVATE) {
		if (IsWin9x ())
			cfm_flags = PAGE_WRITECOPY;
		mvf_flags = FILE_MAP_COPY;
	}
	fprintf (stderr, "Addr before:   %p\n", gran_addr);
	fprintf (stderr, "Offset before: %#I64X\n", gran_offset);
	if (flags & MAP_FIXED) {
		gran_offset = offset;
		gran_addr = addr;
	}
	else {
		gran_offset = offset & ~(sysgran - 1);
		gran_addr = (caddr_t) (((DWORD) gran_addr / sysgran) * sysgran);
	}
	fprintf (stderr, "Addr after:    %p\n", gran_addr);
	fprintf (stderr, "Offset after:  %#I64X\n", gran_offset);
	mmlen = (filelen < gran_offset + len ? filelen - gran_offset : len);

	handle = CreateFileMapping ((HANDLE) _get_osfhandle(fd), NULL, cfm_flags,
		0, mmlen, NULL);
	if (!handle) {
		set_werrno;
		WinErr ("CreateFileMapping");
		return MAP_FAILED;
	}
	map = (__ptr_t) MapViewOfFileEx (handle, mvf_flags, HIDWORD(gran_offset),
		LODWORD(gran_offset), (SIZE_T) mmlen, (LPVOID) gran_addr);
	if (map == NULL && (flags & MAP_FIXED) ) {
		fprintf (stderr, "Starting address: %p\n", (LPVOID) gran_addr);
		WinErr ("First try of MapViewOfFileEx failed");
		map = (__ptr_t) MapViewOfFileEx (handle, mvf_flags, HIDWORD(gran_offset),
			LODWORD(gran_offset), (SIZE_T) mmlen, (LPVOID) NULL);
	}
	CloseHandle(handle);
	if (map == NULL) {
		set_werrno;
		WinErr ("MapViewOfFileEx");
		return MAP_FAILED;
	}
	return map;
}
Exemplo n.º 4
0
CFile::FILESIZE CWinFile::GetSize() const
{
  ASSERT(IsValid());
  return _filelengthi64(m_hFile);
}
Exemplo n.º 5
0
 static sf_count_t size(void *cookie)
 {
     int fd = static_cast<int>(reinterpret_cast<intptr_t>(cookie));
     return _filelengthi64(fd);
 }
Exemplo n.º 6
0
//-----------------------------------------------------------------------------
// Purpose: total size of pack file in megabytes
//-----------------------------------------------------------------------------
int Pack_GetSizeMB()
{
	return (int)(_filelengthi64(g_Pack.m_hPack) / (1024 * 1024));
}
Exemplo n.º 7
0
//-----------------------------------------------------------------------------
// Purpose: Starts adding a file to the packfile 
//-----------------------------------------------------------------------------
bool Pack_StartAppendingFile(const char *pakfile, const char *pFileName)
{
	memset(&g_Pack, 0, sizeof(g_Pack));

	if (strlen(pFileName) >= 112)
	{
		printf("Couldn't append file '%s', filename is too long (> 128 characters)\n", pFileName);
		return false;
	}
	strncpy(g_Pack.m_szNewFileName, pFileName, sizeof(g_Pack.m_szNewFileName));

	g_Pack.m_hPack = _open(pakfile, _O_RDWR | _O_BINARY);
	if (g_Pack.m_hPack == -1)
	{
		printf("Couldn't open pakfile '%s'\n", pakfile);
		return false;
	}

	g_Pack.m_iOldPakFileSize = _filelengthi64(g_Pack.m_hPack);

	// jump to the end of the file, see if it has a header
	packappenededheader_t appended;
	_lseeki64(g_Pack.m_hPack, (long)(0 - sizeof(packappenededheader_t)), SEEK_END);
	_read(g_Pack.m_hPack, &appended, sizeof(appended));

	if (!(appended.id[0] == 'P'
		&& appended.id[1] == 'A'
		&& appended.id[2] == 'C'
		&& appended.id[3] == 'K'
		&& appended.id[4] == 'A'
		&& appended.id[5] == 'P'
		&& appended.id[6] == 'P'
		&& appended.id[7] == 'E'))
	{
		printf("Invalid pack file '%s'\n", pakfile);
		return false;
	}

	g_Pack.m_iOldHeaderPos = appended.packheaderpos;
	g_Pack.m_iOriginalFileSize = appended.originalfilesize;

	// read in the pack file header
	packheader64_t header;
	_lseeki64(g_Pack.m_hPack, appended.packheaderpos, SEEK_SET);
	_read(g_Pack.m_hPack, &header, sizeof(header));
	g_Pack.m_iFilesInPack = (int)(header.dirlen / sizeof(packfile64_t));
	g_Pack.m_iFilesInPack += 1;
	
	// read in the directory
	g_Pack.m_pPackFiles = new packfile64_t[g_Pack.m_iFilesInPack];
	if (header.dirlen)
	{
		_lseeki64(g_Pack.m_hPack, appended.packheaderpos + header.dirofs, SEEK_SET);
		_read(g_Pack.m_hPack, g_Pack.m_pPackFiles, (long)header.dirlen);
	}

	// we're going to put the new file where the current packheader is, and move the header forward in the file

	// set back to the header position to write the new file
	_lseeki64(g_Pack.m_hPack, appended.packheaderpos, SEEK_SET);

	return true;
}