Ejemplo n.º 1
0
/**
   adds a mnemonic tag associated with an unresolved path (which
   gets resolved) to the managers known mnemonic
   @param tag the mnemonic reference
   @param an unresolved path name that may contain additional $ variables
   @return whether or not the path was added resolved to a directory and added
   to the manager's known mnemonic
*/
bool 
SysPathMgr::add(
            const String& tag, 
            const String& unresolved)
{
    bool res = false;

    String resolved = resolve(unresolved);
    
    // if it does not exist create
    FileAttributes attrs;
    if ( !FileUtils::getAttributes( resolved, attrs ) )
    {
        if (!FileUtils::mkdirs( resolved ))
        {
            CBLOGERR(_logger, NTEXT("SysPathMgr::add: could not create directory '") + resolved + NTEXT("'"));
            return false;
        }

        // update the file attributes with another call to get info in the directory
        FileUtils::getAttributes( resolved, attrs );
    }

    if (!attrs.isDirectory())
    {
        CBLOGERR(_logger, NTEXT("SysPathMgr::add: found a file where a directory '") + resolved + NTEXT("' was expected"));
        return false;
    }

    _paths[tag] = resolved;

    return res;
}
Ejemplo n.º 2
0
int FileUtils::rmdir(
    const String &dirName )
{
    int res = 0;
    
    String cleanName = FileUtils::properName( dirName );
    
    { // required because the directory would otherwise be in use
#if defined(_WINDOWS) || defined(WIN32)
        FileIterator it(new FileIterImpWin32);
#else
        FileIterator it(new FileIterImpUnix);
#endif
        String deletePattern = cleanName;
        deletePattern += FileUtils::PATH_SEP;
        deletePattern += FileUtils::ALL_FILES_PAT;

        String dataFileName;

        FileIterator::Status stat = it.findFirst( deletePattern, dataFileName );
        while ( (res == 0) && (stat == FileIterator::cFound) )
        {
            // construct the path to the target
            String targetName = cleanName;
            targetName += FileUtils::PATH_SEP;
            targetName += dataFileName;

            // check to see if the target is a directory
            FileAttributes attrs;
            if ( FileUtils::getAttributes( targetName, attrs ) && 
                 attrs.isDirectory() )
            {
                // if this is a directory, then call rmdir to delete
                res = FileUtils::rmdir( targetName );
            }
            else
            {
                // otherwise delete all the file from this directory
                res = FileUtils::fileDelete( targetName ) ? 0 : -1;
            }

            stat = it.findNext( dataFileName );
        }
    }

#if defined(_WINDOWS) || defined(WIN32)
    res = _trmdir( cleanName.c_str() );
#else
    // now delete the target directory
    res = ::rmdir( cleanName.c_str() );
#endif

    return res;
}
Ejemplo n.º 3
0
FileIterator::Status 
FileIterImpUnix::findFirst(
            const String &findStr,
            FileAttributes &attrs )
{    
    FileIterator::Status status = FileIterator::cFindError;
    
    closeFind();

    // split the filter from the base path
    StringUtils::splitBack(
        FileUtils::PATH_SEP, 
        findStr,
        &_basePath, &_filter);

    // Make sure that the user has not passed us a directory
    FileAttributes sourceAttrs;
    if ( fpattern_isvalid(_filter.c_str()) &&
         FileUtils::getAttributes(_basePath, sourceAttrs) && 
         sourceAttrs.isDirectory() )
    {
        // have the directory, so now start returning the 
        // and a valid starch path - make sure that the
        // path is fully resolved
        char resolvedPath[MAXPATHLEN+1];
        resolvedPath[0] = '\0';
        
        if ( ::realpath(_basePath.c_str(), resolvedPath) != NULL )
        {
            _fd = ::opendir( resolvedPath );
            if ( _fd != NULL )
            {
                status = findNext( attrs );
            }
        }
    }
    
    return status;
}
Ejemplo n.º 4
0
bool 
FileUtils::fileCopy( 
    const String &source, 
    const String& dest,
    const bool replace )
{
    ASSERT_D( source.length() > 0 );
    ASSERT_D( dest.length() > 0 );

#if defined(_WINDOWS) || defined(WIN32)
    return ::CopyFile( source.c_str(), dest.c_str(), replace ? FALSE : TRUE ) == TRUE;
#else

    // validate the source file 
    FileAttributes attrs;
    bool res = FileUtils::getAttributes( source, attrs );
    if ( !res || attrs.isDirectory() )
    {
        return false;
    }

    // if the destination file exists, replace is false, then we don't 
    // allow the procedure to complete
    FileAttributes destattrs;
    if ( FileUtils::getAttributes( dest, destattrs ) && 
         !destattrs.isDirectory() && 
         (!replace || destattrs.isReadonly()) )
    {
        return false;
    }

#ifdef __GNUG__
            IFSTREAM input( source.c_str(),
                                 std::ios::in |
                                 std::ios::binary );
            OFSTREAM output( dest.c_str(),
                                 std::ios::out |
                                 std::ios::binary );
#else
            IFSTREAM input( source.c_str(),
                                 std::ios_base::in |
                                 std::ios_base::binary );
            OFSTREAM output( dest.c_str(),
                                 std::ios::out |
                                 std::ios::binary );
#endif

    if ( input.good() && output.good() )
    {
        std::auto_ptr<char> buff( new char[COPY_BUFF_SIZE] );
        if ( buff.get() != NULL )
        {
            const char* pBuff = buff.get();
#ifdef __GNUG__
            input.seekg( 0, std::ios::end );
            int numBytesLeft = input.tellg() ;
            input.seekg( 0, std::ios::beg );
#else
            input.seekg( 0, std::ios_base::end );
            int numBytesLeft = input.tellg() ;
            input.seekg( 0, std::ios_base::beg );
#endif

            while ( numBytesLeft > 0 && res )
            {
                int numToRead = (numBytesLeft > COPY_BUFF_SIZE) ? COPY_BUFF_SIZE : numBytesLeft;
                res = ( input.read( (char*)pBuff, numToRead ) && 
                        output.write( pBuff, numToRead ) );
                numBytesLeft -= numToRead;
            }
        }
    
        if ( res )
        {
            res = FileUtils::setAttributes( dest, attrs );
        }
    }
    
    return res;
#endif

}
Ejemplo n.º 5
0
int 
SysPathMgr::init( 
            const DOMNode* config, 
            RefCountedPtr<SysContext>& ctx )
{
    int res = -1;

    REFCOUNTED_CAST(iSysComponent, StdLogger, ctx->getComponent( StdLogger::getRegistryName()), _logger);

	// I expect the config node to be an element node
    if ( ( config != NULL ) && ( config->getNodeType() == DOMNode::ELEMENT_NODE ) )
    {
        // set the root to be the current directory by default
        String root(NTEXT("."));

        Mapping rootAttrs;
        if ( DomUtils::getNodeValue( (const DOMElement*)config, NULL, &rootAttrs ) )
        {
            rootAttrs.getIfAvailable(SYS_PATH_ROOTATTR, root);
        }

        // resolve whatever we have in the root and then verify a file does not exist
        // in place of the intended directory
        _root = FileUtils::resolve( root );

        FileAttributes fattrs;
        if ( !FileUtils::getAttributes( _root, fattrs ) )
        {
            if (!FileUtils::mkdirs( _root ))
            {
                CBLOGERR(_logger, NTEXT("SysPathMgr::init: could not create directory '") + _root + NTEXT("'"));
                return -1;
            }

            // update the file attributes with another call to get info in the directory
            FileUtils::getAttributes( _root, fattrs );
        }

        if (!fattrs.isDirectory())
        {
            CBLOGERR(_logger, NTEXT("SysPathMgr::init: found a file where a directory '") + _root + NTEXT("' was expected"));
            return -1;
        }

        // put a special key with the root attribute in the map
    	_paths[ SYS_PATH_IND + SYS_PATH_ROOTATTR ] = _root;

        // ok to this point, no errors
        res = 0;

        // iterate through the list of child elements whose tag is SYS_PATH_ENTRY
        // and get the name attribute and value. If all is found properly, then
        // construct a counter whose value is retrieved from the config node
        DOMNodeList* children = DomUtils::getNodeList( (const DOMElement*)config, SYS_PATH_ENTRY );
        if ( children != NULL )
        {
            for ( XMLSize_t i = 0, sz = children->getLength() ;
                  i < sz; i++ )
            {
                DOMNode* child = children->item(i);
                if ( (child != NULL) && (child->getNodeType() == DOMNode::ELEMENT_NODE) )
                {
                    String value;
                    Mapping attrs;

                    bool found = DomUtils::getNodeValue( (const DOMElement*)child, &value, &attrs );
                    if ( found )
                    {
                        res = 0;

                        String tagName;
                        attrs.getIfAvailable(SYS_PATH_TAGATTR, tagName);

                        if ( tagName.length() > 0 )
                        {
                            add( tagName, value );
                        }
                    }
                }
            }
        }
    }

    return res;
}