예제 #1
0
// =-=-=-=-=-=-=-
// local function to handle call to mkdir via resource plugin
int _rsFileMkdir(
    rsComm_t*       _comm,
    fileMkdirInp_t* _mkdir_inp ) {
    // =-=-=-=-=-=-=-
    // make call to mkdir via resource plugin

    irods::collection_object_ptr coll_obj(
        new irods::collection_object(
            _mkdir_inp->dirName,
            _mkdir_inp->rescHier,
            _mkdir_inp->mode,
            0 ) );

    // =-=-=-=-=-=-=-
    // pass condInput
    coll_obj->cond_input( _mkdir_inp->condInput );

    irods::error mkdir_err = fileMkdir( _comm, coll_obj );

    // =-=-=-=-=-=-=-
    // log error if necessary
    if ( !mkdir_err.ok() ) {
        if ( getErrno( mkdir_err.code() ) != EEXIST ) {
            std::stringstream msg;
            msg << "fileMkdir failed for ";
            msg << _mkdir_inp->dirName;
            msg << "]";
            irods::error ret_err = PASSMSG( msg.str(), mkdir_err );
            irods::log( ret_err );
        }
    }

    return ( mkdir_err.code() );

} // _rsFileMkdir
예제 #2
0
// =-=-=-=-=-=-=-
// local function to call readdir via resource plugin
int _rsFileReaddir(
    rsComm_t*         _comm,
    fileReaddirInp_t* _file_readdir_inp,
    rodsDirent_t**    _rods_dirent ) {
    // =-=-=-=-=-=-=-
    // create a collection_object, and extract dir ptr from the file desc table
    irods::collection_object_ptr coll_obj(
        new irods::collection_object(
            FileDesc[ _file_readdir_inp->fileInx].fileName,
            FileDesc[ _file_readdir_inp->fileInx].rescHier,
            0, 0 ) );
    coll_obj->directory_pointer( reinterpret_cast< DIR* >( FileDesc[ _file_readdir_inp->fileInx].driverDep ) );

    // =-=-=-=-=-=-=-
    // make call to readdir via resource plugin and handle errors, if necessary
    irods::error readdir_err = fileReaddir( _comm,
                                            coll_obj,
                                            _rods_dirent );
    if ( !readdir_err.ok() ) {
        std::stringstream msg;
        msg << "fileReaddir failed for [";
        msg << FileDesc[ _file_readdir_inp->fileInx].fileName;
        msg << "]";
        irods::error err = PASSMSG( msg.str(), readdir_err );
        irods::log( err );

        return readdir_err.code();
    }
    else {
        // =-=-=-=-=-=-=-
        // case for handle end of file
        if ( -1 == readdir_err.code() ) {
            return readdir_err.code();
        }
    }

    // =-=-=-=-=-=-=-
    // error code holds 'status' of api call
    return readdir_err.code();

} // _rsFileReaddir
예제 #3
0
파일: fileOpr.cpp 프로젝트: 0x414A/irods
// =-=-=-=-=-=-=-
//
int chkEmptyDir(
    rsComm_t*           rsComm,
    const std::string&  cacheDir,
    const std::string&  hier ) {
    int status = 0;
    char childPath[MAX_NAME_LEN];
    struct stat myFileStat;
    struct rodsDirent* myFileDirent = 0;

    // =-=-=-=-=-=-=-
    // call opendir via resource plugin
    irods::collection_object_ptr cacheDir_obj(
        new irods::collection_object(
            cacheDir,
            hier,
            0, 0 ) );
    irods::error opendir_err = fileOpendir( rsComm, cacheDir_obj );

    // =-=-=-=-=-=-=-
    // don't log an error as this is part
    // of the functionality of this code
    if ( !opendir_err.ok() ) {
        return 0;
    }

    // =-=-=-=-=-=-=-
    // make call to readdir via resource plugin
    irods::error readdir_err = fileReaddir( rsComm, cacheDir_obj, &myFileDirent );
    while ( readdir_err.ok() && 0 == readdir_err.code() ) {
        // =-=-=-=-=-=-=-
        // handle relative paths
        if ( strcmp( myFileDirent->d_name, "." ) == 0 ||
                strcmp( myFileDirent->d_name, ".." ) == 0 ) {
            readdir_err = fileReaddir( rsComm, cacheDir_obj, &myFileDirent );
            continue;
        }

        // =-=-=-=-=-=-=-
        // get status of path
        snprintf( childPath, MAX_NAME_LEN, "%s/%s", cacheDir.c_str(), myFileDirent->d_name );
        irods::collection_object_ptr tmp_coll_obj(
            new irods::collection_object(
                childPath,
                hier,
                0, 0 ) );

        irods::error stat_err = fileStat( rsComm, tmp_coll_obj, &myFileStat );

        // =-=-=-=-=-=-=-
        // handle hard error
        if ( stat_err.code() < 0 ) {
            rodsLog( LOG_ERROR, "chkEmptyDir: fileStat error for %s, status = %d",
                     childPath, stat_err.code() );
            break;
        }

        // =-=-=-=-=-=-=-
        // path exists
        if ( myFileStat.st_mode & S_IFREG ) {
            status = SYS_DIR_IN_VAULT_NOT_EMPTY;
            rodsLog( LOG_ERROR, "chkEmptyDir: file %s exists",
                     childPath, status );
            break;
        }

        // =-=-=-=-=-=-=-
        // recurse for another directory
        if ( myFileStat.st_mode & S_IFDIR ) {
            status = chkEmptyDir( rsComm, childPath, hier );
            if ( status == SYS_DIR_IN_VAULT_NOT_EMPTY ) {
                rodsLog( LOG_ERROR, "chkEmptyDir: dir %s is not empty", childPath );
                break;
            }
        }

        // =-=-=-=-=-=-=-
        // continue with child path
        readdir_err = fileReaddir( rsComm, cacheDir_obj, &myFileDirent );

    } // while

    // =-=-=-=-=-=-=-
    // make call to closedir via resource plugin, log error if necessary
    irods::error closedir_err = fileClosedir( rsComm, cacheDir_obj );
    if ( !closedir_err.ok() ) {
        std::stringstream msg;
        msg << "fileClosedir failed for [";
        msg << cacheDir;
        msg << "]";
        irods::error log_err = PASSMSG( msg.str(), closedir_err );
        irods::log( log_err );
    }

    if ( status != SYS_DIR_IN_VAULT_NOT_EMPTY ) {
        irods::collection_object_ptr coll_obj(
            new irods::collection_object(
                cacheDir,
                hier, 0, 0 ) );
        irods::error rmdir_err = fileRmdir( rsComm, coll_obj );
        if ( !rmdir_err.ok() ) {
            std::stringstream msg;
            msg << "fileRmdir failed for [";
            msg << cacheDir;
            msg << "]";
            irods::error err = PASSMSG( msg.str(), rmdir_err );
            irods::log( err );
        }
        status = 0;
    }

    return status;

} // chkEmptyDir
예제 #4
0
    /// =-=-=-=-=-=-=-
    /// @brief This routine is for testing the TEST_STAGE_FILE_TYPE.
    ///        Just copy the file from cacheFilename to filename. optionalInfo info
    ///        is not used.
    irods::error univ_mss_file_sync_to_arch(
        irods::resource_plugin_context& _ctx,
        const char*                         _cache_file_name ) {
        // =-=-=-=-=-=-=-
        // check context
        irods::error err = univ_mss_check_param< irods::file_object >( _ctx );
        if ( !err.ok() ) {
            std::stringstream msg;
            msg << __FUNCTION__;
            msg << " - invalid context";
            return PASSMSG( msg.str(), err );

        }

        // =-=-=-=-=-=-=-
        // snag a ref to the fco
        irods::file_object_ptr fco = boost::dynamic_pointer_cast< irods::file_object >( _ctx.fco() );
        std::string filename = fco->physical_path();

        // =-=-=-=-=-=-=-
        // first create the directory name
        char  dirname[MAX_NAME_LEN] = "";
        const char* lastpart = strrchr( filename.c_str(), '/' );
        int   lenDir   = strlen( filename.c_str() ) - strlen( lastpart );
        strncpy( dirname, filename.c_str(), lenDir );

        // =-=-=-=-=-=-=-
        // create a context to call the mkdir operation
        irods::collection_object_ptr coll_obj(
            new irods::collection_object(
                dirname,
                fco->resc_hier(),
                fco->mode(), 0 ) );
        irods::resource_plugin_context context(
            _ctx.prop_map(),
            coll_obj, "",
            _ctx.comm(),
            _ctx.child_map() );

        // =-=-=-=-=-=-=-
        // create the directory on the MSS
        int status = 0;
        err = univ_mss_file_mkdir( context );

        execCmdOut_t* execCmdOut = NULL;
        char  cmdArgv[HUGE_NAME_LEN] = "";

        execCmd_t execCmdInp;
        bzero( &execCmdInp, sizeof( execCmdInp ) );

        // =-=-=-=-=-=-=-
        // get the script property
        std::string script;
        err = _ctx.prop_map().get< std::string >( SCRIPT_PROP, script );
        if ( !err.ok() ) {
            return PASSMSG( __FUNCTION__, err );
        }

        rstrcpy( execCmdInp.cmd, script.c_str(), LONG_NAME_LEN );
        strcat( cmdArgv, "syncToArch" );
        strcat( cmdArgv, " " );
        strcat( cmdArgv, _cache_file_name );
        strcat( cmdArgv, " " );
        strcat( cmdArgv, filename.c_str() );
        strcat( cmdArgv, "" );

        rstrcpy( execCmdInp.cmdArgv, cmdArgv, HUGE_NAME_LEN );
        rstrcpy( execCmdInp.execAddr, "localhost", LONG_NAME_LEN );
        status = _rsExecCmd( _ctx.comm(), &execCmdInp, &execCmdOut );
        if ( status == 0 ) {
            err = univ_mss_file_chmod( _ctx );
            if ( !err.ok() ) {
                PASSMSG( "univ_mss_file_sync_to_arch - failed.", err );
            }
        }
        else {
            status = UNIV_MSS_SYNCTOARCH_ERR - errno;
            std::stringstream msg;
            msg << "univ_mss_file_sync_to_arch: copy of [";
            msg << _cache_file_name;
            msg << "] to [";
            msg << filename;
            msg << "] failed.";
            msg << "   stdout buff [";
            msg << execCmdOut->stdoutBuf.buf;
            msg << "]   stderr buff [";
            msg << execCmdOut->stderrBuf.buf;
            msg << "]  status [";
            msg << execCmdOut->status << "]";
            return ERROR( status, msg.str() );
        }

        return CODE( status );

    } // univ_mss_file_sync_to_arch
