Ejemplo n.º 1
0
STDMETHODIMP GuestSessionWrap::FileRename(IN_BSTR aSource,
                                          IN_BSTR aDest,
                                          ComSafeArrayIn(PathRenameFlag_T, aFlags))
{
    LogRelFlow(("{%p} %s:enter aSource=%ls aDest=%ls aFlags=%zu\n", this, "GuestSession::fileRename", aSource, aDest, aFlags));

    VirtualBoxBase::clearError();

    HRESULT hrc;

    try
    {

        AutoCaller autoCaller(this);
        if (FAILED(autoCaller.rc()))
            throw autoCaller.rc();

        hrc = fileRename(BSTRInConverter(aSource).str(),
                         BSTRInConverter(aDest).str(),
                         ArrayInConverter<PathRenameFlag_T>(ComSafeArrayInArg(aFlags)).array());
    }
    catch (HRESULT hrc2)
    {
        hrc = hrc2;
    }
    catch (...)
    {
        hrc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
    }

    LogRelFlow(("{%p} %s: leave hrc=%Rhrc\n", this, "GuestSession::fileRename", hrc));
    return hrc;
}
Ejemplo n.º 2
0
void renameImgAndMeta(const char *src, const char *dst)
{
    char * src_meta_file = appendExt(src, ".meta");
    char * src_img_file = appendExt(src, ".img");

    char * dst_meta_file = appendExt(dst, ".meta");
    char * dst_img_file = appendExt(dst, ".img");

    fileRename(src_meta_file, dst_meta_file);
    fileRename(src_img_file, dst_img_file);

    free(src_meta_file);
    free(src_img_file);

    free(dst_meta_file);
    free(dst_img_file);
}
Ejemplo n.º 3
0
// =-=-=-=-=-=-=-
// local function which makes the call to rename via the resource plugin
int _rsFileRename(
    rsComm_t*         _comm,
    fileRenameInp_t*  _rename_inp,
    fileRenameOut_t** _rename_out,
    rodsServerHost_t* _server_host ) {
    // =-=-=-=-=-=-=-
    // FIXME: need to check resource permission and vault permission
    // when RCAT is available
    // mkDirForFilePath( _comm, "/", _rename_inp->newFileName, getDefDirMode () ); - The actual file path depends on the resource

    // =-=-=-=-=-=-=-
    // make the call to rename via the resource plugin
    irods::file_object_ptr file_obj(
        new irods::file_object(
            _comm,
            _rename_inp->objPath,
            _rename_inp->oldFileName,
            _rename_inp->rescHier,
            0, 0, 0 ) );
    irods::error rename_err = fileRename( _comm, file_obj, _rename_inp->newFileName );

    // =-=-=-=-=-=-=-
    // report errors if any
    if ( !rename_err.ok() ) {
        std::stringstream msg;
        msg << "fileRename failed for [";
        msg << _rename_inp->oldFileName;
        msg << "] to [";
        msg << _rename_inp->newFileName;
        msg << "]";
        irods::error err = PASSMSG( msg.str(), rename_err );
        irods::log( err );
    }
    // =-=-=-=-=-=-=-
    // compare fco phy path and old file name
    // if they differ then repave for next call
    if ( file_obj->physical_path() == _rename_inp->oldFileName ) {
        file_obj->physical_path( _rename_inp->newFileName );
    }

    // =-=-=-=-=-=-=-
    // percolate possible change in phy path up
    ( *_rename_out ) = ( fileRenameOut_t* ) malloc( sizeof( fileRenameOut_t ) );
    strncpy(
        ( *_rename_out )->file_name,
        file_obj->physical_path().c_str(),
        MAX_NAME_LEN );

    return rename_err.code();

} // _rsFileRename
Ejemplo n.º 4
0
Archivo: Rimp.cpp Proyecto: benn-cs/aim
void Rimp::deleteVirtualImageFromDatastore(std::string& datastore, const std::string& virtualMachineUUID)
{
    string error("");
    RimpException rexecption;

    // check datastore path end with '/'
    if (datastore.at(datastore.size() - 1) != '/')
    {
        datastore = datastore.append("/");
    }
    
    checkRimpConfiguration();
    if (!checkDatastore(datastore))
    {
        error = error.append("Provided ''datastore'' :").append(datastore).append(" can not be used");
        LOG("[ERROR] [RIMP] %s", error.c_str());
        rexecption.description = error;
        throw rexecption;
    }

    LOG("[DEBUG] [RIMP] Deleting virtual machine [%s]", virtualMachineUUID.c_str());

    string viDatastorePath(datastore);
    viDatastorePath = viDatastorePath.append(virtualMachineUUID);

    // check the file exist on the datastore
    if (access(viDatastorePath.c_str(), F_OK) == -1)
    {
        error = error.append("Virtual image file does not exist on the ''datastore'' :");
        error = error.append(viDatastorePath);

        LOG("[WARNING] [RIMP] %s", error.c_str());
        return;
    }

    if (autobackup)
    {
        // Then backup the file rather than deleting it.
        string viDatastorePathBackup(datastore);
        viDatastorePathBackup = viDatastorePathBackup.append("backup/");
        viDatastorePathBackup = viDatastorePathBackup.append(virtualMachineUUID);

        if (access(viDatastorePathBackup.c_str(), F_OK) == 0)
        {
            // Then a backup already exists, delete it.
            remove(viDatastorePathBackup.c_str());
        }

        // Then perform a recovery rather then a full deploy.
        string renameError2 = fileRename( viDatastorePath, viDatastorePathBackup);

        if (!renameError2.empty())
        {
            error = error.append("Can not move :").append(viDatastorePath).append(" to :").append(viDatastorePathBackup).append("\nCaused by :").append(renameError2);

            LOG("[ERROR] [RIMP] %s", error.c_str());
            rexecption.description = error;
            throw rexecption;
        }

        LOG("[INFO] [RIMP] Backedup virtual machine [%s]", virtualMachineUUID.c_str());
        return;
    }

    remove(viDatastorePath.c_str());

    LOG("[INFO] [RIMP] Deleted virtual machine [%s]", virtualMachineUUID.c_str());
}
Ejemplo n.º 5
0
Archivo: Rimp.cpp Proyecto: benn-cs/aim
void Rimp::copyFromRepositoryToDatastore(const std::string& virtualImageRepositoryPath, std::string& datastore,
        const std::string& virtualMachineUUID)
{
    string error("");
    RimpException rexecption;

    // check datastore path end with '/'
    if (datastore.at(datastore.size() - 1) != '/')
    {
        datastore = datastore.append("/");
    }
    
    checkRimpConfiguration();
    if (!checkDatastore(datastore))
    {
        error = error.append("Provided ''datastore'' :").append(datastore).append(" can not be used");
        LOG("[ERROR] [RIMP] %s", error.c_str());
        rexecption.description = error;
        throw rexecption;
    }

    LOG("[DEBUG] [RIMP] Instantiating virtual image [%s] for virtual machine [%s]",
            virtualImageRepositoryPath.c_str(), virtualMachineUUID.c_str());

    string viRepositoryPath(repository);
    viRepositoryPath = viRepositoryPath.append(virtualImageRepositoryPath);

    // Check the source file (on the repository) exist and can be read
    if (access(viRepositoryPath.c_str(), F_OK | R_OK) == -1)
    {
        error = error.append("Source file does not exist at [").append(viRepositoryPath).append("]");

        LOG("[ERROR] [RIMP] %s", error.c_str());
        rexecption.description = error;
        throw rexecption;
    }

    unsigned long int viSize = getFileSize(viRepositoryPath);

    /** Copy from ''Local Repository'' to ''Datastore''. */
    string viDatastorePath(datastore);
    viDatastorePath = viDatastorePath.append(virtualMachineUUID);

    // if the file exist on the datastore delete it
    if (access(viDatastorePath.c_str(), F_OK) == 0)
    {
        if (autorestore && autobackup)
        {
            // Sometimes an undeploy fails and the origional image does not get re/moved so we don't want to delete it and we don't need to try to recover so there is nothing to do.
            // NOTE: You really do want this, don't over think it.

            LOG("[INFO] [RIMP] Not checking for backup; existing virtual image instance found for virtual machine [%s]", virtualMachineUUID.c_str());
            return;
        }

        LOG("[WARNING] [RIMP] File with the same UUID already present on the ''datastore'' [%s], removing it.",
                viDatastorePath.c_str());

        remove(viDatastorePath.c_str());
    }

    if (autorestore)
    {
        string viDatastorePathBackup(datastore);
        viDatastorePathBackup = viDatastorePathBackup.append("backup/");
        viDatastorePathBackup = viDatastorePathBackup.append(virtualMachineUUID);
        if (access(viDatastorePathBackup.c_str(), F_OK | R_OK) == 0)
        {
            // Then perform a recovery rather then a full deploy.
            string renameError2 = fileRename(viDatastorePathBackup, viDatastorePath);

            if (!renameError2.empty())
            {
                error = error.append("Can not move :").append(viDatastorePathBackup).append(" to :").append(viDatastorePath).append("\nCaused by :").append(renameError2);

                LOG("[ERROR] [RIMP] %s", error.c_str());
                rexecption.description = error;
                throw rexecption;
            }

            LOG("[INFO] [RIMP] Recovered virtual image instance for virtual machine [%s]", virtualMachineUUID.c_str());
            return;
        }
    }

    // XXX viSize is the same on the repository and on the local repository
    unsigned long int datastoreFreeSize = getFreeSpaceOn(datastore);

    if (datastoreFreeSize < viSize)
    {
        error = error.append("There is no enough space left to copy the file :");
        error = error.append(viRepositoryPath).append(" to :").append(datastore);

        LOG("[ERROR] [RIMP] %s", error.c_str());
        rexecption.description = error;
        throw rexecption;
    }

    string copyError2 = fileCopy(viRepositoryPath, viDatastorePath);

    if (!copyError2.empty())
    {
        error = error.append("Can not copy to :").append(viDatastorePath).append("\nCaused by :").append(copyError2);

        LOG("[ERROR] [RIMP] %s", error.c_str());
        rexecption.description = error;
        throw rexecption;
    }

    LOG("[INFO] [RIMP] Created virtual image instance for virtual machine [%s]", virtualMachineUUID.c_str());
}
int MTPDevice::rename(const std::string &oldpath, const std::string &newpath)
{
#ifndef SMTPFS_MOVE_BY_SET_OBJECT_PROPERTY
    const std::string tmp_old_basename(smtpfs_basename(oldpath));
    const std::string tmp_old_dirname(smtpfs_dirname(oldpath));
    const std::string tmp_new_dirname(smtpfs_dirname(newpath));
    if (tmp_old_dirname != tmp_new_dirname)
        return -EINVAL;

    const TypeDir *dir_parent = dirFetchContent(tmp_old_dirname);
    if (!dir_parent || dir_parent->id() == 0)
        return -EINVAL;
    const TypeDir *dir_to_rename = dir_parent->dir(tmp_old_basename);
    if (dir_to_rename)
        return dirRename(oldpath, newpath);
    else
        return fileRename(oldpath, newpath);
#else
    const std::string tmp_old_basename(smtpfs_basename(oldpath));
    const std::string tmp_old_dirname(smtpfs_dirname(oldpath));
    const std::string tmp_new_basename(smtpfs_basename(newpath));
    const std::string tmp_new_dirname(smtpfs_dirname(newpath));
    const TypeDir *dir_old_parent = dirFetchContent(tmp_old_dirname);
    const TypeDir *dir_new_parent = dirFetchContent(tmp_new_dirname);
    const TypeDir *dir_to_rename = dir_old_parent ? dir_old_parent->dir(tmp_old_basename) : nullptr;
    const TypeFile *file_to_rename = dir_old_parent ? dir_old_parent->file(tmp_old_basename) : nullptr;

    logdebug("dir_to_rename:    ", dir_to_rename, "\n");
    logdebug("file_to_rename:   ", file_to_rename, "\n");

    if (!dir_old_parent || !dir_new_parent || dir_old_parent->id() == 0)
        return -EINVAL;

    const TypeBasic *object_to_rename =  dir_to_rename ?
        static_cast<const TypeBasic*>(dir_to_rename) :
        static_cast<const TypeBasic*>(file_to_rename);

    logdebug("object_to_rename: ", object_to_rename, "\n");
    logdebug("object_to_rename->id(): ", object_to_rename->id(), "\n");

    if (!object_to_rename) {
        logerr("No such file or directory to rename/move!\n");
        return -ENOENT;
    }

    if (tmp_old_dirname != tmp_new_dirname) {
        criticalEnter();
        int rval = LIBMTP_Set_Object_u32(m_device, object_to_rename->id(),
            LIBMTP_PROPERTY_ParentObject, dir_new_parent->id());
        criticalLeave();
        if (rval != 0) {
            logerr("Could not move '", oldpath, "' to '", newpath, "'.\n");
            LIBMTP_Dump_Errorstack(m_device);
            LIBMTP_Clear_Errorstack(m_device);
            return -EINVAL;
        }
        const_cast<TypeBasic*>(object_to_rename)->setParent(dir_new_parent->id());
    }
    if (tmp_old_basename != tmp_new_basename) {
        criticalEnter();
        int rval = LIBMTP_Set_Object_String(m_device, object_to_rename->id(),
            LIBMTP_PROPERTY_Name, tmp_new_basename.c_str());
        criticalLeave();
        if (rval != 0) {
            logerr("Could not rename '", oldpath, "' to '", newpath, "'.\n");
            LIBMTP_Dump_Errorstack(m_device);
            LIBMTP_Clear_Errorstack(m_device);
            return -EINVAL;
        }
    }
    return 0;
#endif
}