Changeset 28689 in vbox
- Timestamp:
- Apr 24, 2010 6:23:57 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/cpp/xml.h
r28199 r28689 1 1 /** @file 2 * VirtualBox XML helper APIs.2 * IPRT - XML Helper APIs. 3 3 */ 4 4 5 5 /* 6 * Copyright (C) 2007-20 09Sun Microsystems, Inc.6 * Copyright (C) 2007-2010 Sun Microsystems, Inc. 7 7 * 8 8 * This file is part of VirtualBox Open Source Edition (OSE), as … … 28 28 */ 29 29 30 #ifndef ___ VBox_vboxxml_h31 #define ___ VBox_vboxxml_h30 #ifndef ___iprt_xml_h 31 #define ___iprt_xml_h 32 32 33 33 #ifndef IN_RING3 … … 288 288 * @param aMode File mode. 289 289 * @param aFileName File name. 290 */ 291 File(Mode aMode, const char *aFileName); 290 * @param aFlushIt Whether to flush a writable file before closing it. 291 */ 292 File(Mode aMode, const char *aFileName, bool aFlushIt = false); 292 293 293 294 /** … … 308 309 * @param aHandle Open file handle. 309 310 * @param aFileName File name (for reference). 310 */ 311 File(RTFILE aHandle, const char *aFileName = NULL); 311 * @param aFlushIt Whether to flush a writable file before closing it. 312 */ 313 File(RTFILE aHandle, const char *aFileName = NULL, bool aFlushIt = false); 312 314 313 315 /** … … 669 671 ~XmlFileWriter(); 670 672 671 void write(const char *pcszFilename); 673 /** 674 * Writes the XML document to the specified file. 675 * 676 * @param pcszFilename The name of the output file. 677 * @param fSafe If @c true, some extra safety precautions will be 678 * taken when writing the file: 679 * -# The file is written with a '-tmp' suffix. 680 * -# It is flushed to disk after writing. 681 * -# Any original file is renamed to '-prev'. 682 * -# The '-tmp' file is then renamed to the 683 * specified name. 684 * -# The directory changes are flushed to disk. 685 */ 686 void write(const char *pcszFilename, bool fSafe); 672 687 673 688 static int WriteCallback(void *aCtxt, const char *aBuf, int aLen); … … 675 690 676 691 private: 692 void writeInternal(const char *pcszFilename, bool fSafe); 693 677 694 /* Obscure class data */ 678 695 struct Data; … … 688 705 } // end namespace xml 689 706 690 #endif /* ___VBox_vboxxml_h */707 #endif /* !___iprt_xml_h */ -
trunk/src/VBox/Main/ApplianceImplExport.cpp
r28596 r28689 1548 1548 // now go write the XML 1549 1549 xml::XmlFileWriter writer(doc); 1550 writer.write(locInfo.strPath.c_str() );1550 writer.write(locInfo.strPath.c_str(), false /*fSafe*/); 1551 1551 1552 1552 /* Create & write the manifest file */ -
trunk/src/VBox/Main/xml/Settings.cpp
r28434 r28689 1293 1293 // now go write the XML 1294 1294 xml::XmlFileWriter writer(*m->pDoc); 1295 writer.write(m->strFilename.c_str() );1295 writer.write(m->strFilename.c_str(), true /*fSafe*/); 1296 1296 1297 1297 m->fFileExists = true; … … 4051 4051 // now go write the XML 4052 4052 xml::XmlFileWriter writer(*m->pDoc); 4053 writer.write(m->strFilename.c_str() );4053 writer.write(m->strFilename.c_str(), true /*fSafe*/); 4054 4054 4055 4055 m->fFileExists = true; -
trunk/src/VBox/Runtime/r3/xml.cpp
r28199 r28689 1 /* $Id$ */ 1 2 /** @file 2 * VirtualBoxXML Manipulation API.3 * IPRT - XML Manipulation API. 3 4 */ 4 5 5 6 /* 6 * Copyright (C) 2007-20 09Sun Microsystems, Inc.7 * Copyright (C) 2007-2010 Sun Microsystems, Inc. 7 8 * 8 9 * This file is part of VirtualBox Open Source Edition (OSE), as … … 28 29 */ 29 30 30 #include <iprt/cdefs.h> 31 #include <iprt/dir.h> 32 #include <iprt/file.h> 31 33 #include <iprt/err.h> 32 #include <iprt/file.h> 34 #include <iprt/param.h> 35 #include <iprt/path.h> 33 36 #include <iprt/cpp/lock.h> 34 37 #include <iprt/cpp/xml.h> … … 179 182 RTFILE handle; 180 183 bool opened : 1; 184 bool flushOnClose : 1; 181 185 }; 182 186 183 File::File(Mode aMode, const char *aFileName )187 File::File(Mode aMode, const char *aFileName, bool aFlushIt /* = false */) 184 188 : m(new Data()) 185 189 { 186 190 m->strFileName = aFileName; 191 m->flushOnClose = aFlushIt; 187 192 188 193 uint32_t flags = 0; … … 208 213 209 214 m->opened = true; 210 } 211 212 File::File(RTFILE aHandle, const char *aFileName /* = NULL */) 215 m->flushOnClose = aFlushIt && (flags & RTFILE_O_ACCESS_MASK) != RTFILE_O_READ; 216 } 217 218 File::File(RTFILE aHandle, const char *aFileName /* = NULL */, bool aFlushIt /* = false */) 213 219 : m(new Data()) 214 220 { 215 221 if (aHandle == NIL_RTFILE) 216 throw EInvalidArg 222 throw EInvalidArg(RT_SRC_POS); 217 223 218 224 m->handle = aHandle; … … 221 227 m->strFileName = aFileName; 222 228 223 setPos (0); 229 m->flushOnClose = aFlushIt; 230 231 setPos(0); 224 232 } 225 233 226 234 File::~File() 227 235 { 236 if (m->flushOnClose) 237 { 238 RTFileFlush(m->handle); 239 if (!m->strFileName.isEmpty()) 240 RTDirFlushParent(m->strFileName.c_str()); 241 } 242 228 243 if (m->opened) 229 244 RTFileClose(m->handle); … … 253 268 254 269 /* check if we overflow int64_t and move to INT64_MAX first */ 255 if (( (int64_t) aPos)< 0)270 if ((int64_t)aPos < 0) 256 271 { 257 272 vrc = RTFileSeek(m->handle, INT64_MAX, method, &p); … … 1360 1375 iprt::MiniString error; 1361 1376 1362 IOContext(const char *pcszFilename, File::Mode mode )1363 : file(mode, pcszFilename )1377 IOContext(const char *pcszFilename, File::Mode mode, bool fFlush = false) 1378 : file(mode, pcszFilename, fFlush) 1364 1379 { 1365 1380 } … … 1386 1401 struct WriteContext : IOContext 1387 1402 { 1388 WriteContext(const char *pcszFilename )1389 : IOContext(pcszFilename, File::Mode_Overwrite )1403 WriteContext(const char *pcszFilename, bool fFlush) 1404 : IOContext(pcszFilename, File::Mode_Overwrite, fFlush) 1390 1405 { 1391 1406 } … … 1473 1488 } 1474 1489 1475 void XmlFileWriter::write (const char *pcszFilename)1476 { 1477 WriteContext context(pcszFilename );1490 void XmlFileWriter::writeInternal(const char *pcszFilename, bool fSafe) 1491 { 1492 WriteContext context(pcszFilename, fSafe); 1478 1493 1479 1494 GlobalLock lock; … … 1507 1522 } 1508 1523 1524 void XmlFileWriter::write(const char *pcszFilename, bool fSafe) 1525 { 1526 if (!fSafe) 1527 writeInternal(pcszFilename, fSafe); 1528 else 1529 { 1530 /* Empty string and directory spec must be avoid. */ 1531 if (RTPathFilename(pcszFilename) == NULL) 1532 throw xml::LogicError(RT_SRC_POS); 1533 1534 /* Construct both filenames first to ease error handling. */ 1535 char szTmpFilename[RTPATH_MAX]; 1536 int rc = RTStrCopy(szTmpFilename, sizeof(szTmpFilename) - sizeof("-tmp") + 1, pcszFilename); 1537 if (RT_FAILURE(rc)) 1538 throw EIPRTFailure(rc, "RTStrCopy"); 1539 strcat(szTmpFilename, "-tmp"); 1540 1541 char szPrevFilename[RTPATH_MAX]; 1542 rc = RTStrCopy(szPrevFilename, sizeof(szPrevFilename) - sizeof("-prev") + 1, pcszFilename); 1543 if (RT_FAILURE(rc)) 1544 throw EIPRTFailure(rc, "RTStrCopy"); 1545 strcat(szPrevFilename, "-prev"); 1546 1547 /* Write the XML document to the temporary file. */ 1548 writeInternal(szTmpFilename, fSafe); 1549 1550 /* Make a backup of any existing file (ignore failure). */ 1551 uint64_t cbPrevFile; 1552 rc = RTFileQuerySize(pcszFilename, &cbPrevFile); 1553 if (RT_SUCCESS(rc) && cbPrevFile >= 16) 1554 RTFileRename(pcszFilename, szPrevFilename, RTPATHRENAME_FLAGS_REPLACE); 1555 1556 /* Commit the temporary file. Just leave the tmp file behind on failure. */ 1557 rc = RTFileRename(szTmpFilename, pcszFilename, RTPATHRENAME_FLAGS_REPLACE); 1558 if (RT_FAILURE(rc)) 1559 throw EIPRTFailure(rc, "Failed to replace '%s' with '%s'", pcszFilename, szTmpFilename); 1560 1561 /* Flush the directory changes (required on linux at least). */ 1562 RTPathStripFilename(szTmpFilename); 1563 rc = RTDirFlush(szTmpFilename); 1564 AssertMsg(RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED || rc == VERR_NOT_IMPLEMENTED, ("%Rrc\n", rc)); 1565 } 1566 } 1567 1509 1568 int XmlFileWriter::WriteCallback(void *aCtxt, const char *aBuf, int aLen) 1510 1569 {
Note:
See TracChangeset
for help on using the changeset viewer.