当前位置: 首页>>代码示例>>C++>>正文


C++ BFile::Lock方法代码示例

本文整理汇总了C++中BFile::Lock方法的典型用法代码示例。如果您正苦于以下问题:C++ BFile::Lock方法的具体用法?C++ BFile::Lock怎么用?C++ BFile::Lock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BFile的用法示例。


在下文中一共展示了BFile::Lock方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1:

status_t
Model::_SaveHistory(const BList& items) const
{
	BFile file;
	status_t status = _OpenFile(&file, PREFS_FILE,
		B_CREATE_FILE | B_WRITE_ONLY | B_ERASE_FILE,
		B_USER_SETTINGS_DIRECTORY, NULL);
	if (status != B_OK)
		return status;

	status = file.Lock();
	if (status != B_OK)
		return status;

	BMessage message;
	int32 count = items.CountItems();
	for (int32 i = 0; i < count; i++) {
		BString* string = static_cast<BString*>(items.ItemAtFast(i));

		if (message.AddString("string", string->String()) != B_OK)
			break;
	}

	status = message.Flatten(&file);
	file.SetSize(message.FlattenedSize());
	file.Sync();
	file.Unlock();

	return status;
}
开发者ID:looncraz,项目名称:haiku,代码行数:30,代码来源:Model.cpp

示例2: new

bool
Model::_LoadHistory(BList& items) const
{
	BFile file;
	status_t status = _OpenFile(&file, PREFS_FILE);
	if (status != B_OK)
		return false;

	status = file.Lock();
	if (status != B_OK)
		return false;

	BMessage message;
	status = message.Unflatten(&file);
	if (status != B_OK)
		return false;

	file.Unlock();

	BString string;
	for (int32 x = 0; message.FindString("string", x, &string) == B_OK; x++) {
		BString* copy = new (nothrow) BString(string);
		if (copy == NULL || !items.AddItem(copy)) {
			delete copy;
			break;
		}
	}

	return true;
}
开发者ID:looncraz,项目名称:haiku,代码行数:30,代码来源:Model.cpp

示例3: sizeof

status_t
Model::LoadPrefs()
{
	BFile file;
	status_t status = _OpenFile(&file, PREFS_FILE);
	if (status != B_OK)
		return status;

	status = file.Lock();
	if (status != B_OK)
		return status;

	int32 value;

	if (file.ReadAttr("RecurseDirs", B_INT32_TYPE, 0, &value,
			sizeof(int32)) > 0)
		fRecurseDirs = (value != 0);

	if (file.ReadAttr("RecurseLinks", B_INT32_TYPE, 0, &value,
			sizeof(int32)) > 0)
		fRecurseLinks = (value != 0);

	if (file.ReadAttr("SkipDotDirs", B_INT32_TYPE, 0, &value,
			sizeof(int32)) > 0)
		fSkipDotDirs = (value != 0);

	if (file.ReadAttr("CaseSensitive", B_INT32_TYPE, 0, &value,
			sizeof(int32)) > 0)
		fCaseSensitive = (value != 0);

	if (file.ReadAttr("EscapeText", B_INT32_TYPE, 0, &value, sizeof(int32)) > 0)
		fEscapeText = (value != 0);

	if (file.ReadAttr("TextOnly", B_INT32_TYPE, 0, &value, sizeof(int32)) > 0)
		fTextOnly = (value != 0);

	if (file.ReadAttr("InvokePe", B_INT32_TYPE, 0, &value, sizeof(int32)) > 0)
		fInvokePe = (value != 0);

	if (file.ReadAttr("ShowContents", B_INT32_TYPE, 0, &value,
			sizeof(int32)) > 0)
		fShowContents = (value != 0);

	char buffer [B_PATH_NAME_LENGTH+1];
	int32 length = file.ReadAttr("FilePanelPath", B_STRING_TYPE, 0, &buffer,
		sizeof(buffer));
	if (length > 0) {
		buffer[length] = '\0';
		fFilePanelPath = buffer;
	}

	file.ReadAttr("WindowFrame", B_RECT_TYPE, 0, &fFrame, sizeof(BRect));

	if (file.ReadAttr("Encoding", B_INT32_TYPE, 0, &value, sizeof(int32)) > 0)
		fEncoding = value;

	file.Unlock();

	return B_OK;
}
开发者ID:mariuz,项目名称:haiku,代码行数:60,代码来源:Model.cpp

