Ejemplo n.º 1
0
void NodeSystem::rename(FUSE::Handle sourceHandle, const std::string& sourceName, FUSE::Handle destHandle, const std::string& destName) {
    auto& sourceNode = getNodeInfo(sourceHandle).node;
    auto& destNode = getNodeInfo(destHandle).node;

    const auto sourcePath = getPath(sourceHandle) + sourceName;
    const auto destPath = getPath(destHandle) + destName;

    if (sourcePath.hasChild(destPath)) {
        // Can't rename to a subdirectory of itself.
        throw FUSE::ErrorException(EINVAL);
    }

    if (sourcePath == destPath) {
        // Paths are the same; nothing to do.
        return;
    }

    Directory sourceDirectory(sourceNode);
    Directory destDirectory(destNode);

    if (!sourceDirectory.hasChild(sourceName)) {
        throw FUSE::ErrorException(ENOENT);
    }

    destDirectory.forceAddChild(destName, sourceDirectory.getChild(sourceName));
    sourceDirectory.removeChild(sourceName);

    // Re-map any affected metadata.
    removeAllMetadata(destPath);
    moveAllMetadata(sourcePath, destPath);

    // Re-map any affected handles.
    detachAllHandles(destPath);
    moveAllHandles(sourcePath, destPath);
}
Ejemplo n.º 2
0
bool DirectoryUtilities::copyDirectory(const QString &sourceDirectoryPath, const QString &destinationDirectoryPath)
{
    QDir sourceDirectory(sourceDirectoryPath), destinationDirectory(destinationDirectoryPath);
    QStringList files, directories;

    if (!sourceDirectory.exists())
    {
        ERROR_LOG(QString("El directori origen %1 no existeix").arg(sourceDirectoryPath));
        return false;
    }

    if (!destinationDirectory.exists())
    {
        if (!destinationDirectory.mkdir(destinationDirectoryPath))
        {
            ERROR_LOG("No s'ha pogut crear el directori " + destinationDirectoryPath);
            return false;
        }
    }

    // Copiem els fitxers del directori
    files = sourceDirectory.entryList(QDir::Files);
    for (int i = 0; i < files.count(); i++)
    {
        QString sourceFile = sourceDirectoryPath + QDir::separator() + files[i];
        QString destinationFile = destinationDirectoryPath + QDir::separator() + files[i];

        if (!QFile::copy(sourceFile, destinationFile))
        {
            ERROR_LOG(QString("No s'ha pogut copiar l'arxiu %1 al seu destí %2").arg(sourceFile, destinationFile));
            return false;
        }
    }

    // Copiem els subdirectoris
    directories = sourceDirectory.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
    for (int i = 0; i < directories.count(); i++)
    {
        QString sourceSubDirectory = sourceDirectoryPath + QDir::separator() + directories[i];
        QString destinationSubDirectory = destinationDirectoryPath + QDir::separator() + directories[i];
        if (!copyDirectory(sourceSubDirectory, destinationSubDirectory))
        {
            return false;
        }
    }

    return true;
}