From 6077cad882788bcf98d50dc596466b23ddb45c9e Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Fri, 13 Jun 2014 17:00:14 +0200 Subject: [PATCH] BEntryOperationEngineBase::Entry: Add debug methods Add GetPathOrName() and PathOrName() methods which try to get some kind of usable path or at least a file name for the entry. Useful mainly for debugging and error reporting cases. --- .../storage/EntryOperationEngineBase.h | 8 ++- src/kits/storage/EntryOperationEngineBase.cpp | 66 ++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/headers/private/storage/EntryOperationEngineBase.h b/headers/private/storage/EntryOperationEngineBase.h index bcdee29e1c..ad8accf6e4 100644 --- a/headers/private/storage/EntryOperationEngineBase.h +++ b/headers/private/storage/EntryOperationEngineBase.h @@ -1,5 +1,5 @@ /* - * Copyright 2013, Haiku, Inc. All Rights Reserved. + * Copyright 2013-2014, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -44,6 +44,12 @@ public: const; BString Path() const; + status_t GetPathOrName(BString& _path) const; + // Tries to return some kind of string + // representation. Useful only for debugging + // and error reporting. + BString PathOrName() const; + private: const BDirectory* fDirectory; const char* fPath; diff --git a/src/kits/storage/EntryOperationEngineBase.cpp b/src/kits/storage/EntryOperationEngineBase.cpp index cf22cd90c6..55e747405e 100644 --- a/src/kits/storage/EntryOperationEngineBase.cpp +++ b/src/kits/storage/EntryOperationEngineBase.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de. + * Copyright 2013-2014, Ingo Weinhold, ingo_weinhold@gmx.de. * Distributed under the terms of the MIT License. */ @@ -117,4 +117,68 @@ BEntryOperationEngineBase::Entry::Path() const } +status_t +BEntryOperationEngineBase::Entry::GetPathOrName(BString& _path) const +{ + _path.Truncate(0); + + BPath buffer; + const char* path; + status_t error = GetPath(buffer, path); + if (error == B_NO_MEMORY) + return error; + + if (error == B_OK) { + _path = path; + } else if (fEntry != NULL) { + // GetPath() apparently failed, so just return the entry name. + _path = fEntry->Name(); + } else if (fDirectory != NULL || fDirectoryRef != NULL) { + if (fPath != NULL && fPath[0] == '/') { + // absolute path -- just return it + _path = fPath; + } else { + // get the directory path + BEntry entry; + if (fDirectory != NULL) { + error = fDirectory->GetEntry(&entry); + } else { + BDirectory directory; + error = directory.SetTo(fDirectoryRef); + if (error == B_OK) + error = directory.GetEntry(&entry); + } + + if (error != B_OK || (error = entry.GetPath(&buffer)) != B_OK) + return error; + + _path = buffer.Path(); + + // If we additionally have a relative path, append it. + if (!_path.IsEmpty() && fPath != NULL) { + int32 length = _path.Length(); + _path << '/' << fPath; + if (_path.Length() < length + 2) + return B_NO_MEMORY; + } + } + } else if (fEntryRef != NULL) { + // Getting the actual path apparently failed, so just return the entry + // name. + _path = fEntryRef->name; + } else if (fPath != NULL) + _path = fPath; + + return _path.IsEmpty() ? B_NO_MEMORY : B_OK; +} + + +BString +BEntryOperationEngineBase::Entry::PathOrName() const +{ + BString path; + return GetPathOrName(path) == B_OK ? path : BString(); +} + + } // namespace BPrivate