示例4: SyncMailbox

void BMailRemoteStorageProtocol::SyncMailbox(const char *mailbox) {
	BPath path(runner->Chain()->MetaData()->FindString("path"));
	path.Append(mailbox);
	
	BDirectory folder(path.Path());
	
	BEntry entry;
	BFile snoodle;
	BString string;
	uint32 chain;
	bool append;
	
	while (folder.GetNextEntry(&entry) == B_OK) {
		if (!entry.IsFile())
			continue;
		while (snoodle.SetTo(&entry,B_READ_WRITE) == B_BUSY) snooze(100);
		append = false;
		
		while (snoodle.Lock() != B_OK) snooze(100);
		snoodle.Unlock();
		
		if (snoodle.ReadAttr("MAIL:chain",B_INT32_TYPE,0,&chain,sizeof(chain)) < B_OK)
			append = true;
		if (chain != runner->Chain()->ID())
			append = true;
		if (snoodle.ReadAttrString("MAIL:unique_id",&string) < B_OK)
			append = true;

		BString folder(string), id("");
		int32 j = string.FindLast('/');
		if ((!append) && (j >= 0)) {
			folder.Truncate(j);
			string.CopyInto(id,j + 1,string.Length());
			if (folder == mailbox)
				continue;
		} else {
			append = true;
		}
		
		if (append)
			AddMessage(mailbox,&snoodle,&id); //---We should check for partial messages here
		else
			CopyMessage(folder.String(),mailbox,&id);
			
		string = mailbox;
		string << '/' << id;
		/*snoodle.RemoveAttr("MAIL:unique_id");
		snoodle.RemoveAttr("MAIL:chain");*/
		chain = runner->Chain()->ID();
		snoodle.WriteAttr("MAIL:chain",B_INT32_TYPE,0,&chain,sizeof(chain));
		snoodle.WriteAttrString("MAIL:unique_id",&string);
		(*manifest) += string.String();
		(*unique_ids) += string.String();
		string = runner->Chain()->Name();
		snoodle.WriteAttrString("MAIL:account",&string);
	}
}
开发者ID:HaikuArchives,项目名称:BeMailDaemon,代码行数:57,代码来源:RemoteStorageProtocol.cpp

示例5: sizeof

status_t
Model::SavePrefs()
{
	BFile file;
	status_t status = _OpenFile(&file, PREFS_FILE,
		B_CREATE_FILE | B_WRITE_ONLY);
	if (status != B_OK)
		return status;

	status = file.Lock();
	if (status != B_OK)
		return status;

	int32 value = 2;
	file.WriteAttr("Version", B_INT32_TYPE, 0, &value, sizeof(int32));

	value = fRecurseDirs ? 1 : 0;
	file.WriteAttr("RecurseDirs", B_INT32_TYPE, 0, &value, sizeof(int32));

	value = fRecurseLinks ? 1 : 0;
	file.WriteAttr("RecurseLinks", B_INT32_TYPE, 0, &value, sizeof(int32));

	value = fSkipDotDirs ? 1 : 0;
	file.WriteAttr("SkipDotDirs", B_INT32_TYPE, 0, &value, sizeof(int32));

	value = fCaseSensitive ? 1 : 0;
	file.WriteAttr("CaseSensitive", B_INT32_TYPE, 0, &value, sizeof(int32));

	value = fRegularExpression ? 1 : 0;
	file.WriteAttr("RegularExpression", B_INT32_TYPE, 0, &value, sizeof(int32));

	value = fTextOnly ? 1 : 0;
	file.WriteAttr("TextOnly", B_INT32_TYPE, 0, &value, sizeof(int32));

	value = fInvokeEditor ? 1 : 0;
	file.WriteAttr("InvokeEditor", B_INT32_TYPE, 0, &value, sizeof(int32));

	file.WriteAttr("WindowFrame", B_RECT_TYPE, 0, &fFrame, sizeof(BRect));

	file.WriteAttr("FilePanelPath", B_STRING_TYPE, 0, fFilePanelPath.String(),
		fFilePanelPath.Length() + 1);

	value = fShowLines ? 1 : 0;
	file.WriteAttr("ShowLines", B_INT32_TYPE, 0, &value, sizeof(int32));

	file.WriteAttr("Encoding", B_INT32_TYPE, 0, &fEncoding, sizeof(int32));

	file.Sync();
	file.Unlock();

	return B_OK;
}
开发者ID:looncraz,项目名称:haiku,代码行数:52,代码来源:Model.cpp

