// Delete all of the partly reconstructed files
bool Par1Repairer::DeleteIncompleteTargetFiles(void) {
	list<Par1RepairerSourceFile*>::iterator sf = verifylist.begin();

	// Iterate through each file in the verification list
	while (sf != verifylist.end()) {
		Par1RepairerSourceFile *sourcefile = *sf;
		if (sourcefile->GetTargetExists()) {
			DiskFile *targetfile = sourcefile->GetTargetFile();

			// Close and delete the file
			if (targetfile->IsOpen())
				targetfile->Close();
			targetfile->Delete();

			// Forget the file
			diskfilemap.Remove(targetfile);

			delete targetfile;

			// There is no target file
			sourcefile->SetTargetExists(false);
			sourcefile->SetTargetFile(0);
		}

		++sf;
	}

	return true;
}
Exemple #2
0
bool Copy(File& srcFile, const char* destFilename)
{
	// Operate in 16k buffers.
	const DWORD BUFFER_SIZE = 16 * 1024;

	DWORD fileSize = (DWORD)srcFile.GetLength();

	// See if the destination file exists.
	DiskFile destFile;
	if (!destFile.Open(destFilename, File::MODE_CREATE | File::MODE_WRITEONLY))
		return false;

	// Allocate the buffer space.
	BYTE* buffer = (BYTE*)_alloca(BUFFER_SIZE);

	// Keep copying until there is no more file left to copy.
	while (fileSize > 0)
	{
		// Copy the minimum of BUFFER_SIZE or the fileSize.
		DWORD readSize = min(BUFFER_SIZE, fileSize);
		srcFile.Read(buffer, readSize);
		destFile.Write(buffer, readSize);
		fileSize -= readSize;
	}

	// Close the destination virtual file.
	destFile.Close();

	return true;
}
Exemple #3
0
Image::Image(const char* filename) : format(RGBA32) {
	printf("Image %s\n", filename);
	DiskFile file; file.open(filename, DiskFile::ReadMode);
	uint size = file.getSize();
	int comp;
	data = stbi_load_from_memory((u8*)file.readAll(), size, &width, &height, &comp, 4);
}
Exemple #4
0
bool Copy(File& srcFile, const char* destFilename)
{
	// Operate in 16k buffers.
	const uint32_t BUFFER_SIZE = 16 * 1024;

	uint32_t fileSize = (uint32_t)srcFile.GetLength();

	if (!Misc::PathCreate(destFilename))
		return false;

	// See if the destination file exists.
	DiskFile destFile;
	if (!destFile.Open(destFilename, File::MODE_CREATE | File::MODE_WRITEONLY))
		return false;

	// Allocate the buffer space.
	BYTE* buffer = (BYTE*)alloca(BUFFER_SIZE);

	// Keep copying until there is no more file left to copy.
	while (fileSize > 0) {
		// Copy the minimum of BUFFER_SIZE or the fileSize.
		uint32_t readSize = BUFFER_SIZE < fileSize ? BUFFER_SIZE : fileSize;
		if (srcFile.Read(buffer, readSize) != readSize)
			return false;
		destFile.Write(buffer, readSize);
		fileSize -= readSize;
	}

	// Close the destination virtual file.
	destFile.Close();

	return true;
}
Exemple #5
0
bool RarVolume::Read()
{
	debug("Checking file %s", *m_filename);

	DiskFile file;
	if (!file.Open(m_filename, DiskFile::omRead))
	{
		return false;
	}

	m_version = DetectRarVersion(file);
	file.Seek(0);

	bool ok = false;

	switch (m_version)
	{
		case 3:
			ok = ReadRar3Volume(file);
			break;

		case 5:
			ok = ReadRar5Volume(file);
			break;
	}

	file.Close();
	DecryptFree();

	LogDebugInfo();

	return ok;
}
Exemple #6
0
	result WavStream::load(const char *aFilename)
	{
		delete[] mFilename;
		delete mMemFile;
		mMemFile = 0;
		mFilename = 0;
		mSampleCount = 0;
		DiskFile fp;
		int res = fp.open(aFilename);
		if (res != SO_NO_ERROR)
			return res;
		
		int len = (int)strlen(aFilename);
		mFilename = new char[len+1];		
		memcpy(mFilename, aFilename, len);
		mFilename[len] = 0;
		
		res = parse(&fp);

		if (res != SO_NO_ERROR)
		{
			delete[] mFilename;
			mFilename = 0;
			return res;
		}

		return 0;
	}