예제 #5
0
// =-=-=-=-=-=-=-=
// _rsFileSyncToArch - this the local version of rsFileSyncToArch.
int _rsFileSyncToArch(
    rsComm_t*           _comm,
    fileStageSyncInp_t* _sync_inp,
    fileSyncOut_t**     _sync_out ) {
    // =-=-=-=-=-=-=-
    // XXXX need to check resource permission and vault permission
    // when RCAT is available

    // =-=-=-=-=-=-=-
    // prep
    if ( _sync_inp->objPath[0] == '\0' ) {
        std::stringstream msg;
        msg << __FUNCTION__;
        msg << " - Empty logical path.";
        irods::log( LOG_ERROR, msg.str() );
        return SYS_INVALID_INPUT_PARAM;
    }

    // =-=-=-=-=-=-=-
    // make call to synctoarch via resource plugin
    irods::file_object_ptr file_obj(
        new irods::file_object(
            _comm,
            _sync_inp->objPath,
            _sync_inp->filename, "", 0,
            _sync_inp->mode,
            _sync_inp->flags ) );
    file_obj->resc_hier( _sync_inp->rescHier );

    // =-=-=-=-=-=-=-
    // pass condInput
    file_obj->cond_input( _sync_inp->condInput );

    irods::error sync_err = fileSyncToArch( _comm, file_obj, _sync_inp->cacheFilename );

    if ( !sync_err.ok() ) {

        if ( getErrno( sync_err.code() ) == ENOENT ) {
            // =-=-=-=-=-=-=-
            // the directory does not exist, lets make one
            int status = mkDirForFilePath(
                             _comm,
                             0,
                             _sync_inp->filename,
                             _sync_inp->rescHier,
                             getDefDirMode() );
            if ( status < 0 ) {
                rodsLog( LOG_ERROR, "mkDirForFilePath failed in _rsFileSyncToArch with status %d", status );
                return status;
            }
        }
        else if ( getErrno( sync_err.code() ) == EEXIST ) {
            // =-=-=-=-=-=-=-
            // an empty dir may be there, make the call to rmdir via the resource plugin
            irods::collection_object_ptr coll_obj(
                new irods::collection_object(
                    _sync_inp->filename,
                    _sync_inp->rescHier,
                    0, 0 ) );
            coll_obj->cond_input( _sync_inp->condInput );
            irods::error rmdir_err = fileRmdir( _comm, coll_obj );
            if ( !rmdir_err.ok() ) {
                std::stringstream msg;
                msg << "fileRmdir failed for [";
                msg << _sync_inp->filename;
                msg << "]";
                irods::error err = PASSMSG( msg.str(), sync_err );
                irods::log( err );
            }
        }
        else {
            std::stringstream msg;
            msg << "fileSyncToArch failed for [";
            msg << _sync_inp->filename;
            msg << "]";
            irods::error err = PASSMSG( msg.str(), sync_err );
            irods::log( err );
            return sync_err.code();
        }

        // =-=-=-=-=-=-=-
        // make call to synctoarch via resource plugin
        sync_err = fileSyncToArch( _comm, file_obj, _sync_inp->cacheFilename );
        if ( !sync_err.ok() ) {
            std::stringstream msg;
            msg << "fileSyncToArch failed for [";
            msg << _sync_inp->filename;
            msg << "]";
            msg << sync_err.code();
            irods::error err = PASSMSG( msg.str(), sync_err );
            irods::log( err );
        }

    } // if !sync_err.ok()

    // =-=-=-=-=-=-=-
    // has the file name has changed?
    if ( *_sync_out ) {
        rstrcpy( ( *_sync_out )->file_name, file_obj->physical_path().c_str(), MAX_NAME_LEN );
    }

    return sync_err.code();

} // _rsFileSyncToArch
예제 #6
0
파일: libunivmss.cpp 프로젝트: 0x414A/irods
    /// =-=-=-=-=-=-=-
    /// @brief interface for POSIX rename
    irods::error univ_mss_file_rename(
        irods::resource_plugin_context& _ctx,
        const char*                      _new_file_name ) {
        // =-=-=-=-=-=-=-
        // check context
        irods::error err = univ_mss_check_param< irods::file_object >( _ctx );
        if ( !err.ok() ) {
            std::stringstream msg;
            msg << __FUNCTION__;
            msg << " - invalid context";
            return PASSMSG( msg.str(), err );

        }

        // =-=-=-=-=-=-=-
        // get the script property
        std::string script;
        err = _ctx.prop_map().get< std::string >( SCRIPT_PROP, script );
        if ( !err.ok() ) {
            return PASSMSG( __FUNCTION__, err );
        }

        // =-=-=-=-=-=-=-
        // snag a ref to the fco
        irods::file_object_ptr fco = boost::dynamic_pointer_cast< irods::file_object >( _ctx.fco() );
        std::string filename = fco->physical_path();

        // =-=-=-=-=-=-=-
        // first create the directory name
        char  dirname[MAX_NAME_LEN] = "";
        const char* lastpart = strrchr( _new_file_name, '/' );
        int   lenDir   = strlen( _new_file_name ) - strlen( lastpart );
        strncpy( dirname, _new_file_name, lenDir );

        // =-=-=-=-=-=-=-
        // create a context to call the mkdir operation
        irods::collection_object_ptr coll_obj(
            new irods::collection_object(
                dirname,
                fco->resc_hier(),
                fco->mode(), 0 ) );
        irods::resource_plugin_context context(
            _ctx.prop_map(),
            coll_obj, "",
            _ctx.comm(),
            _ctx.child_map() );

        // =-=-=-=-=-=-=-
        // create the directory on the MSS
        int status = 0;
        err = univ_mss_file_mkdir( context );

        execCmd_t execCmdInp;

        bzero( &execCmdInp, sizeof( execCmdInp ) );
        snprintf( execCmdInp.cmd, sizeof( execCmdInp.cmd ), "%s", script.c_str() );
        snprintf( execCmdInp.cmdArgv, sizeof( execCmdInp.cmdArgv ), "mv '%s' '%s'", filename.c_str(), _new_file_name );
        snprintf( execCmdInp.execAddr, sizeof( execCmdInp.execAddr ), "%s", "localhost" );
        execCmdOut_t *execCmdOut = NULL;
        status = _rsExecCmd( &execCmdInp, &execCmdOut );
        freeCmdExecOut( execCmdOut );

        if ( status < 0 ) {
            status = UNIV_MSS_RENAME_ERR - errno;
            std::stringstream msg;
            msg << "univ_mss_file_rename - failed for [";
            msg << filename;
            msg << "]";
            return ERROR( status, msg.str() );

        }

        return CODE( status );

    } // univ_mss_file_rename
