Пример #1
0
static int MEGAreaddir(const char *p, void *buf, fuse_fill_dir_t filler,
                         off_t offset, struct fuse_file_info *fi)
{
	string path = megaBasePath + p;
	MegaApi::log(MegaApi::LOG_LEVEL_DEBUG, "Listing folder:");
	MegaApi::log(MegaApi::LOG_LEVEL_DEBUG, path.c_str());

	MegaNode *node = megaApi->getNodeByPath(path.c_str());
	if (!node)
	{
		MegaApi::log(MegaApi::LOG_LEVEL_DEBUG, "Folder not found");
		return -ENOENT;
	}
	
	filler(buf, ".", NULL, 0);
	filler(buf, "..", NULL, 0);
	MegaNodeList *children = megaApi->getChildren(node);
	for (int i=0; i<children->size(); i++)
	{
		MegaNode *n = children->get(i);
		filler(buf, n->getName(), NULL, 0);
		MegaApi::log(MegaApi::LOG_LEVEL_DEBUG, n->getName());
	}
	
	delete node;
	delete children;

	MegaApi::log(MegaApi::LOG_LEVEL_DEBUG, "Folder listed OK");	
	return 0;
}
Пример #2
0
void NodeSelector::onDeleteClicked()
{
    MegaNode *node = megaApi->getNodeByHandle(selectedFolder);
    int access = megaApi->getAccess(node);
    if(!node || access < MegaShare::ACCESS_FULL)
    {
        delete node;
        return;
    }

    if(QMessageBox::question(this,
                             QString::fromUtf8("MEGAsync"),
                             tr("Are you sure that you want to delete \"%1\"?")
                                .arg(QString::fromUtf8(node->getName())),
                             QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes)
    {
        ui->tMegaFolders->setEnabled(false);
        ui->bNewFolder->setEnabled(false);
        ui->bOk->setEnabled(false);
        const char *name = node->getName();
        if (access == MegaShare::ACCESS_FULL
                || !strcmp(name, "NO_KEY")
                || !strcmp(name, "CRYPTO_ERROR")
                || !strcmp(name, "BLANK"))
        {
            megaApi->remove(node, delegateListener);
        }
        else
        {
            MegaNode *rubbish = megaApi->getRubbishNode();
            megaApi->moveNode(node, rubbish, delegateListener);
            delete rubbish;
        }
    }
    delete node;
}
Пример #3
0
void LinkProcessor::importLinks(MegaNode *node)
{
    if (!node)
    {
        return;
    }

    MegaNodeList *children = megaApi->getChildren(node);
    importParentFolder = node->getHandle();

    for (int i = 0; i < linkList.size(); i++)
    {
        if (!linkNode[i])
        {
            MegaApi::log(MegaApi::LOG_LEVEL_ERROR, "Trying to import a NULL node");
        }

        if (linkNode[i] && linkSelected[i] && !linkError[i])
        {
            bool dupplicate = false;
            long long dupplicateHandle;
            const char* name = linkNode[i]->getName();
            long long size = linkNode[i]->getSize();

            for (int j = 0; j < children->size(); j++)
            {
                MegaNode *child = children->get(j);
                if (!strcmp(name, child->getName()) && (size == child->getSize()))
                {
                    dupplicate = true;
                    dupplicateHandle = child->getHandle();
                }
            }

            if (!dupplicate)
            {
                remainingNodes++;
                megaApi->copyNode(linkNode[i], node, delegateListener);
            }
            else
            {
                emit onDupplicateLink(linkList[i], QString::fromUtf8(name), dupplicateHandle);
            }
        }
    }
    delete children;
}
Пример #4
0
	virtual void onRequestFinish(MegaApi* api, MegaRequest *request, MegaError* e)
	{
		if(e->getErrorCode() != MegaError::API_OK)
		{
			finished = true;
			return;
		}

		switch(request->getType())
		{
			case MegaRequest::TYPE_LOGIN:
			{
				api->fetchNodes();
				break;
			}
			case MegaRequest::TYPE_FETCH_NODES:
			{
				cout << "***** Showing files/folders in the root folder:" << endl;
				MegaNode *root = api->getRootNode();
				MegaNodeList *list = api->getChildren(root);
			
				for(int i=0; i < list->size(); i++)
				{
					MegaNode *node = list->get(i);
					if(node->isFile())
						cout << "*****   File:   ";
					else
						cout << "*****   Folder: ";
				
					cout << node->getName() << endl;
				}
				cout << "***** Done" << endl;

				delete list;

				cout << "***** Uploading the image MEGA.png" << endl;
				api->startUpload("MEGA.png", root);
				delete root;

				break;
			}
			default:
				break;
		}
	}
Пример #5
0
void ImportMegaLinksDialog::onLinkInfoAvailable(int id)
{
    ImportListWidgetItem *item = (ImportListWidgetItem *)ui->linkList->itemWidget(ui->linkList->item(id));
    MegaNode *node = linkProcessor->getNode(id);

    int e = linkProcessor->getError(id);
    if (node && (e == MegaError::API_OK))
    {
        QString name = QString::fromUtf8(node->getName());
        if (!name.compare(QString::fromAscii("NO_KEY")) || !name.compare(QString::fromAscii("CRYPTO_ERROR")))
        {
            item->setData(tr("Decryption error"), ImportListWidgetItem::WARNING, node->getSize());
        }
        else
        {
            item->setData(name, ImportListWidgetItem::CORRECT, node->getSize());
        }
    }
    else
    {
        if ((e != MegaError::API_OK) && (e != MegaError::API_ETOOMANY))
        {
            ImportListWidgetItem::linkstatus status = ImportListWidgetItem::FAILED;
            if (e == MegaError::API_ETEMPUNAVAIL)
            {
                status = ImportListWidgetItem::WARNING;
            }
            item->setData(QCoreApplication::translate("MegaError", MegaError::getErrorString(e)), status);
        }
        else
        {
            item->setData(tr("Not found"), ImportListWidgetItem::FAILED);
        }
    }
    item->updateGui();
}
Пример #6
0
static int MEGArename(const char *f, const char *t)
{
	string from = megaBasePath + f;
	string to = megaBasePath + t;

	MegaApi::log(MegaApi::LOG_LEVEL_DEBUG, "Renaming/moving file/folder");
	MegaApi::log(MegaApi::LOG_LEVEL_DEBUG, from.c_str());
	MegaApi::log(MegaApi::LOG_LEVEL_DEBUG, to.c_str());

	MegaNode *source = megaApi->getNodeByPath(from.c_str());
	if (!source)
	{
		MegaApi::log(MegaApi::LOG_LEVEL_WARNING, "Source not found");
		return -ENOENT;
	}
	
	MegaNode *dest = megaApi->getNodeByPath(to.c_str());
	if (dest)
	{
		if (dest->isFile())
		{
			delete source;
			delete dest;
			MegaApi::log(MegaApi::LOG_LEVEL_WARNING, "The destination is an existing file");
			return -ENOTDIR;
		}
		else
		{
			SynchronousRequestListenerFuse listener;	
			megaApi->moveNode(source, dest, &listener);
			listener.wait();
			delete source;
			delete dest;
			
			if (listener.getError()->getErrorCode() != MegaError::API_OK)
			{
				MegaApi::log(MegaApi::LOG_LEVEL_ERROR, "Error moving file/folder");
				return -EIO;
			}

			MegaApi::log(MegaApi::LOG_LEVEL_DEBUG, "File/folder moved OK");
			return 0;
		}
	}
	
	string destpath = to;
	size_t index = destpath.find_last_of('/');
	if (index == string::npos)
	{
		delete source;
		MegaApi::log(MegaApi::LOG_LEVEL_ERROR, "Invalid path");
		return -ENOENT;
	}
	
	string destname = destpath.c_str() + index + 1;
	destpath.resize(index + 1);
	dest = megaApi->getNodeByPath(destpath.c_str());
	if (!dest)
	{
		delete source;
		MegaApi::log(MegaApi::LOG_LEVEL_WARNING, "Destination folder not found");
		return -ENOENT;
	}
	
	if (dest->isFile())
	{
		delete source;
		delete dest;
		MegaApi::log(MegaApi::LOG_LEVEL_WARNING, "The destination folder is a file");
		return -ENOTDIR;
	}
	
	MegaNode *n = megaApi->getChildNode(dest, destname.c_str());
	if(n)
	{
		delete n;
		delete source;
		delete dest;
		MegaApi::log(MegaApi::LOG_LEVEL_WARNING, "The destination path already exists");
		return -EEXIST;
	}
	
	SynchronousRequestListenerFuse listener;	
	megaApi->moveNode(source, dest, &listener);
	listener.wait();
	delete dest;
	
	if (listener.getError()->getErrorCode() != MegaError::API_OK)
	{
		delete source;
		MegaApi::log(MegaApi::LOG_LEVEL_ERROR, "Error moving file/folder");
		return -EIO;
	}
	MegaApi::log(MegaApi::LOG_LEVEL_DEBUG, "File/folder moved OK");

	if (strcmp(source->getName(), destname.c_str()))
	{
		listener.reset();
		megaApi->renameNode(source, destname.c_str(), &listener);
		listener.wait();
		
		if(listener.getError()->getErrorCode() != MegaError::API_OK)
		{
			delete source;
			MegaApi::log(MegaApi::LOG_LEVEL_ERROR, "Error renaming file/folder");
			return -EIO;
		}
		
		MegaApi::log(MegaApi::LOG_LEVEL_DEBUG, "File/folder renamed OK");
	}
	
	delete source;
	return 0;	
}
Пример #7
0
void NodeSelector::on_bNewFolder_clicked()
{
    QPointer<QInputDialog> id = new QInputDialog(this);
    id->setWindowTitle(tr("New folder"));
    id->setLabelText(tr("Enter the new folder name:"));
    int result = id->exec();

    if (!id || !result)
    {
        delete id;
        return;
    }

    QString text = id->textValue();
    text = text.trimmed();
    if (!text.isEmpty())
    {
        MegaNode *parent = megaApi->getNodeByHandle(selectedFolder);
        if (!parent)
        {
            parent = megaApi->getRootNode();
            if (!parent)
            {
                delete id;
                return;
            }
            selectedFolder = parent->getHandle();
            selectedItem = QModelIndex();
        }

        MegaNode *node = megaApi->getNodeByPath(text.toUtf8().constData(), parent);
        if (!node || node->isFile())
        {
            ui->bNewFolder->setEnabled(false);
            ui->bOk->setEnabled(false);
            ui->tMegaFolders->setEnabled(false);
            megaApi->createFolder(text.toUtf8().constData(), parent, delegateListener);
        }
        else
        {
            for (int i = 0; i < model->rowCount(selectedItem); i++)
            {
                QModelIndex row = model->index(i, 0, selectedItem);
                MegaNode *node = model->getNode(row);

                if (node && text.compare(QString::fromUtf8(node->getName())) == 0)
                {
                    setSelectedFolderHandle(node->getHandle());
                    ui->tMegaFolders->selectionModel()->select(row, QItemSelectionModel::ClearAndSelect);
                    ui->tMegaFolders->selectionModel()->setCurrentIndex(row, QItemSelectionModel::ClearAndSelect);
                    break;
                }
            }
        }
        delete parent;
        delete node;
    }
    else
    {
        QMessageBox::critical(this, QString::fromUtf8("MEGAsync"), tr("Please enter a valid folder name"));
    }
    delete id;
}
Пример #8
0
/**
 * @brief TEST_F SdkTestShares
 *
 * Initialize a test scenario by:
 *
 * - Creating/uploading some folders/files to share
 * - Creating a new contact to share to
 *
 * Performs different operations related to sharing:
 *
 * - Share a folder with an existing contact
 * - Check the correctness of the outgoing share
 * - Check the reception and correctness of the incoming share
 * - Modify the access level
 * - Revoke the access to the share
 * - Share a folder with a non registered email
 * - Check the correctness of the pending outgoing share
 * - Create a public link
 * - Import a public link
 * - Get a node from public link
 * - Remove a public link
 */
TEST_F(SdkTest, SdkTestShares)
{
    MegaShareList *sl;
    MegaShare *s;
    MegaNodeList *nl;
    MegaNode *n;
    MegaNode *n1;

    getMegaApiAux();    // login + fetchnodes


    // Initialize a test scenario : create some folders/files to share

    // Create some nodes to share
    //  |--Shared-folder
    //    |--subfolder
    //    |--file.txt

    MegaNode *rootnode = megaApi->getRootNode();
    char foldername1[64] = "Shared-folder";
    MegaHandle hfolder1;

    responseReceived = false;
    megaApi->createFolder(foldername1, rootnode);
    waitForResponse(&responseReceived);

    ASSERT_EQ(MegaError::API_OK, lastError) << "Cannot create a folder (error: " << lastError << ")";
    hfolder1 = h;     // 'h' is set in 'onRequestFinish()'
    n1 = megaApi->getNodeByHandle(hfolder1);

    char foldername2[64] = "subfolder";
    MegaHandle hfolder2;

    responseReceived = false;
    megaApi->createFolder(foldername2, megaApi->getNodeByHandle(hfolder1));
    waitForResponse(&responseReceived);

    ASSERT_EQ(MegaError::API_OK, lastError) << "Cannot create a folder (error: " << lastError << ")";
    hfolder2 = h;

    MegaHandle hfile1;
    createFile(PUBLICFILE.data(), false);   // not a large file since don't need to test transfers here

    uploadFinished = false;
    megaApi->startUpload(PUBLICFILE.data(), megaApi->getNodeByHandle(hfolder1));
    waitForResponse(&uploadFinished);

    ASSERT_EQ(MegaError::API_OK, lastError) << "Cannot upload file (error: " << lastError << ")";
    hfile1 = h;


    // Initialize a test scenario: create a new contact to share to

    string message = "Hi contact. Let's share some stuff";

    contactRequestUpdated = false;
    contactRequestUpdatedAux = false;

    ASSERT_NO_FATAL_FAILURE( inviteContact(emailaux, message, MegaContactRequest::INVITE_ACTION_ADD) );

    waitForResponse(&contactRequestUpdatedAux); // at the target side (auxiliar account)
    waitForResponse(&contactRequestUpdated);    // at the source side (main account)


    MegaContactRequestList *crlaux = megaApiAux->getIncomingContactRequests();
    ASSERT_EQ(1, crlaux->size()) << "Too many incoming contact requests in auxiliar account";
    MegaContactRequest *craux = crlaux->get(0);

    contactRequestUpdated = false;
    contactRequestUpdatedAux = false;

    ASSERT_NO_FATAL_FAILURE( replyContact(craux, MegaContactRequest::REPLY_ACTION_ACCEPT) );

    waitForResponse(&contactRequestUpdatedAux); // at the target side (auxiliar account)
    waitForResponse(&contactRequestUpdated);    // at the source side (main account)

    delete crlaux;


    // --- Create a new outgoing share ---

    nodeUpdated = false;
    nodeUpdatedAux = false;

    ASSERT_NO_FATAL_FAILURE( shareFolder(n1, emailaux.data(), MegaShare::ACCESS_READ) );

    waitForResponse(&nodeUpdated);
    waitForResponse(&nodeUpdatedAux);


    // --- Check the outgoing share ---

    sl = megaApi->getOutShares();
    ASSERT_EQ(1, sl->size()) << "Outgoing share failed";
    s = sl->get(0);

    ASSERT_EQ(MegaShare::ACCESS_READ, s->getAccess()) << "Wrong access level of outgoing share";
    ASSERT_EQ(hfolder1, s->getNodeHandle()) << "Wrong node handle of outgoing share";
    ASSERT_STREQ(emailaux.data(), s->getUser()) << "Wrong email address of outgoing share";
    ASSERT_TRUE(megaApi->isShared(n1)) << "Wrong sharing information at outgoing share";
    ASSERT_TRUE(megaApi->isOutShare(n1)) << "Wrong sharing information at outgoing share";

    delete sl;


    // --- Check the incoming share ---

    nl = megaApiAux->getInShares(megaApiAux->getContact(email.data()));
    ASSERT_EQ(1, nl->size()) << "Incoming share not received in auxiliar account";
    n = nl->get(0);

    ASSERT_EQ(hfolder1, n->getHandle()) << "Wrong node handle of incoming share";
    ASSERT_STREQ(foldername1, n->getName()) << "Wrong folder name of incoming share";
    ASSERT_EQ(MegaError::API_OK, megaApiAux->checkAccess(n, MegaShare::ACCESS_READ).getErrorCode()) << "Wrong access level of incoming share";
    ASSERT_TRUE(megaApiAux->isInShare(n)) << "Wrong sharing information at incoming share";
    ASSERT_TRUE(megaApiAux->isShared(n)) << "Wrong sharing information at incoming share";

    delete nl;


    // --- Modify the access level of an outgoing share ---

    nodeUpdated = false;
    nodeUpdatedAux = false;

    ASSERT_NO_FATAL_FAILURE( shareFolder(megaApi->getNodeByHandle(hfolder1), emailaux.data(), MegaShare::ACCESS_READWRITE) );

    waitForResponse(&nodeUpdated);
    waitForResponse(&nodeUpdatedAux);

    nl = megaApiAux->getInShares(megaApiAux->getContact(email.data()));
    ASSERT_EQ(1, nl->size()) << "Incoming share not received in auxiliar account";
    n = nl->get(0);

    ASSERT_EQ(MegaError::API_OK, megaApiAux->checkAccess(n, MegaShare::ACCESS_READWRITE).getErrorCode()) << "Wrong access level of incoming share";

    delete nl;


    // --- Revoke access to an outgoing share ---

    nodeUpdated = false;
    nodeUpdatedAux = false;

    ASSERT_NO_FATAL_FAILURE( shareFolder(n1, emailaux.data(), MegaShare::ACCESS_UNKNOWN) );

    waitForResponse(&nodeUpdated);
    waitForResponse(&nodeUpdatedAux);

    sl = megaApi->getOutShares();
    ASSERT_EQ(0, sl->size()) << "Outgoing share revocation failed";
    delete sl;

    nl = megaApiAux->getInShares(megaApiAux->getContact(email.data()));
    ASSERT_EQ(0, nl->size()) << "Incoming share revocation failed";
    delete nl;


    // --- Get pending outgoing shares ---

    char emailfake[64];
    srand (time(NULL));
    sprintf(emailfake, "*****@*****.**", rand()%1000000);
    // carefull, antispam rejects too many tries without response for the same address

    n = megaApi->getNodeByHandle(hfolder2);

    nodeUpdated = false;
    ASSERT_NO_FATAL_FAILURE( shareFolder(n, emailfake, MegaShare::ACCESS_FULL) );
    waitForResponse(&nodeUpdated);

    sl = megaApi->getPendingOutShares(n);
    delete n;
    ASSERT_EQ(1, sl->size()) << "Pending outgoing share failed";
    s = sl->get(0);
    n = megaApi->getNodeByHandle(s->getNodeHandle());

//    ASSERT_STREQ(emailfake, s->getUser()) << "Wrong email address of outgoing share"; User is not created yet
    ASSERT_FALSE(megaApi->isShared(n)) << "Node is already shared, must be pending";
    ASSERT_FALSE(megaApi->isOutShare(n)) << "Node is already shared, must be pending";

    delete sl;
    delete n;


    // --- Create a public link ---

    MegaNode *nfile1 = megaApi->getNodeByHandle(hfile1);

    ASSERT_NO_FATAL_FAILURE( createPublicLink(nfile1) );
    // The created link is stored in this->link at onRequestFinish()


    // --- Import a public link ---

    ASSERT_NO_FATAL_FAILURE( importPublicLink(link, rootnode) );

    MegaNode *nimported = megaApi->getNodeByHandle(h);

    ASSERT_STREQ(nfile1->getName(), nimported->getName()) << "Imported file with wrong name";
    ASSERT_EQ(rootnode->getHandle(), nimported->getParentHandle()) << "Imported file in wrong path";


    // --- Get node from public link ---

    ASSERT_NO_FATAL_FAILURE( getPublicNode(link) );

    ASSERT_TRUE(publicNode->isPublic()) << "Cannot get a node from public link";


    // --- Remove a public link ---

    ASSERT_NO_FATAL_FAILURE( removePublicLink(nfile1) );

    delete nfile1;
    nfile1 = megaApi->getNodeByHandle(h);
    ASSERT_FALSE(nfile1->isPublic()) << "Public link removal failed (still public)";

    delete nimported;
}
Пример #9
0
void MegaUploader::upload(QFileInfo info, MegaNode *parent)
{
    QApplication::processEvents();

    MegaNodeList *children =  megaApi->getChildren(parent);
    QByteArray utf8name = info.fileName().toUtf8();
    QString currentPath = QDir::toNativeSeparators(info.absoluteFilePath());
    MegaNode *dupplicate = NULL;
    for (int i = 0; i < children->size(); i++)
    {
        MegaNode *child = children->get(i);
        if (!strcmp(utf8name.constData(), child->getName())
                && ((info.isDir() && (child->getType() == MegaNode::TYPE_FOLDER))
                    || (info.isFile() && (child->getType() == MegaNode::TYPE_FILE)
                        && (info.size() == child->getSize()))))
        {
            dupplicate = child->copy();
            break;
        }
    }
    delete children;

    if (dupplicate)
    {
        if (dupplicate->getType() == MegaNode::TYPE_FILE)
        {
            emit dupplicateUpload(info.absoluteFilePath(), info.fileName(), dupplicate->getHandle());
        }

        if (dupplicate->getType() == MegaNode::TYPE_FOLDER)
        {
            QDir dir(info.absoluteFilePath());
            QFileInfoList entries = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
            for (int i = 0; i < entries.size(); i++)
            {
                upload(entries[i], dupplicate);
            }
        }
        delete dupplicate;
        return;
    }

    string localPath = megaApi->getLocalPath(parent);
    if (localPath.size() && megaApi->isSyncable(info.fileName().toUtf8().constData()))
    {
#ifdef WIN32
        QString destPath = QDir::toNativeSeparators(QString::fromWCharArray((const wchar_t *)localPath.data()) + QDir::separator() + info.fileName());
        if (destPath.startsWith(QString::fromAscii("\\\\?\\")))
        {
            destPath = destPath.mid(4);
        }
#else
        QString destPath = QDir::toNativeSeparators(QString::fromUtf8(localPath.data()) + QDir::separator() + info.fileName());
#endif
        megaApi->moveToLocalDebris(destPath.toUtf8().constData());
        QtConcurrent::run(Utilities::copyRecursively, currentPath, destPath);
    }
    else if (info.isFile())
    {
        megaApi->startUpload(currentPath.toUtf8().constData(), parent);
    }
    else if (info.isDir())
    {
        folders.enqueue(info);
        megaApi->createFolder(info.fileName().toUtf8().constData(), parent, delegateListener);
    }
}