bool Par1Repairer::RemoveParFiles(void) {
	if (noiselevel > CommandLine::nlSilent
			&& parlist.size() > 0) {
			cout << endl << "Purge par files." << endl;
	}

	for (list<string>::const_iterator s=parlist.begin(); s!=parlist.end(); ++s) {
		DiskFile *diskfile = new DiskFile;

		if (diskfile->Open(*s)) {
			if (noiselevel > CommandLine::nlSilent) {
				string name;
				string path;
				DiskFile::SplitFilename((*s), path, name);
				cout << "Remove \"" << name << "\"." << endl;
			}

			if (diskfile->IsOpen())
				diskfile->Close();
			diskfile->Delete();
		}

		delete diskfile;
	}

	return true;
}
File* DiskFileDevice::open(const ansichar *path, FileModeFlags flags){
    DiskFile* file = eng_new(DiskFile)(this, path, flags);
	if (!file || !file->valid()) {
		eng_delete(file);
		return nullptr;
	}
    return file;
}
Exemple #9
0
	result Sfxr::loadParams(const char *aFilename)
	{
		DiskFile df;
		int res = df.open(aFilename);
		if (res != SO_NO_ERROR)
			return res;
		return loadParamsFile(&df);
	}
// Scan any extra files specified on the command line
bool Par1Repairer::VerifyExtraFiles(const list<CommandLine::ExtraFile> &extrafiles) {
	for (ExtraFileIterator i=extrafiles.begin();
			 i!=extrafiles.end() && completefilecount<sourcefiles.size();
			 ++i) {
		string filename = i->FileName();

		bool skip = false;

		// Find the file extension
		string::size_type where = filename.find_last_of('.');
		if (where != string::npos) {
			string tail = filename.substr(where+1);

			// Check the the file extension is the correct form
			if ((tail[0] == 'P' || tail[0] == 'p') &&
					(
						((tail[1] == 'A' || tail[1] == 'a') && (tail[2] == 'R' || tail[2] == 'r'))
						||
						(isdigit(tail[1]) && isdigit(tail[2]))
					)) {
				skip = true;
			}
		}

		if (!skip) {
			filename = DiskFile::GetCanonicalPathname(filename);

			// Has this file already been dealt with
			if (diskfilemap.Find(filename) == 0) {
				DiskFile *diskfile = new DiskFile;

				// Does the file exist
				if (!diskfile->Open(filename)) {
					delete diskfile;
					continue;
				}

				// Remember that we have processed this file
				bool success = diskfilemap.Insert(diskfile);
				assert(success);

				// Do the actual verification
				VerifyDataFile(diskfile, 0);
				// Ignore errors

				// We have finished with the file for now
				diskfile->Close();

				// Find out how much data we have found
				UpdateVerificationResults();
			}
		}
	}

	return true;
}
Exemple #11
0
	result Wav::load(const char *aFilename)
	{
		DiskFile dr;
		int res = dr.open(aFilename);
		if (res != SO_NO_ERROR)
        {
			return res;
        }
		return testAndLoadFile(&dr);
	}
Exemple #12
0
	result WavStream::loadToMem(const char *aFilename)
	{
		DiskFile df;
		int res = df.open(aFilename);
		if (res == SO_NO_ERROR)
		{
			res = loadFileToMem(&df);
		}
		return res;
	}
