xref: /haiku/src/kits/storage/EntryOperationEngineBase.cpp (revision 6077cad882788bcf98d50dc596466b23ddb45c9e)
14cf3529cSIngo Weinhold /*
2*6077cad8SIngo Weinhold  * Copyright 2013-2014, Ingo Weinhold, ingo_weinhold@gmx.de.
34cf3529cSIngo Weinhold  * Distributed under the terms of the MIT License.
44cf3529cSIngo Weinhold  */
54cf3529cSIngo Weinhold 
64cf3529cSIngo Weinhold 
74cf3529cSIngo Weinhold #include <EntryOperationEngineBase.h>
84cf3529cSIngo Weinhold 
94cf3529cSIngo Weinhold #include <Directory.h>
104cf3529cSIngo Weinhold #include <Entry.h>
114cf3529cSIngo Weinhold #include <Path.h>
124cf3529cSIngo Weinhold 
134cf3529cSIngo Weinhold 
144cf3529cSIngo Weinhold namespace BPrivate {
154cf3529cSIngo Weinhold 
164cf3529cSIngo Weinhold 
Entry(const char * path)174cf3529cSIngo Weinhold BEntryOperationEngineBase::Entry::Entry(const char* path)
184cf3529cSIngo Weinhold 	:
194cf3529cSIngo Weinhold 	fDirectory(NULL),
204cf3529cSIngo Weinhold 	fPath(path),
214cf3529cSIngo Weinhold 	fEntry(NULL),
229680cf0bSIngo Weinhold 	fEntryRef(NULL),
239680cf0bSIngo Weinhold 	fDirectoryRef(NULL)
244cf3529cSIngo Weinhold {
254cf3529cSIngo Weinhold }
264cf3529cSIngo Weinhold 
274cf3529cSIngo Weinhold 
Entry(const BDirectory & directory,const char * path)289680cf0bSIngo Weinhold BEntryOperationEngineBase::Entry::Entry(const BDirectory& directory,
299680cf0bSIngo Weinhold 	const char* path)
304cf3529cSIngo Weinhold 	:
314cf3529cSIngo Weinhold 	fDirectory(&directory),
324cf3529cSIngo Weinhold 	fPath(path),
334cf3529cSIngo Weinhold 	fEntry(NULL),
349680cf0bSIngo Weinhold 	fEntryRef(NULL),
359680cf0bSIngo Weinhold 	fDirectoryRef(NULL)
364cf3529cSIngo Weinhold {
374cf3529cSIngo Weinhold }
384cf3529cSIngo Weinhold 
394cf3529cSIngo Weinhold 
Entry(const BEntry & entry)404cf3529cSIngo Weinhold BEntryOperationEngineBase::Entry::Entry(const BEntry& entry)
414cf3529cSIngo Weinhold 	:
424cf3529cSIngo Weinhold 	fDirectory(NULL),
434cf3529cSIngo Weinhold 	fPath(NULL),
444cf3529cSIngo Weinhold 	fEntry(&entry),
459680cf0bSIngo Weinhold 	fEntryRef(NULL),
469680cf0bSIngo Weinhold 	fDirectoryRef(NULL)
474cf3529cSIngo Weinhold {
484cf3529cSIngo Weinhold }
494cf3529cSIngo Weinhold 
504cf3529cSIngo Weinhold 
Entry(const entry_ref & entryRef)514cf3529cSIngo Weinhold BEntryOperationEngineBase::Entry::Entry(const entry_ref& entryRef)
524cf3529cSIngo Weinhold 	:
534cf3529cSIngo Weinhold 	fDirectory(NULL),
544cf3529cSIngo Weinhold 	fPath(NULL),
554cf3529cSIngo Weinhold 	fEntry(NULL),
569680cf0bSIngo Weinhold 	fEntryRef(&entryRef),
579680cf0bSIngo Weinhold 	fDirectoryRef(NULL)
589680cf0bSIngo Weinhold {
599680cf0bSIngo Weinhold }
609680cf0bSIngo Weinhold 
619680cf0bSIngo Weinhold 
Entry(const node_ref & directoryRef,const char * path)629680cf0bSIngo Weinhold BEntryOperationEngineBase::Entry::Entry(const node_ref& directoryRef,
639680cf0bSIngo Weinhold 	const char* path)
649680cf0bSIngo Weinhold 	:
659680cf0bSIngo Weinhold 	fDirectory(NULL),
669680cf0bSIngo Weinhold 	fPath(path),
679680cf0bSIngo Weinhold 	fEntry(NULL),
689680cf0bSIngo Weinhold 	fEntryRef(NULL),
699680cf0bSIngo Weinhold 	fDirectoryRef(&directoryRef)
704cf3529cSIngo Weinhold {
714cf3529cSIngo Weinhold }
724cf3529cSIngo Weinhold 
734cf3529cSIngo Weinhold 
~Entry()744cf3529cSIngo Weinhold BEntryOperationEngineBase::Entry::~Entry()
754cf3529cSIngo Weinhold {
764cf3529cSIngo Weinhold }
774cf3529cSIngo Weinhold 
784cf3529cSIngo Weinhold 
794cf3529cSIngo Weinhold status_t
GetPath(BPath & buffer,const char * & _path) const804cf3529cSIngo Weinhold BEntryOperationEngineBase::Entry::GetPath(BPath& buffer, const char*& _path)
814cf3529cSIngo Weinhold 	const
824cf3529cSIngo Weinhold {
83c8f47aeaSRene Gollent 	status_t error = B_OK;
844cf3529cSIngo Weinhold 
854cf3529cSIngo Weinhold 	if (fEntry != NULL) {
864cf3529cSIngo Weinhold 		error = buffer.SetTo(fEntry);
874cf3529cSIngo Weinhold 	} else if (fDirectory != NULL) {
884cf3529cSIngo Weinhold 		error = buffer.SetTo(fDirectory, fPath);
894cf3529cSIngo Weinhold 	} else if (fEntryRef != NULL) {
904cf3529cSIngo Weinhold 		error = buffer.SetTo(fEntryRef);
919680cf0bSIngo Weinhold 	} else if (fDirectoryRef != NULL) {
929680cf0bSIngo Weinhold 		BDirectory directory;
939680cf0bSIngo Weinhold 		error = directory.SetTo(fDirectoryRef);
949680cf0bSIngo Weinhold 		if (error == B_OK)
959680cf0bSIngo Weinhold 			error = buffer.SetTo(&directory, fPath);
964cf3529cSIngo Weinhold 	} else if (fPath != NULL) {
974cf3529cSIngo Weinhold 		_path = fPath;
984cf3529cSIngo Weinhold 		return B_OK;
994cf3529cSIngo Weinhold 	}
1004cf3529cSIngo Weinhold 
1014cf3529cSIngo Weinhold 	if (error != B_OK)
1024cf3529cSIngo Weinhold 		return error;
1034cf3529cSIngo Weinhold 
1044cf3529cSIngo Weinhold 	_path = buffer.Path();
1054cf3529cSIngo Weinhold 	return B_OK;
1064cf3529cSIngo Weinhold }
1074cf3529cSIngo Weinhold 
1084cf3529cSIngo Weinhold 
1094cf3529cSIngo Weinhold BString
Path() const1104cf3529cSIngo Weinhold BEntryOperationEngineBase::Entry::Path() const
1114cf3529cSIngo Weinhold {
1124cf3529cSIngo Weinhold 	BPath pathBuffer;
1134cf3529cSIngo Weinhold 	const char* path;
1144cf3529cSIngo Weinhold 	if (GetPath(pathBuffer, path) == B_OK)
1154cf3529cSIngo Weinhold 		return BString(path);
1164cf3529cSIngo Weinhold 	return BString();
1174cf3529cSIngo Weinhold }
1184cf3529cSIngo Weinhold 
1194cf3529cSIngo Weinhold 
120*6077cad8SIngo Weinhold status_t
GetPathOrName(BString & _path) const121*6077cad8SIngo Weinhold BEntryOperationEngineBase::Entry::GetPathOrName(BString& _path) const
122*6077cad8SIngo Weinhold {
123*6077cad8SIngo Weinhold 	_path.Truncate(0);
124*6077cad8SIngo Weinhold 
125*6077cad8SIngo Weinhold 	BPath buffer;
126*6077cad8SIngo Weinhold 	const char* path;
127*6077cad8SIngo Weinhold 	status_t error = GetPath(buffer, path);
128*6077cad8SIngo Weinhold 	if (error == B_NO_MEMORY)
129*6077cad8SIngo Weinhold 		return error;
130*6077cad8SIngo Weinhold 
131*6077cad8SIngo Weinhold 	if (error == B_OK) {
132*6077cad8SIngo Weinhold 		_path = path;
133*6077cad8SIngo Weinhold 	} else if (fEntry != NULL) {
134*6077cad8SIngo Weinhold 		// GetPath() apparently failed, so just return the entry name.
135*6077cad8SIngo Weinhold 		_path = fEntry->Name();
136*6077cad8SIngo Weinhold 	} else if (fDirectory != NULL || fDirectoryRef != NULL) {
137*6077cad8SIngo Weinhold 		if (fPath != NULL && fPath[0] == '/') {
138*6077cad8SIngo Weinhold 			// absolute path -- just return it
139*6077cad8SIngo Weinhold 			_path = fPath;
140*6077cad8SIngo Weinhold 		} else {
141*6077cad8SIngo Weinhold 			// get the directory path
142*6077cad8SIngo Weinhold 			BEntry entry;
143*6077cad8SIngo Weinhold 			if (fDirectory != NULL) {
144*6077cad8SIngo Weinhold 				error = fDirectory->GetEntry(&entry);
145*6077cad8SIngo Weinhold 			} else {
146*6077cad8SIngo Weinhold 				BDirectory directory;
147*6077cad8SIngo Weinhold 				error = directory.SetTo(fDirectoryRef);
148*6077cad8SIngo Weinhold 				if (error == B_OK)
149*6077cad8SIngo Weinhold 					error = directory.GetEntry(&entry);
150*6077cad8SIngo Weinhold 			}
151*6077cad8SIngo Weinhold 
152*6077cad8SIngo Weinhold 			if (error != B_OK || (error = entry.GetPath(&buffer)) != B_OK)
153*6077cad8SIngo Weinhold 				return error;
154*6077cad8SIngo Weinhold 
155*6077cad8SIngo Weinhold 			_path = buffer.Path();
156*6077cad8SIngo Weinhold 
157*6077cad8SIngo Weinhold 			// If we additionally have a relative path, append it.
158*6077cad8SIngo Weinhold 			if (!_path.IsEmpty() && fPath != NULL) {
159*6077cad8SIngo Weinhold 				int32 length = _path.Length();
160*6077cad8SIngo Weinhold 				_path << '/' << fPath;
161*6077cad8SIngo Weinhold 				if (_path.Length() < length + 2)
162*6077cad8SIngo Weinhold 					return B_NO_MEMORY;
163*6077cad8SIngo Weinhold 			}
164*6077cad8SIngo Weinhold 		}
165*6077cad8SIngo Weinhold 	} else if (fEntryRef != NULL) {
166*6077cad8SIngo Weinhold 		// Getting the actual path apparently failed, so just return the entry
167*6077cad8SIngo Weinhold 		// name.
168*6077cad8SIngo Weinhold 		_path = fEntryRef->name;
169*6077cad8SIngo Weinhold 	} else if (fPath != NULL)
170*6077cad8SIngo Weinhold 		_path = fPath;
171*6077cad8SIngo Weinhold 
172*6077cad8SIngo Weinhold 	return _path.IsEmpty() ? B_NO_MEMORY : B_OK;
173*6077cad8SIngo Weinhold }
174*6077cad8SIngo Weinhold 
175*6077cad8SIngo Weinhold 
176*6077cad8SIngo Weinhold BString
PathOrName() const177*6077cad8SIngo Weinhold BEntryOperationEngineBase::Entry::PathOrName() const
178*6077cad8SIngo Weinhold {
179*6077cad8SIngo Weinhold 	BString path;
180*6077cad8SIngo Weinhold 	return GetPathOrName(path) == B_OK ? path : BString();
181*6077cad8SIngo Weinhold }
182*6077cad8SIngo Weinhold 
183*6077cad8SIngo Weinhold 
1844cf3529cSIngo Weinhold } // namespace BPrivate
185