示例6: WriteMessageFile


//.........这里部分代码省略.........
	// We can't count on not clobbering the tempfile to gain
	// exclusivity because, if the system crashes between when
	// we create the tempfile an when we rename it, we will have
	// a zombie tempfile that will prevent us from doing any more
	// saves.
	//
	// What we can do is:
	//
	//    Create or open the tempfile
	//    // At this point no one will *clobber* the file, but
	//    // others may open it
	//    Lock the tempfile
	//    // At this point, no one else may open it and we have
	//    // exclusive access to it.  Because of the above, we
	//    // know that our entry is still valid
	//
	//    Truncate the tempfile
	//    Write settings
	//    Sync
	//    Rename to the realfile
	//    // this does not affect the lock, but now open-
	//    // ing the realfile will fail with B_BUSY
	//    Unlock
	//
	// If this code is the only code that changes these files,
	// then we are guaranteed that all realfiles will be valid
	// settings files.  I think that's the best we can do unless
	// we get the Relink() api.  An implementation of the above
	// follows.
	//
	
	// Create or open
	ret = B_TIMED_OUT;
	while (system_time() - now < timeout) //-ATT-no timeout arg. Setting by #define
	{
		ret = tmpfile.SetTo(&settings_entry, B_WRITE_ONLY | B_CREATE_FILE);
		if (ret != B_BUSY) break;
		
		// wait 1/100th second
		snooze((bigtime_t)1e4);
	}
	if (ret != B_OK)
	{
		fprintf(stderr, "Couldn't open '%s/%s' within the timeout period (%fs): %s\n",
			path.Path(), leaf.String(), (float)timeout/1e6, strerror(ret));
		return ret==B_BUSY? B_TIMED_OUT:ret;
	}
	
	// lock
	ret = B_TIMED_OUT;
	while (system_time() - now < timeout)
	{
		ret = tmpfile.Lock(); //-ATT-changed account_file to tmpfile. Is that allowed?
		if (ret != B_BUSY) break;
		
		// wait 1/100th second
		snooze((bigtime_t)1e4);
	}
	if (ret != B_OK)
	{
		fprintf(stderr, "Couldn't lock '%s/%s' in within the timeout period (%fs): %s\n",
			path.Path(), leaf.String(), (float)timeout/1e6, strerror(ret));
		// Can't remove it here, since it might be someone else's.
		// Leaving a zombie shouldn't cause any problems tho so
		// that's OK.
		return ret==B_BUSY? B_TIMED_OUT:ret;
	}
	
	// truncate
	tmpfile.SetSize(0);
	
	// write
	ret = archive.Flatten(&tmpfile);
	if (ret != B_OK)
	{
		fprintf(stderr, "Couldn't flatten settings to '%s/%s': %s\n",
			path.Path(), leaf.String(), strerror(ret));
		return ret;
	}
	
	// ensure it's actually writen
	ret = tmpfile.Sync();
	if (ret != B_OK)
	{
		fprintf(stderr, "Couldn't sync settings to '%s/%s': %s\n",
			path.Path(), leaf.String(), strerror(ret));
		return ret;
	}
	
	// clobber old settings
	ret = settings_entry.Rename(name,true);
	if (ret != B_OK)
	{
		fprintf(stderr, "Couldn't clobber old settings '%s/%s': %s\n",
			path.Path(), name, strerror(ret));
		return ret;
	}
	
	return B_OK;
}
开发者ID:HaikuArchives,项目名称:BeMailDaemon,代码行数:101,代码来源:numailkit.cpp


注:本文中的BFile::Lock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。