Exemple #13
0
	WavStreamInstance::WavStreamInstance(WavStream *aParent)
	{
		mParent = aParent;
		mOffset = 0;
		mOgg = 0;
		mFile = 0;
		if (aParent->mMemFile)
		{
			MemoryFile *mf = new MemoryFile();
			mFile = mf;
			mf->openMem(aParent->mMemFile->getMemPtr(), aParent->mMemFile->length(), false, false);
		}
		else
		if (aParent->mFilename)
		{
			DiskFile *df = new DiskFile;
			mFile = df;
			df->open(aParent->mFilename);
		}
		else
		if (aParent->mStreamFile)
		{
			mFile = aParent->mStreamFile;
			mFile->seek(0); // stb_vorbis assumes file offset to be at start of ogg
		}
		else
		{
			return;
		}
		
		if (mFile)
		{
			if (mParent->mOgg)
			{
				int e;

				mOgg = stb_vorbis_open_file((Soloud_Filehack *)mFile, 0, &e, 0);

				if (!mOgg)
				{
					if (mFile != mParent->mStreamFile)
						delete mFile;
					mFile = 0;
				}
				mOggFrameSize = 0;
				mOggFrameOffset = 0;
				mOggOutputs = 0;
			}
			else
			{		
				mFile->seek(aParent->mDataOffset);
			}
		}
	}
Exemple #14
0
	result Wav::load(const char *aFilename)
	{
		if (aFilename == 0)
			return INVALID_PARAMETER;
		stop();
		DiskFile dr;
		int res = dr.open(aFilename);
		if (res == SO_NO_ERROR)
			return loadFile(&dr);
		return res;
	}
