📄 instanceindexfile.cpp
字号:
fs.seekg(0); char hexString[9]; fs.read(hexString, 8); if (!fs) { PEG_METHOD_EXIT(); return false; } hexString[8] = '\0'; // // Convert hex string to integer: // char* end = 0; long tmp = strtol(hexString, &end, 16); if (!end || *end != '\0' || tmp < 0) { PEG_METHOD_EXIT(); return false; } freeCount = Uint32(tmp); // // Increment and rewrite the free count: // sprintf(hexString, "%08X", ++freeCount); fs.seekg(0); fs.write(hexString, 8); PEG_METHOD_EXIT(); return !!fs;}Boolean InstanceIndexFile::_openFile( const String& path, PEGASUS_STD(fstream)& fs, Boolean create){ PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::_openFile()"); const char ZERO_FREE_COUNT[] = "00000000\n"; // // Open the file: // if (!FileSystem::openNoCase( fs, path, ios::in | ios::out PEGASUS_OR_IOS_BINARY)) { if (create) { // // File does not exist so create it: //#if defined(PEGASUS_OS_OS400) fs.open( path.getCString(), ios::out PEGASUS_OR_IOS_BINARY, PEGASUS_STD(_CCSID_T(1208)));#else fs.open(path.getCString(), ios::out PEGASUS_OR_IOS_BINARY);#endif if (!fs) { PEG_METHOD_EXIT(); return false; } fs.write(ZERO_FREE_COUNT, sizeof(ZERO_FREE_COUNT) - 1); fs.close(); // // Reopen the file: // if (!FileSystem::openNoCase( fs, path, ios::in | ios::out PEGASUS_OR_IOS_BINARY)) { PEG_METHOD_EXIT(); return false; } } else { PEG_METHOD_EXIT(); return false; } } // // Position the file pointer beyond the free count: // fs.seekg(sizeof(ZERO_FREE_COUNT) - 1); PEG_METHOD_EXIT(); return true;}Boolean InstanceIndexFile::_appendEntry( PEGASUS_STD(fstream)& fs, const CIMObjectPath& instanceName, Uint32 indexIn, Uint32 sizeIn){ PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::_appendEntry()"); // // Position the file at the end: // fs.seekg(0, ios::end); if (!fs) { PEG_METHOD_EXIT(); return false; } // // Write the entry: // Uint32 targetHashCode = instanceName.makeHashCode(); char buffer[32]; sprintf(buffer, "%08X", targetHashCode); fs << "0 " << buffer << ' ' << indexIn << ' ' << sizeIn << ' '; // Calling getCString to ensure that utf-8 goes to the file // Calling write to ensure no data conversion by the stream CString name = instanceName.toString().getCString(); fs.write((const char *)name, static_cast<streamsize>(strlen((const char *)name))); fs << endl; PEG_METHOD_EXIT(); return !!fs;}Boolean InstanceIndexFile::_markEntryFree( PEGASUS_STD(fstream)& fs, const CIMObjectPath& instanceName){ PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::_markEntryFree()"); // // First look up the entry: // Uint32 index = 0; Uint32 size = 0; Uint32 entryOffset = 0; if (!InstanceIndexFile::_lookupEntry( fs, instanceName, index, size, entryOffset)) { PEG_METHOD_EXIT(); return false; } // // Now mark the entry as free (change the first character of the entry // from a '0' to a '1'). // fs.seekg(entryOffset); if (!fs) { PEG_METHOD_EXIT(); return false; } fs.write("1", 1); PEG_METHOD_EXIT(); return !!fs;}Boolean InstanceIndexFile::_lookupEntry( PEGASUS_STD(fstream)& fs, const CIMObjectPath& instanceName, Uint32& indexOut, Uint32& sizeOut, Uint32& entryOffset){ PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::_lookupEntry()"); indexOut = 0; sizeOut = 0; entryOffset = 0; // For for bugzilla 1508. Hostname and namespace are not included // in the comparison here. Instances in the repository index file // are generally stored without hostname and namespace. If the hostname // and namespace of the instance to look up would not match, we would have // not gotten here at all. CIMObjectPath shortInstanceName = instanceName; shortInstanceName.setNameSpace(CIMNamespaceName()); shortInstanceName.setHost(String::EMPTY); Uint32 targetHashCode = shortInstanceName.makeHashCode(); Buffer line; Uint32 freeFlag; Uint32 hashCode; const char* instanceNameTmp; Uint32 index; Uint32 size; Boolean error;#ifndef PEGASUS_OS_ZOS entryOffset = fs.tellp();#else entryOffset = getOffset(fs.tellp());#endif while (_GetNextRecord( fs, line, freeFlag, hashCode, index, size, instanceNameTmp, error)) {#ifdef PEGASUS_REPOSITORY_NOT_NORMALIZED // See bugzilla 1207. If the object paths in the repository // are not normalized, then the hashcodes cannot be used for // the look up (because the hash is based on the normalized path). if (freeFlag == 0 && CIMObjectPath(instanceNameTmp) == shortInstanceName)#else if (freeFlag == 0 && hashCode == targetHashCode && CIMObjectPath(instanceNameTmp) == shortInstanceName)#endif { indexOut = index; sizeOut = size; PEG_METHOD_EXIT(); return true; }#ifndef PEGASUS_OS_ZOS entryOffset = fs.tellp();#else entryOffset = getOffset(fs.tellp());#endif } fs.clear(); PEG_METHOD_EXIT(); return false;}Boolean InstanceIndexFile::compact( const String& path){ PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::compact()"); // // Open input file: // fstream fs; if (!_openFile(path, fs)) { PEG_METHOD_EXIT(); return false; } // // Open temporary file (delete it first): // fstream tmpFs; String tmpPath = path; tmpPath.append(".tmp"); FileSystem::removeFileNoCase(tmpPath); if (!_openFile(tmpPath, tmpFs, true)) { PEG_METHOD_EXIT(); return false; } // // Iterate over all instances to build output arrays: // Buffer line; Uint32 freeFlag; Uint32 hashCode; const char* instanceName; Uint32 index; Uint32 size; Boolean error; Uint32 adjust = 0; while (_GetNextRecord( fs, line, freeFlag, hashCode, index, size, instanceName, error)) { // // Copy the entry over to the temporary file if it is not free. // Otherwise, discard the entry and update subsequent indices to // compensate for removal of this block. // if (freeFlag) { adjust += size; } else { if (!_appendEntry(tmpFs, CIMObjectPath (instanceName), index - adjust, size)) { error = true; break; } } } // // Close both files: fs.close(); tmpFs.close(); // // If an error occurred, remove the temporary file and // return false. // if (error) { FileSystem::removeFileNoCase(tmpPath); PEG_METHOD_EXIT(); return false; } // // Replace index file with temporary file: // if (!FileSystem::removeFileNoCase(path)) { PEG_METHOD_EXIT(); return false; } if (!FileSystem::renameFile(tmpPath, path)) { PEG_METHOD_EXIT(); return false; } PEG_METHOD_EXIT(); return true;}Boolean InstanceIndexFile::hasNonFreeEntries(const String& path){ // // If file does not exist, there are no instances: // if (!FileSystem::existsNoCase(path)) return false; // // We must iterate all the entries looking for a non-free one: // Array<Uint32> freeFlags; Array<Uint32> indices; Array<Uint32> sizes; Array<CIMObjectPath> instanceNames; if (!InstanceIndexFile::enumerateEntries( path, freeFlags, indices, sizes, instanceNames, false)) { // This won't happen! return false; } return freeFlags.size() != 0;}Boolean InstanceIndexFile::beginTransaction(const String& path){ PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::beginTransaction()"); String rollbackPath = path; rollbackPath.append(".rollback"); // // If the index file does not exist, then create a rollback file with // freecount of 0. // if (!FileSystem::existsNoCase(path)) { // Make sure the rollback file does not exist. if (FileSystem::existsNoCase(rollbackPath)) { if (!FileSystem::removeFileNoCase(rollbackPath)) { PEG_METHOD_EXIT(); return false; } } // Create the rollback file, and write the freecount of 0. fstream fs; if (!_openFile(rollbackPath, fs, true)) { // Make sure no rollback file is left over. FileSystem::removeFileNoCase(rollbackPath); PEG_METHOD_EXIT(); return false; } fs.close(); PEG_METHOD_EXIT(); return true; } // // Create a rollback file which is a copy of the index file. The // new filename is formed by appending ".rollback" to the name of // the index file. The rollback file, if it exists, is considered // the "master" copy of the data. To ensure its completeness, the // index file is renamed to the rollback file and the data is then // copied back to the index file. // if (!FileSystem::renameFileNoCase(path, rollbackPath)) { PEG_METHOD_EXIT(); return false; } if (!FileSystem::copyFile(rollbackPath, path)) { // Try to restore the initial state FileSystem::removeFileNoCase(path); FileSystem::renameFileNoCase(rollbackPath, path); PEG_METHOD_EXIT(); return false; } PEG_METHOD_EXIT(); return true;}Boolean InstanceIndexFile::rollbackTransaction(const String& path){ PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::rollbackTransaction()"); // // If the rollback file does not exist, then everything is fine (nothing // to roll back). // if (!FileSystem::existsNoCase(path + ".rollback")) { PEG_METHOD_EXIT(); return true; } // // To roll back, simply delete the index file and rename // the rollback file over it. // if (FileSystem::existsNoCase(path)) { if (!FileSystem::removeFileNoCase(path)) { PEG_METHOD_EXIT(); return false; } } PEG_METHOD_EXIT(); return FileSystem::renameFileNoCase(path + ".rollback", path);}Boolean InstanceIndexFile::commitTransaction(const String& path){ PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::commitTransaction()"); // // To commit, simply remove the rollback file: // String rollbackPath = path; rollbackPath.append(".rollback"); PEG_METHOD_EXIT(); return FileSystem::removeFileNoCase(rollbackPath);}PEGASUS_NAMESPACE_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -