Beispiel #1
0
bool CUserComponentValidator::applySave(const std::string& signature)
{
    NLMISC::COFile out;

    std::string::size_type index = NLMISC::CFile::getLastSeparator(_Filename);
    if (index != std::string::npos)
    {
        std::string dir = _Filename.substr(0, index);
        if (NLMISC::CFile::isExists(dir) == false )
        {
            if (NLMISC::CFile::createDirectory(dir) == false)
            {
                return false;
            }
        }
    }

    if (!out.open(_Filename, false, false, true))
    {

        nlwarning("Can't save: the file %s can not be saved.", _Filename.c_str());
        return false;
    }

    try
    {


        //std::stringstream out2;

        //out2.str("");
        //out2 << "---- Header\n";
        //out2 << NLMISC::toString("-- Version = '%u'\n", 1 );
        //out2 << NLMISC::toString("-- Signature = '%s'\n", signature.c_str() );
        //out2 << NLMISC::toString("-- HeaderMD5 = '%s'\n", _HeaderMd5.c_str() );
        //out2 << _HeaderBody;
        //out2 << "---- /Header\n\n";
        //out2 << _UserComponentBody;


        //out.serialBuffer((uint8*)out2.str().c_str(),out2.str().size());

        std::string out2;

        out2 += "---- Header\n";
        out2 += NLMISC::toString("-- Version = '%u'\n", 1 );
        out2 += NLMISC::toString("-- Signature = '%s'\n", signature.c_str() );
        out2 += NLMISC::toString("-- HeaderMD5 = '%s'\n", _HeaderMd5.c_str() );
        out2 += _HeaderBody;
        out2 += "---- /Header\n\n";
        out2 += _UserComponentBody;


        out.serialBuffer((uint8*)out2.c_str(),(uint)out2.size());
        if (_Filename != std::string("data/r2_buffer.dat") )
        {
            nlinfo("UserComponent %s saved", _Filename.c_str());
        }

        out.close();

        return true;
    }
    catch (...)
    {
        nlwarning("Can't save user component: the file %s can not be saved.", _Filename.c_str());
        out.close();

        return false;
    }

    return true;

}
IFileAccess::TReturnCode	CWriteFile::execute(CFileAccessManager& manager)
{
	bool	fileExists = NLMISC::CFile::fileExists(getBackupFileName(Filename));

	if (!fileExists)
	{
		if (checkFailureMode(MajorFailureIfFileNotExists))
		{
			FailureReason = NLMISC::toString("MAJOR_FAILURE:WRITE: file '%s' does not exist", Filename.c_str());
			return MajorFailure;
		}
		if (checkFailureMode(MinorFailureIfFileNotExists))
		{
			FailureReason = NLMISC::toString("MINOR_FAILURE:WRITE: file '%s' does not exist", Filename.c_str());
			return MinorFailure;
		}
	}
	else
	{
		if (checkFailureMode(MajorFailureIfFileExists))
		{
			FailureReason = NLMISC::toString("MAJOR_FAILURE:WRITE: file '%s' already exists", Filename.c_str());
			return MajorFailure;
		}
		if (checkFailureMode(MinorFailureIfFileExists))
		{
			FailureReason = NLMISC::toString("MINOR_FAILURE:WRITE: file '%s' already exists", Filename.c_str());
			return MinorFailure;
		}
	}

	if (CreateDir)
	{
		std::string	dir = NLMISC::CFile::getPath(getBackupFileName(Filename));

		if (!NLMISC::CFile::isExists(dir))
		{
			if (!NLMISC::CFile::createDirectoryTree(dir) ||
				!NLMISC::CFile::setRWAccess(dir))
			{
				if (checkFailureMode(MajorFailureIfFileUnwritable))
				{
					FailureReason = NLMISC::toString("MAJOR_FAILURE:WRITE: can't open file '%s', failed to create directory", Filename.c_str());
					return MajorFailure;
				}
				FailureReason = NLMISC::toString("MINOR_FAILURE:WRITE: can't open file '%s', failed to create directory", Filename.c_str());
				return MinorFailure;
			}

			if (VerboseLog)
				nlinfo("Created directory tree '%s'", dir.c_str());
		}
	}

	// if can't open file, file is unwritable, failure in all cases (and no backup)
	// file is kept untouched
	NLMISC::COFile	f;
	bool	fileOpen = f.open(getBackupFileName(Filename), Append, false, UseTempFile);
	if (!fileOpen)
	{
		if (checkFailureMode(MajorFailureIfFileUnwritable))
		{
			FailureReason = NLMISC::toString("MAJOR_FAILURE:WRITE: can't open file '%s'", Filename.c_str());
			return MajorFailure;
		}
		FailureReason = NLMISC::toString("MINOR_FAILURE:WRITE: can't open file '%s'", Filename.c_str());
		return MinorFailure;
	}

	bool	fileSaved = false;

	try
	{
		f.serialBuffer(&(Data[0]), (uint)Data.size());
		fileSaved = true;

		if (VerboseLog)
			nlinfo("%s %u octets to file '%s'", Append ? "Append" : "Save", Data.size(), Filename.c_str());
	}
	catch(const NLMISC::Exception &)
	{
	}

	// if can't write in file, file is unwritable, failure in all cases (and no backup)
	// file is kept untouched
	if (!fileSaved && checkFailureMode(MajorFailureIfFileUnwritable))
	{
		if (checkFailureMode(MajorFailureIfFileUnwritable))
		{
			FailureReason = NLMISC::toString("MAJOR_FAILURE:WRITE: can't write file '%s'", Filename.c_str());
			return MajorFailure;
		}
		FailureReason = NLMISC::toString("MINOR_FAILURE:WRITE: can't write file '%s'", Filename.c_str());
		return MinorFailure;
	}

	// if backup failed
	//  -> if it is a major issue, don't write file
	//  -> else write file anyway
	bool	fileBackuped = true;
	if (fileExists && BackupFile)
	{
		if (!NLMISC::CFile::copyFile( getBackupFileName(Filename)+".backup", getBackupFileName(Filename)))
		{
			fileBackuped = false;
			if (checkFailureMode(MajorFailureIfFileUnbackupable))
			{
				FailureReason = NLMISC::toString("MAJOR_FAILURE:WRITE: can't backup file '%s'", Filename.c_str());
				return MajorFailure;
			}
		}
	}

	f.close();

	if (!fileBackuped)
	{
		FailureReason = NLMISC::toString("MINOR_FAILURE:WRITE: can't backup file '%s'", Filename.c_str());
		return MinorFailure;
	}

	NLMISC::COFile flog;
	std::string str = getBackupFileName(Filename)+"\n";
	if(str.find("characters")!=std::string::npos)
	{
		flog.open(getBackupFileName("new_save.txt"), true);
		flog.serialBuffer((uint8*)&(str[0]), str.size());
		flog.close();
	}

	return Success;
}