Ejemplo n.º 1
0
int loop(int sock) {
    struct stat obj;
    size_t size;
    int success;
    char buf[128], filename[96], command[8], *response;

    int i = 1;
    while(1)
    {
        printf("%s\n","Enter a command (ls, cd, pwd, rm, downl, upld, rpwd, rls, rcd, quit): ");
        scanf("%s", command);

        if(!strcmp(command, "ls")) {
            if(ls()) {
                printf("%s\n","The remote directory listing is as follows: ");
                system("cat ls.txt");
                system("rm ls.txt");
            }
        } else if(!strcmp(command, "cd")) {
            printf("%s\n","Enter the path to change the directory: ");
            scanf("%s", filename);
            printf("%s\n", cd(filename) ? "Directory changed successfully !" : "Error when trying to change directory");
        }  else if(!strcmp(command, "pwd")) {
            if(pwd()) {
                printf("%s\n","The current directory name is: \n");
                system("cat pwd.txt");
                system("rm pwd.txt");
            }
        }  else if(!strcmp(command, "rm")) {
            printf("%s\n","Enter the filename to remove: ");
            scanf("%s", filename);
            printf("%s\n", rm(filename) ? "File removed successfully !" : "Error when trying to remove file");
        } else if(!strcmp(command, "downl")) {
            printf("%s\n","Enter filename to get: ");
            scanf("%s", filename);
            success = downl(sock, filename, buf, size, i);
            printf(success ? "File successfully downloaded\n" : "Failed to download the requested file\n");
        } else if(!strcmp(command, "upld")) {
            printf("%s\n","Enter filename to put to server: ");
            scanf("%s", filename);
            success = upld(sock, filename, buf, obj, size);
            printf("%s\n", success ? "File successfully uploaded\n" : "Failed to upload the given file\n");
        } else if(!strcmp(command, "rpwd")) {
            response = rpwd(sock, buf);
            printf("%s%s\n","The current remote directory name is: ", response);
        } else if(!strcmp(command, "rls")) {
            success = rls(sock, buf, size);
            if(success) {
                printf("%s\n","The remote directory listing is as follows:\n");
                system("cat temp.txt");
                system("rm temp.txt");
            }
        } else if(!strcmp(command, "rcd")) {
            printf("%s\n", "Enter the path to change the remote directory: ");
            scanf("%s", filename);
            success = rcd(sock, filename, buf);
            printf("%s\n", success ? "Directory changed successfully !" : "Error when trying to change directory");
        } else if(!strcmp(command, "quit")) {
            success = quit(sock, buf);
            if(success) {
                printf("%s\n", "Server closed\nQuitting..\n");
                return 0;
            }
            printf("%s\n", "Server failed to close the connection\n");
        } else {
            printf("%s\n", "Please choose a choice from the list.");
        }
    }
}
Ejemplo n.º 2
0
void dtkUpdaterPrivate::onRequestFinished(QNetworkReply *reply)
{

    if (reply->url() == cfgUrl) { // WARN: the url may not be the same ...

        if(!cfgFile->open(QFile::ReadWrite)) {
            qDebug() << "Unable to open config file  for writing.";
            return;
        }
        cfgFile->write(reply->readAll());
        cfgFile->flush();
        cfgFile->close();

        if(!cfgFile->openMode() == QIODevice::NotOpen)
            cfgFile->close();

        if (!cfgFile->open(QFile::ReadOnly | QFile::Text))
            qDebug() << "Unable to open stream for reading.";

        QXmlStreamReader reader(cfgFile);

        while (!reader.atEnd()) {
            reader.readNext();
            if (reader.isStartElement() && reader.attributes().hasAttribute("version") && reader.attributes().value("version").toString() > qApp->applicationVersion()) {
                reader.readNext();
                if(reader.isCharacters()) {
                    binUrl.setUrl(reader.text().toString());
                }
            }
        }

        if(!cfgFile->openMode() == QIODevice::NotOpen)
            cfgFile->close();

        if (reader.error())
            qDebug() << reader.error() << reader.errorString();

        if(binUrl.isEmpty()) {
            qDebug() << "You are up to date at version" << qApp->applicationVersion();
            return;
        } else {
            qDebug() << "Updates are available (you have " << qApp->applicationVersion() << "), would you like to download ?";
        }

        char c = getchar(); getchar();

        if(c == 'y')
            downl(binUrl);

    }  else {

        binFile->write(reply->readAll());
        binFile->flush();
        binFile->close();

        qDebug() << "Download completed, would you like to install ?";

        char c = getchar(); getchar();

        if(c == 'y')
            extract();
    }
}