⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 instanceindexfile.cpp

📁 Pegasus is an open-source implementationof the DMTF CIM and WBEM standards. It is designed to be por
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================////%/////////////////////////////////////////////////////////////////////////////#include <Pegasus/Common/Config.h>#include <fstream>#include <cctype>#include <cstdio>#include <cstring>#include <cstdlib>#include <Pegasus/Common/FileSystem.h>#include <Pegasus/Common/Tracer.h>#include "InstanceIndexFile.h"#if defined(PEGASUS_OS_OS400)#include "OS400ConvertChar.h"#endifPEGASUS_USING_STD;PEGASUS_NAMESPACE_BEGINstatic const Uint32 _MAX_FREE_COUNT = 16;//////////////////////////////////////////////////////////////////////////////////// Local routines://////////////////////////////////////////////////////////////////////////////////#ifdef PEGASUS_OS_ZOSstatic Uint32 getOffset( streampos sp ){    Uint32 result = (streamoff)sp;    fpos_t posArray = sp.seekpos();    result += posArray.__fpos_elem[1];    return result;}#endif//// Gets one line from the given file.//static Boolean _GetLine(fstream& fs, Buffer& x){    const Uint32 buffersize = 1023 + 1;    Uint32 xcount = 0;    Uint32 gcount = 0;    x.clear();    x.reserveCapacity(buffersize);    // The general idea is, we will read the stream at buffersize each time.    // if we get exactly buffersize-1, that means we didn't hit a \n    // so we loop again to read more,    // until we get a buffer that's not completely full.    // That means we hit a \n so we exit the loop.    do    {        char input[buffersize];        // This will read up to buffersize-1 char,        // but stop as soon as it hit \n.        // This will NOT consume the \n at the end.        fs.get(input, buffersize, '\n');        gcount = fs.gcount();        x.append(input, gcount);        xcount += gcount;    } while (gcount == buffersize-1 && fs.rdstate() != istream::failbit);    // if we read 0 byte in the last call, that's because the read buffer is    // exactly multiple of the input line.    // So the 2nd last get() read everything up to the \n and    // the last get() read 0 char and set the failbit on the    // stream.  The clear() call will set the stream to ready state.    if (gcount == 0)    {        fs.clear();    }    if (!fs.eof())    {        // we need to consume the '\n', because get() doesn't        char c = 0;        fs.get(c);    }    // if xcount is non zero, then we have read something from the buffer.    return (xcount != 0);}inline void _SkipWhitespace(char*& p){    while (*p && isspace(*p))        p++;}//// Get an integer field from the character pointer and advance the// pointer past the field.//Boolean _GetIntField(    char*& ptr,    Boolean& error,    Uint32& value,    int base){    char* end = 0;    value = strtoul(ptr, &end, base);    error = false;    if (!end)    {        error = true;        return false;    }    _SkipWhitespace(end);    if (*end == '\0')    {        error = true;        return false;    }    ptr = end;    return true;}//// Gets the next record in the index file.//static Boolean _GetNextRecord(    fstream& fs,    Buffer& line,    Uint32& freeFlag,    Uint32& hashCode,    Uint32& index,    Uint32& size,    const char*& instanceName,    Boolean& error){    error = false;    //    // Get next line:    //    if (!_GetLine(fs, line))        return false;    //    // Get the free flag field:    //    char* end = (char*)line.getData();    if (!_GetIntField(end, error, freeFlag, 10))        return false;    if (freeFlag != 0 && freeFlag != 1)    {        error = true;        return false;    }    //    // Get the hash-code field:    //    if (!_GetIntField(end, error, hashCode, 16))        return false;    //    // Get index field:    //    if (!_GetIntField(end, error, index, 10))        return false;    //    // Get size field:    //    if (!_GetIntField(end, error, size, 10))        return false;    //    // Get instance name:    //    instanceName = end;    return true;}//////////////////////////////////////////////////////////////////////////////////// InstanceIndexFile://////////////////////////////////////////////////////////////////////////////////Boolean InstanceIndexFile::lookupEntry(    const String& path,    const CIMObjectPath& instanceName,    Uint32& indexOut,    Uint32& sizeOut){    PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::lookupEntry()");    fstream fs;    if (!_openFile(path, fs))    {        PEG_METHOD_EXIT();        return false;    }    Uint32 entryOffset = 0;    Boolean result = _lookupEntry(        fs, instanceName, indexOut, sizeOut, entryOffset);    fs.close();    PEG_METHOD_EXIT();    return result;}Boolean InstanceIndexFile::createEntry(    const String& path,    const CIMObjectPath& instanceName,    Uint32 indexIn,    Uint32 sizeIn){    PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::createEntry()");    //    // Open the file:    //    fstream fs;    if (!_openFile(path, fs, true))    {        PEG_METHOD_EXIT();        return false;    }    //    // Return false if entry already exists:    //    Uint32 tmpIndex;    Uint32 tmpSize;    Uint32 tmpEntryOffset;    if (InstanceIndexFile::_lookupEntry(        fs, instanceName, tmpIndex, tmpSize, tmpEntryOffset))    {        PEG_METHOD_EXIT();        return false;    }    //    // Append the new entry to the end of the file:    //    if (!_appendEntry(fs, instanceName, indexIn, sizeIn))    {        PEG_METHOD_EXIT();        return false;    }    //    // Close the file:    //    fs.close();    PEG_METHOD_EXIT();    return true;}Boolean InstanceIndexFile::deleteEntry(    const String& path,    const CIMObjectPath& instanceName,    Uint32& freeCount){    PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::deleteEntry()");    freeCount = 0;    //    // Open the file:    //    fstream fs;    if (!_openFile(path, fs))    {        PEG_METHOD_EXIT();        return false;    }    //    // Mark the entry as free:    //    if (!_markEntryFree(fs, instanceName))    {        PEG_METHOD_EXIT();        return false;    }    //    // Increment the free count:    //    freeCount = 0;    if (!_incrementFreeCount(fs, freeCount))    {        PEG_METHOD_EXIT();        return false;    }    //    // Close the file:    //    fs.close();    PEG_METHOD_EXIT();    return true;}Boolean InstanceIndexFile::modifyEntry(    const String& path,    const CIMObjectPath& instanceName,    Uint32 indexIn,    Uint32 sizeIn,    Uint32& freeCount){    PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::modifyEntry()");    //    // Open the file:    //    fstream fs;    if (!_openFile(path, fs))    {        PEG_METHOD_EXIT();        return false;    }    //    // Mark the entry as free:    //    if (!_markEntryFree(fs, instanceName))    {        PEG_METHOD_EXIT();        return false;    }    //    // Append new entry:    //    if (!_appendEntry(fs, instanceName, indexIn, sizeIn))    {        PEG_METHOD_EXIT();        return false;    }    //    // Increment the free count:    //    freeCount = 0;    if (!_incrementFreeCount(fs, freeCount))    {        PEG_METHOD_EXIT();        return false;    }    //    // Close the file:    //    fs.close();    PEG_METHOD_EXIT();    return true;}Boolean InstanceIndexFile::enumerateEntries(    const String& path,    Array<Uint32>& freeFlags,    Array<Uint32>& indices,    Array<Uint32>& sizes,    Array<CIMObjectPath>& instanceNames,    Boolean includeFreeEntries){    PEG_METHOD_ENTER(TRC_REPOSITORY, "InstanceIndexFile::enumerateEntries()");    //    // Reserve space for at least COUNT entries:    //    const Uint32 COUNT = 1024;    freeFlags.reserveCapacity(COUNT);    indices.reserveCapacity(COUNT);    sizes.reserveCapacity(COUNT);    instanceNames.reserveCapacity(COUNT);    //    // Open input file:    //    fstream fs;    if (!_openFile(path, fs))    {        // file does not exist, just return with no instanceNames        PEG_METHOD_EXIT();        return true;    }    //    // Iterate over all instances to build output arrays:    //    Buffer line;    Uint32 freeFlag;    Uint32 hashCode;    const char* instanceName;    Uint32 index;    Uint32 size;    Boolean error;    while (_GetNextRecord(        fs, line, freeFlag, hashCode, index, size, instanceName, error))    {        if (!freeFlag || includeFreeEntries)        {            freeFlags.append(freeFlag);            indices.append(index);            sizes.append(size);            instanceNames.append (CIMObjectPath (instanceName));        }    }    if (error)    {        PEG_METHOD_EXIT();        return false;    }    PEG_METHOD_EXIT();    return true;}Boolean InstanceIndexFile::_incrementFreeCount(    PEGASUS_STD(fstream)& fs,    Uint32& freeCount){    PEG_METHOD_ENTER(TRC_REPOSITORY,        "InstanceIndexFile::_incrementFreeCount()");    //    // Position file pointer to beginning of file (where free count is    // located) and read the current free count.    //

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -