2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
24 //%% (c) Copyright 1993, 1994 International Business Machines Corp.
25 //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
26 //%% (c) Copyright 1993, 1994 Novell, Inc.
27 //%% $XConsortium: tt_db_file.C /main/3 1995/10/23 10:01:59 rswiston $
29 * tt_db_file.cc - Implement the TT DB server file class.
31 * Copyright (c) 1992 by Sun Microsystems, Inc.
33 * NOTE: Caching is not implemented. Therefore all of the cache levels
34 * are always set to -1 to force a full read from the database.
37 #include "util/tt_path.h"
38 #include "db/tt_db_client_consts.h"
39 #include "db/tt_db_file_utils.h"
40 #include "db/tt_db_network_path.h"
41 #include "db/tt_db_object_utils.h"
42 #include "db/tt_db_access.h"
43 #include "db/tt_db_property.h"
50 _Tt_db_file::_Tt_db_file (const _Tt_string &file)
53 dbResults = TT_DB_ERR_ILLEGAL_FILE;
56 _Tt_db_property_list_ptr properties;
57 _Tt_db_access_ptr access;
58 dbResults = setTtDBFileDefaults(file, properties, access);
62 _Tt_db_file::_Tt_db_file (const _Tt_string &file,
63 const _Tt_db_property_list_ptr &properties,
64 const _Tt_db_access_ptr &access)
67 dbResults = TT_DB_ERR_ILLEGAL_FILE;
70 dbResults = setTtDBFileDefaults(file, properties, access);
75 _Tt_db_file::setTtDBFileDefaults (const _Tt_string &file,
76 const _Tt_db_property_list_ptr &properties,
77 const _Tt_db_access_ptr &access)
79 checkedDatabase = FALSE;
80 directoryFlag = FALSE;
81 dbFileCurrentAccess = new _Tt_db_access;
82 dbFileObjects = new _Tt_string_list;
83 dbFileObjectsCacheLevel = -1;
84 dbFilePropertiesCacheLevel = -1;
85 dbFilePtypes = new _Tt_string_list;
88 if (access.is_null()) {
89 dbFileAccess = new _Tt_db_access;
92 dbFileAccess = access;
95 if (properties.is_null()) {
96 dbFileProperties = new _Tt_db_property_list;
99 dbFileProperties = properties;
103 _Tt_string local_path;
105 if (_tt_is_network_path(file)) {
106 dbFile = _tt_network_path_to_local_path(file);
107 dbFileNetworkPath = file;
108 (void)dbFileNetworkPath.split(':', dbFileHostname);
111 // Get the absoulte network information for the path
112 _Tt_string temp_string;
113 dbResults = _tt_db_network_path (file,
120 if ((dbResults == TT_DB_OK)) {
121 if (!isFileInDatabase()) {
122 if (dbResults != TT_DB_ERR_DB_OPEN_FAILED) {
123 dbResults = createDBFile();
132 _Tt_db_file::~_Tt_db_file ()
136 void _Tt_db_file::setCurrentAccess (const _Tt_db_access_ptr &access)
138 dbFileCurrentAccess = access;
141 _Tt_db_access_ptr _Tt_db_file::getCurrentAccess () const
143 return dbFileCurrentAccess;
146 _Tt_db_results _Tt_db_file::remove ()
148 if (dbFileNetworkPath.len()) {
149 setCurrentDBAccess ();
150 dbResults = dbFileDatabase->removeFile(dbFileNetworkPath);
152 if (dbResults == TT_DB_OK) {
153 dbFile = (char *)NULL;
154 dbFileNetworkPath = (char *)NULL;
155 dbFileProperties = (_Tt_db_property_list *)NULL;
156 dbFileAccess = (_Tt_db_access *)NULL;
160 dbResults = TT_DB_ERR_ILLEGAL_FILE;
163 return processDBResults();
166 _Tt_db_results _Tt_db_file::copy (const _Tt_string &new_file)
168 if (!new_file.len()) {
169 return (dbResults = TT_DB_ERR_ILLEGAL_FILE);
172 if (isFileInDatabase()) {
173 _Tt_string new_local_path;
174 _Tt_string new_hostname;
175 _Tt_string new_partition;
176 _Tt_string new_network_path;
177 dbResults = _tt_db_network_path(new_file,
183 if (dbResults == TT_DB_OK) {
184 // If we're not copying to the exact same file
185 if (dbFileNetworkPath != new_network_path) {
186 setCurrentDBAccess();
187 _Tt_db_property_list_ptr properties = getProperties();
188 _Tt_db_access_ptr access = getAccess ();
189 _Tt_string_list_ptr objids = getObjects();
191 if (dbResults == TT_DB_OK) {
192 _Tt_db_file_ptr new_db_file = new _Tt_db_file(new_network_path,
196 _Tt_string_list_cursor objids_cursor(objids);
197 while (objids_cursor.next()) {
198 _Tt_db_object_ptr object = new _Tt_db_object(*objids_cursor);
199 (void)object->copy(new_file);
204 dbResults = TT_DB_ERR_SAME_FILE;
209 dbResults = TT_DB_ERR_ILLEGAL_FILE;
215 _Tt_db_results _Tt_db_file::move (const _Tt_string &new_file)
217 if (!new_file.len()) {
218 return (dbResults = TT_DB_ERR_ILLEGAL_FILE);
221 if (isFileInDatabase()) {
222 // Get the hostname and remote path of the new file
223 _Tt_string new_local_path;
224 _Tt_string new_hostname;
225 _Tt_string new_partition;
226 _Tt_string new_network_path;
227 dbResults = _tt_db_network_path(new_file,
233 if (dbResults == TT_DB_OK) {
234 // If we're not moving to the exact same file
235 if (dbFileNetworkPath != new_network_path) {
236 setCurrentDBAccess();
238 if (dbResults == TT_DB_OK) {
239 // If is the new file is on the same machine...
240 if (dbFileHostname == new_hostname) {
241 // We're not allowed to move directories across partitions
242 if (directoryFlag && (dbFilePartition != new_partition)) {
243 dbResults = TT_DB_ERR_ILLEGAL_FILE;
246 dbResults = dbFileDatabase->moveFile(dbFileNetworkPath,
249 if (dbResults == TT_DB_OK) {
250 // Change the official file of this object
251 _Tt_db_property_list_ptr properties;
252 _Tt_db_access_ptr access;
253 dbResults = setTtDBFileDefaults(new_network_path,
259 // Else, the move is between different hosts...
261 // We're not allowed to move directories across machines
263 dbResults = TT_DB_ERR_ILLEGAL_FILE;
265 // Not a directory, move it...
267 _Tt_db_property_list_ptr properties = getProperties();
268 _Tt_db_access_ptr access = getAccess ();
269 _Tt_string_list_ptr objids = getObjects();
271 if (dbResults == TT_DB_OK) {
272 _Tt_db_file_ptr new_db_file = new _Tt_db_file(new_network_path,
276 _Tt_string_list_cursor objids_cursor(objids);
277 while (objids_cursor.next()) {
278 _Tt_db_object_ptr object = new _Tt_db_object(*objids_cursor);
279 (void)object->move(new_network_path);
284 if (dbResults == TT_DB_OK) {
285 dbResults = dbFileDatabase->removeFile(dbFileNetworkPath);
288 if (dbResults == TT_DB_OK) {
289 // Change the official file of this object
290 _Tt_db_property_list_ptr properties;
291 _Tt_db_access_ptr access;
292 dbResults = setTtDBFileDefaults(new_network_path,
300 dbResults = TT_DB_ERR_SAME_FILE;
305 dbResults = TT_DB_ERR_ILLEGAL_FILE;
311 _Tt_string _Tt_db_file::getHostname ()
313 return dbFileHostname;
317 _Tt_db_file::setProperty (const _Tt_db_property_ptr &property)
319 if (isFileInDatabase()) {
320 setCurrentDBAccess();
321 dbResults = dbFileDatabase->setFileProperty(dbFileNetworkPath,
323 dbFilePropertiesCacheLevel);
326 dbResults = TT_DB_ERR_ILLEGAL_FILE;
329 return processDBResults();
333 _Tt_db_file::setProperties (const _Tt_db_property_list_ptr &properties)
335 if (isFileInDatabase()) {
336 setCurrentDBAccess();
338 dbFileDatabase->setFileProperties(dbFileNetworkPath,
340 dbFilePropertiesCacheLevel);
343 dbResults = TT_DB_ERR_ILLEGAL_FILE;
346 return processDBResults();
350 _Tt_db_file::addProperty (const _Tt_db_property_ptr &property,
353 if (isFileInDatabase()) {
354 setCurrentDBAccess();
355 dbResults = dbFileDatabase->addFileProperty(dbFileNetworkPath,
358 dbFilePropertiesCacheLevel);
361 dbResults = TT_DB_ERR_ILLEGAL_FILE;
364 return processDBResults();
368 _Tt_db_file::deleteProperty (const _Tt_db_property_ptr &property)
370 if (isFileInDatabase()) {
371 setCurrentDBAccess();
373 dbFileDatabase->deleteFileProperty(dbFileNetworkPath,
375 dbFilePropertiesCacheLevel);
378 dbResults = TT_DB_ERR_ILLEGAL_FILE;
381 return processDBResults();
384 _Tt_db_property_ptr _Tt_db_file::getProperty (const _Tt_string &name)
386 _Tt_db_property_ptr property;
388 if (isFileInDatabase()) {
389 setCurrentDBAccess();
390 dbFilePropertiesCacheLevel = -1;
391 dbResults = dbFileDatabase->getFileProperty(dbFileNetworkPath,
393 dbFilePropertiesCacheLevel,
397 dbResults = TT_DB_ERR_ILLEGAL_FILE;
400 dbResults = processDBResults();
404 _Tt_db_property_list_ptr _Tt_db_file::getProperties ()
406 _Tt_db_property_list_ptr properties;
408 if (isFileInDatabase()) {
409 setCurrentDBAccess();
410 dbFilePropertiesCacheLevel = -1;
411 dbResults = dbFileDatabase->getFileProperties(dbFileNetworkPath,
412 dbFilePropertiesCacheLevel,
416 dbResults = TT_DB_ERR_ILLEGAL_FILE;
419 dbResults = processDBResults();
423 _Tt_string_list_ptr _Tt_db_file::getObjects ()
425 _Tt_string_list_ptr objids;
427 if (isFileInDatabase()) {
428 setCurrentDBAccess();
429 dbFileObjectsCacheLevel = -1;
430 dbResults = dbFileDatabase->getFileObjects(dbFileNetworkPath,
431 dbFileObjectsCacheLevel,
435 dbResults = TT_DB_ERR_ILLEGAL_FILE;
438 dbResults = processDBResults();
442 _Tt_db_results _Tt_db_file::setAccess (const _Tt_db_access_ptr &access)
444 if (isFileInDatabase()) {
445 setCurrentDBAccess();
446 dbResults = dbFileDatabase->setFileAccess(dbFileNetworkPath, access);
449 dbResults = TT_DB_ERR_ILLEGAL_FILE;
452 return processDBResults();
455 _Tt_db_access_ptr _Tt_db_file::getAccess ()
457 _Tt_db_access_ptr access;
459 if (isFileInDatabase()) {
460 setCurrentDBAccess();
461 dbResults = dbFileDatabase->getFileAccess(dbFileNetworkPath, access);
464 dbResults = TT_DB_ERR_ILLEGAL_FILE;
467 dbResults = processDBResults();
471 _Tt_db_results _Tt_db_file::addSession (const _Tt_string &session)
473 _Tt_db_property_ptr property = new _Tt_db_property;
474 property->name = TT_DB_SESSION_PROPERTY;
476 _Tt_string value = session;
477 (void)property->values->append(value);
479 return addProperty(property, TRUE);
482 _Tt_db_results _Tt_db_file::deleteSession (const _Tt_string &session)
484 _Tt_db_property_ptr property = new _Tt_db_property;
485 property->name = TT_DB_SESSION_PROPERTY;
487 _Tt_string value = session;
488 property->values->append(value);
490 return deleteProperty(property);
493 _Tt_string_list_ptr _Tt_db_file::getSessions ()
495 _Tt_db_property_ptr property;
496 _Tt_string_list_ptr sessions;
498 if (isFileInDatabase()) {
499 _Tt_string name(TT_DB_SESSION_PROPERTY);
500 property = getProperty(name);
501 if (dbResults == TT_DB_ERR_NO_SUCH_PROPERTY) {
502 dbResults = TT_DB_OK;
505 if (!property.is_null()) {
506 if (!property->is_empty()) {
507 sessions = new _Tt_string_list;
509 _Tt_string_list_cursor values_cursor(property->values);
510 while (values_cursor.next()) {
511 sessions->append(*values_cursor);
521 _Tt_db_file::queueMessage (const _Tt_string_list_ptr &ptypes,
522 const _Tt_message_ptr &message)
524 if (isFileInDatabase()) {
525 dbResults = dbFileDatabase->queueMessage(dbFileNetworkPath,
530 dbResults = TT_DB_ERR_ILLEGAL_FILE;
537 _Tt_db_file::dequeueMessages (const _Tt_string_list_ptr &ptypes,
538 _Tt_message_list_ptr &messages)
540 if (isFileInDatabase()) {
541 dbResults = dbFileDatabase->dequeueMessages(dbFileNetworkPath,
546 dbResults = TT_DB_ERR_ILLEGAL_FILE;
552 _Tt_string _Tt_db_file::getNetworkPath (const _Tt_string &file)
554 _Tt_string network_path;
557 if (_tt_is_network_path(file)) {
561 _Tt_string local_path;
563 _Tt_string partition;
565 _Tt_db_results results = _tt_db_network_path(file,
576 _Tt_db_results _Tt_db_file::createDBFile ()
578 if (!isFileInDatabase()) {
579 dbFileDatabase = dbHostnameGlobalMapRef.getDB(dbFileHostname,
582 if (dbFileDatabase.is_null()) {
586 int cache_level = -1;
588 setCurrentDBAccess ();
589 dbResults = dbFileDatabase->createFile(dbFileNetworkPath,
594 dbFileObjectsCacheLevel = cache_level;
595 dbFilePropertiesCacheLevel = cache_level;
598 dbResults = TT_DB_ERR_FILE_EXISTS;
601 return processDBResults();
604 bool_t _Tt_db_file::isFileInDatabase ()
606 // Property caching is not currently implemented in _Tt_db_file,
607 // so this function does not read the properties from the database
608 // if the file exists. If caching is added, this is where the
609 // cache should be initially filled from.
611 if (!checkedDatabase) {
612 checkedDatabase = TRUE;
614 dbFileDatabase = dbHostnameGlobalMapRef.getDB(dbFileHostname,
617 if (dbFileDatabase.is_null() || dbResults != TT_DB_OK) {
620 _Tt_db_access_ptr access;
621 dbResults = dbFileDatabase->isFileInDatabase(dbFileNetworkPath,
624 if (dbResults != TT_DB_OK) {
625 dbHostnameGlobalMapRef.removeDB(dbFileHostname);
626 dbFileDatabase = (_Tt_db_client *)NULL;
630 return (dbFileDatabase.is_null() ? FALSE : TRUE);
633 _Tt_db_results _Tt_db_file::processDBResults ()
635 // If an RPC fails, assume the DB server went down and attempt to
637 if ((dbResults == TT_DB_ERR_DB_CONNECTION_FAILED) ||
638 (dbResults == TT_DB_ERR_RPC_CONNECTION_FAILED) ||
639 (dbResults == TT_DB_ERR_RPC_FAILED) ||
640 (dbResults == TT_DB_ERR_DB_OPEN_FAILED)) {
641 if (!dbFileDatabase.is_null()) {
642 dbHostnameGlobalMapRef.removeDB(dbFileHostname);
643 dbFileDatabase = dbHostnameGlobalMapRef.getDB(dbFileHostname,