예제 #7
0
// =-=-=-=-=-=-=-
// local function which handles removing directories via the resource plugin
int _rsFileRmdir(
    rsComm_t*       _comm,
    fileRmdirInp_t* _rmdir_inp ) {

    irods::collection_object_ptr coll_obj(
        new irods::collection_object(
            _rmdir_inp->dirName,
            _rmdir_inp->rescHier,
            0, 0 ) );

    if ( ( _rmdir_inp->flags & RMDIR_RECUR ) != 0 ) {
        // FIXME :: revisit this after moving to First class Objects
        // recursive. This is a very dangerous operation. curently
        // it is only used to remove cache directory of structured
        // files
        struct rodsDirent* myFileDirent = 0;

        // if it is not a cache dir, return an error as we only do this
        // for cache dirs at the moment.
        if ( strstr( _rmdir_inp->dirName, CACHE_DIR_STR ) == NULL ) {
            rodsLog( LOG_ERROR, "_rsFileRmdir: recursive rm of non cachedir path %s",
                     _rmdir_inp->dirName );
            return SYS_INVALID_FILE_PATH;
        }

        // call opendir via resource plugin
        irods::error opendir_err = fileOpendir( _comm, coll_obj );

        // log an error, if any
        if ( !opendir_err.ok() ) {
            std::stringstream msg;
            msg << "fileOpendir failed for [";
            msg << _rmdir_inp->dirName;
            msg << "]";
            irods::error err = PASSMSG( msg.str(), opendir_err );
            irods::log( err );
            return opendir_err.code();
        }

        // read the directory via resource plugin and either handle files or recurse into another directory
        irods::error readdir_err = fileReaddir( _comm, coll_obj, &myFileDirent );
        while ( readdir_err.ok() && 0 == readdir_err.code() ) {
            struct stat statbuf;
            char myPath[MAX_NAME_LEN];

            // handle relative paths
            if ( strcmp( myFileDirent->d_name, "." ) == 0 ||
                    strcmp( myFileDirent->d_name, ".." ) == 0 ) {
                readdir_err = fileReaddir( _comm, coll_obj, &myFileDirent );
                continue;
            }

            // cache path name
            snprintf( myPath, MAX_NAME_LEN, "%s/%s", &_rmdir_inp->dirName[0], &myFileDirent->d_name[0] );

            // =-=-=-=-=-=-=-
            // call stat via resource plugin, handle errors
            irods::collection_object_ptr myPath_obj(
                new irods::collection_object(
                    myPath,
                    _rmdir_inp->rescHier,
                    0, 0 ) );
            irods::error stat_err = fileStat( _comm, myPath_obj, &statbuf );
            if ( !stat_err.ok() ) {
                std::stringstream msg;
                msg << "fileStat failed for [";
                msg << myPath;
                msg << "]";
                irods::error log_err = PASSMSG( msg.str(), stat_err );
                irods::log( log_err );

                // call closedir via resource plugin, handle errors
                irods::error closedir_err = fileClosedir( _comm, myPath_obj );
                if ( !closedir_err.ok() ) {
                    std::stringstream msg;
                    msg << "fileClosedir for [";
                    msg << myPath;
                    msg << "]";
                    irods::error log_err = PASSMSG( msg.str(), closedir_err );
                    irods::log( log_err );
                }

                return stat_err.code();

            }  // if !stat_err.ok()

            // filter based on stat results: file, dir, or error
            int status = 0;
            if ( ( statbuf.st_mode & S_IFREG ) != 0 ) {
                // handle case where file is found
                irods::error unlink_err = fileUnlink( _comm, myPath_obj );
                status = unlink_err.code();
                if ( !unlink_err.ok() ) {
                    irods::error log_err = PASSMSG( "error during unlink.", unlink_err );
                    irods::log( log_err );
                }
            }
            else if ( ( statbuf.st_mode & S_IFDIR ) != 0 ) {
                // handle case where directory is found
                fileRmdirInp_t myRmdirInp;
                memset( &myRmdirInp, 0, sizeof( myRmdirInp ) );

                myRmdirInp.flags    = _rmdir_inp->flags;
                rstrcpy( myRmdirInp.dirName, myPath, MAX_NAME_LEN );
                rstrcpy( myRmdirInp.rescHier, _rmdir_inp->rescHier, MAX_NAME_LEN );

                // RECURSE - call _rsFileRmdir on this new found directory
                status = _rsFileRmdir( _comm, &myRmdirInp );

            }
            else {
                status = SYS_INVALID_FILE_PATH;
                rodsLog( LOG_NOTICE, "_rsFileRmdir:  for %s, status = %d", myPath, status );
            }

            // handle error condition on the above three cases
            if ( status < 0 ) {
                rodsLog( LOG_NOTICE, "_rsFileRmdir:  rm of %s failed, status = %d", myPath, status );

                // call closedir via resource plugin, handle errors
                irods::error closedir_err = fileClosedir( _comm, myPath_obj );
                if ( !closedir_err.ok() ) {
                    std::stringstream msg;
                    msg << "fileClosedir failed for [";
                    msg << myPath;
                    msg << "]";
                    irods::error log_err = PASSMSG( msg.str(), closedir_err );
                    irods::log( log_err );
                }

                return status;

            } // if status < 0

            // =-=-=-=-=-=-=-
            // continue readdir via resource plugin
            readdir_err = fileReaddir( _comm, coll_obj, &myFileDirent );

        } // while

        // =-=-=-=-=-=-=-
        // call closedir via resource plugin, handle errors
        irods::error closedir_err = fileClosedir( _comm, coll_obj );
        if ( !closedir_err.ok() ) {
            std::stringstream msg;
            msg << "fileClosedir failed for [";
            msg << _rmdir_inp->dirName;
            msg << "]";
            irods::error log_err = PASSMSG( msg.str(), closedir_err );
            irods::log( log_err );
        }

    } // if RMDIR_RECUR ( recursive delete )

    // =-=-=-=-=-=-=-
    // make the call to rmdir via the resource plugin
    irods::error rmdir_err = fileRmdir( _comm, coll_obj );
    if ( !rmdir_err.ok() ) {
        std::stringstream msg;
        msg << "fileRmdir failed for [";
        msg << _rmdir_inp->dirName;
        msg << "]";
        irods::error err = PASSMSG( msg.str(), rmdir_err );
        irods::log( err );
    }

    return rmdir_err.code();

} // _rsFileRmdir