Exemple #15
0
bool FileSystem::SaveBufferIntoFile(const char* filename, const char* buffer, int bufLen)
{
	DiskFile file;
	if (!file.Open(filename, DiskFile::omWrite))
	{
		return false;
	}

	int writtenBytes = (int)file.Write(buffer, bufLen);
	file.Close();

	return writtenBytes == bufLen;
}
Exemple #16
0
bool FileSystem::CopyFile(const char* srcFilename, const char* dstFilename)
{
	DiskFile infile;
	if (!infile.Open(srcFilename, DiskFile::omRead))
	{
		return false;
	}

	DiskFile outfile;
	if (!outfile.Open(dstFilename, DiskFile::omWrite))
	{
		return false;
	}

	CharBuffer buffer(1024 * 50);

	int cnt = buffer.Size();
	while (cnt == buffer.Size())
	{
		cnt = (int)infile.Read(buffer, buffer.Size());
		outfile.Write(buffer, cnt);
	}

	infile.Close();
	outfile.Close();

	return true;
}
Exemple #17
0
void Log::Filelog(const char* msg, ...)
{
	if (m_logFilename.Empty())
	{
		return;
	}

	char tmp2[1024];

	va_list ap;
	va_start(ap, msg);
	vsnprintf(tmp2, 1024, msg, ap);
	tmp2[1024-1] = '\0';
	va_end(ap);

	time_t rawtime = Util::CurrentTime() + g_Options->GetTimeCorrection();

	char time[50];
	Util::FormatTime(rawtime, time, 50);

	if ((int)rawtime/86400 != (int)m_lastWritten/86400 && g_Options->GetWriteLog() == Options::wlRotate)
	{
		RotateLog();
	}

	m_lastWritten = rawtime;

	DiskFile file;
	if (file.Open(m_logFilename, DiskFile::omAppend))
	{
#ifdef WIN32
		uint64 processId = GetCurrentProcessId();
		uint64 threadId = GetCurrentThreadId();
#else
		uint64 processId = (uint64)getpid();
		uint64 threadId = (uint64)pthread_self();
#endif
#ifdef DEBUG
		file.Print("%s\t%llu\t%llu\t%s%s", time, processId, threadId, tmp2, LINE_ENDING);
#else
		file.Print("%s\t%s%s", time, tmp2, LINE_ENDING);
#endif
		file.Close();
	}
	else
	{
		perror(m_logFilename);
	}
}
Exemple #18
0
void ParRenamer::CheckRegularFile(const char* destDir, const char* filename)
{
	debug("Computing hash for %s", filename);

	DiskFile file;
	if (!file.Open(filename, DiskFile::omRead))
	{
		PrintMessage(Message::mkError, "Could not open file %s", filename);
		return;
	}

	// load first 16K of the file into buffer
	static const int blockSize = 16*1024;
	CharBuffer buffer(blockSize);

	int readBytes = (int)file.Read(buffer, buffer.Size());
	if (readBytes != buffer.Size() && file.Error())
	{
		PrintMessage(Message::mkError, "Could not read file %s", filename);
		return;
	}

	file.Close();

	Par2::MD5Hash hash16k;
	Par2::MD5Context context;
	context.Update(buffer, readBytes);
	context.Final(hash16k);

	debug("file: %s; hash16k: %s", FileSystem::BaseFileName(filename), hash16k.print().c_str());

	for (FileHash& fileHash : m_fileHashList)
	{
		if (!strcmp(fileHash.GetHash(), hash16k.print().c_str()))
		{
			debug("Found correct filename: %s", fileHash.GetFilename());
			fileHash.SetFileExists(true);

			BString<1024> dstFilename("%s%c%s", destDir, PATH_SEPARATOR, fileHash.GetFilename());

			if (!FileSystem::FileExists(dstFilename) && !IsSplittedFragment(filename, fileHash.GetFilename()))
			{
				RenameFile(filename, dstFilename);
			}

			break;
		}
	}
}
Exemple #19
0
TEXLoader::IoStatus TEXLoader::Save(const str& file_name, const Texture& texture, bool compressed) {
	IoStatus io_status = kStatusSuccess;
	DiskFile file;
	save_file_ = &file;

	if (file.Open(file_name, DiskFile::kModeWrite) == false) {
		io_status = kStatusOpenError;
	}

	if (io_status == kStatusSuccess) {
		io_status = Save(texture, compressed);
	}

	return io_status;
}
Exemple #20
0
void ArticleWriter::SetWriteBuffer(DiskFile& outFile, int recSize)
{
	if (g_Options->GetWriteBuffer() > 0)
	{
		outFile.SetWriteBuffer(recSize > 0 && recSize < g_Options->GetWriteBuffer() * 1024 ?
			recSize : g_Options->GetWriteBuffer() * 1024);
	}
}
Exemple #21
0
bool FileSystem::LoadFileIntoBuffer(const char* filename, CharBuffer& buffer, bool addTrailingNull)
{
	DiskFile file;
	if (!file.Open(filename, DiskFile::omRead))
	{
		return false;
	}

	// obtain file size.
	file.Seek(0, DiskFile::soEnd);
	int size  = (int)file.Position();
	file.Seek(0);

	// allocate memory to contain the whole file.
	buffer.Reserve(size + (addTrailingNull ? 1 : 0));

	// copy the file into the buffer.
	file.Read(buffer, size);
	file.Close();

	if (addTrailingNull)
	{
		buffer[size] = 0;
	}

	return true;
}
Exemple #22
0
int lziparchive_fileinsert(lua_State* L) {
	ZipArchive* archive = lziparchive_check(L, 1);
	const char* srcFileName = luaL_checkstring(L, 2);
	const char* destFileName = luaL_checkstring(L, 3);
    int compressionMethod = (int)luaL_optnumber(L, 4, ZipArchive::DEFLATED);
    int compressionLevel = (int)luaL_optnumber(L, 5, Z_DEFAULT_COMPRESSION);

	DiskFile file;
    if (!file.Open(srcFileName, File::MODE_READONLY)) {
		lua_pushboolean(L, false);
		return 1;
	}

	time_t fileTime = (UINT)luaL_optnumber(L, 5, (lua_Number)file.GetLastWriteTime());

	lua_pushboolean(L, archive->FileCopy(file, destFileName, compressionMethod, compressionLevel, &fileTime) != 0);
	return 1;
}
Exemple #23
0
int LS_VirtualDrive_FileInsert(LuaState* state, LuaStackObject* args)
{
	VirtualDrive* drive = VirtualDriveFromLua(state, args[1]);
	const char* srcFileName = luaL_checkstring(*state, 2);
	const char* destFileName = luaL_checkstring(*state, 3);
    int compressionMethod = (UINT)luaL_optnumber(*state, 4, VirtualDrive::COMPRESSED);

	DiskFile file;
    if (!file.Open(srcFileName, File::MODE_READONLY))
	{
		state->PushBoolean(false);
		return 1;
	}

	time_t fileTime = (UINT)luaL_optnumber(*state, 5, (lua_Number)file.GetLastWriteTime());

	state->PushBoolean(drive->FileCopy(file, destFileName, compressionMethod, &fileTime) != 0);
	return 1;
}
//-----------------------------------------------------------------------------
void* TextResource::load(Allocator& allocator, Bundle& bundle, ResourceId id)
{
	DiskFile* stream = bundle.open(id);

	CE_ASSERT(stream != NULL, "Resource does not exist: %.8X%.8X", id.name, id.type);

	TextResource* resource = (TextResource*)allocator.allocate(sizeof(TextResource));

	stream->read(&resource->length, sizeof(uint32_t));
	
	resource->data = (char*)allocator.allocate(sizeof(char) * (resource->length + 1));

	stream->read(resource->data, (size_t)resource->length);
	
	resource->data[resource->length] = '\0';

	bundle.close(stream);

	return resource;
}
//-----------------------------------------------------------------------------
DiskFile* FileBundle::open(ResourceId name)
{
	// Convert name/type into strings
	char resource_name[512];
	snprintf(resource_name, 512, "%.8X%.8X", name.name, name.type);

	// Search the resource in the filesystem
	bool exists = m_filesystem.exists(resource_name);
	CE_ASSERT(exists == true, "Resource does not exist: %s", resource_name);

	// Open the resource and check magic number/version
	DiskFile* file = (DiskFile*)m_filesystem.open(resource_name, FOM_READ);

	ResourceHeader header;
	file->read(&header, sizeof(ResourceHeader));

	CE_ASSERT(header.magic == RESOURCE_MAGIC_NUMBER, "Resource is not valid: %s", resource_name);
	CE_ASSERT(header.version == RESOURCE_VERSION, "Resource version mismatch: %s", resource_name);

	return file;
}
Exemple #26
0
void ParRenamer::CheckParFile(const char* destDir, const char* filename)
{
	debug("Checking par2-header for %s", filename);

	DiskFile file;
	if (!file.Open(filename, DiskFile::omRead))
	{
		PrintMessage(Message::mkError, "Could not open file %s", filename);
		return;
	}

	// load par2-header
	Par2::PACKET_HEADER header;

	int readBytes = (int)file.Read(&header, sizeof(header));
	if (readBytes != sizeof(header) && file.Error())
	{
		PrintMessage(Message::mkError, "Could not read file %s", filename);
		return;
	}

	file.Close();

	// Check the packet header
	if (Par2::packet_magic != header.magic ||          // not par2-file
		sizeof(Par2::PACKET_HEADER) > header.length || // packet length is too small
		0 != (header.length & 3) ||              // packet length is not a multiple of 4
		FileSystem::FileSize(filename) < (int)header.length)       // packet would extend beyond the end of the file
	{
		// not par2-file or damaged header, ignoring the file
		return;
	}

	BString<100> setId = header.setid.print().c_str();
	for (char* p = setId; *p; p++) *p = tolower(*p); // convert string to lowercase

	debug("Storing: %s; setid: %s", FileSystem::BaseFileName(filename), *setId);

	m_parInfoList.emplace_back(filename, setId);
}
// Work out which files are being repaired, create them, and allocate
// target DataBlocks to them, and remember them for later verification.
bool Par1Repairer::CreateTargetFiles(void) {
	vector<Par1RepairerSourceFile*>::iterator sf = sourcefiles.begin();

	// Create any missing target files
	while (sf != sourcefiles.end()) {
		Par1RepairerSourceFile *sourcefile = *sf;

		// If the file does not exist
		if (!sourcefile->GetTargetExists()) {
			DiskFile *targetfile = new DiskFile;
			string filename = sourcefile->FileName();
			u64 filesize = sourcefile->FileSize();

			// Create the target file
			if (!targetfile->Create(filename, filesize)) {
				delete targetfile;
				return false;
			}

			// This file is now the target file
			sourcefile->SetTargetExists(true);
			sourcefile->SetTargetFile(targetfile);

			// Remember this file
			bool success = diskfilemap.Insert(targetfile);
			assert(success);

			sourcefile->SetTargetBlock(targetfile);

			// Add the file to the list of those that will need to be verified
			// once the repair has completed.
			verifylist.push_back(sourcefile);
		}

		++sf;
	}

	return true;
}
// Verify that all of the reconstructed target files are now correct
bool Par1Repairer::VerifyTargetFiles(void) {
	bool finalresult = true;

	// Verify the target files in alphabetical order
//  sort(verifylist.begin(), verifylist.end(), SortSourceFilesByFileName);

	// Iterate through each file in the verification list
	for (list<Par1RepairerSourceFile*>::iterator sf = verifylist.begin();
			 sf != verifylist.end();
			 ++sf) {
		Par1RepairerSourceFile *sourcefile = *sf;
		DiskFile *targetfile = sourcefile->GetTargetFile();

		// Close the file
		if (targetfile->IsOpen())
			targetfile->Close();

		// Say we don't have a complete version of the file
		sourcefile->SetCompleteFile(0);

		// Re-open the target file
		if (!targetfile->Open()) {
			finalresult = false;
			continue;
		}

		// Verify the file again
		if (!VerifyDataFile(targetfile, sourcefile))
			finalresult = false;

		// Close the file again
		targetfile->Close();

		// Find out how much data we have found
		UpdateVerificationResults();
	}

	return finalresult;
}
Exemple #29
0
void DiskDir::load()
{
    _files.clear();
    _subdirs.clear();
    // TODO: cache existing files and keep them unless they do no longer exist

    StringList li;
    GetFileList(fullname(), li);
    for(StringList::iterator it = li.begin(); it != li.end(); ++it)
    {
        DiskFile *f = new DiskFile(joinPath(fullname(), it->c_str()).c_str());
        _files[f->name()] = f;
    }

    li.clear();
    GetDirList(fullname(), li, 0);
    for(StringList::iterator it = li.begin(); it != li.end(); ++it)
    {
        // GetDirList() returns relative paths, so need to join
        Dir *d = createNew(joinPath(fullname(), it->c_str()).c_str());
        _subdirs[d->name()] = d;
    }
}
Exemple #30
0
int RarVolume::DetectRarVersion(DiskFile& file)
{
	static char RAR3_SIGNATURE[] = { 0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x00 };
	static char RAR5_SIGNATURE[] = { 0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x01, 0x00 };

	char fileSignature[8];

	int cnt = 0;
	cnt = (int)file.Read(fileSignature, sizeof(fileSignature));

	bool rar5 = cnt == sizeof(fileSignature) && !strcmp(RAR5_SIGNATURE, fileSignature);
	bool rar3 = !rar5 && cnt == sizeof(fileSignature) && !strcmp(RAR3_SIGNATURE, fileSignature);

	return rar3 ? 3 : rar5 ? 5 : 0;
}