Ejemplo n.º 1
0
int main(int argc, char **argv)
{
    KApplication app(argc, argv, "kruntest", true /* it _has_ a GUI ! */);

    // First some non-interactive tests
    checkBN("/usr/bin/ls", true, "ls");
    checkBN("/usr/bin/ls", false, "/usr/bin/ls");
    checkBN("/path/to/wine \"long argument with path\"", true, "wine");
    checkBN("/path/with/a/sp\\ ace/exe arg1 arg2", true, "exe");
    checkBN("\"progname\" \"arg1\"", true, "progname");
    checkBN("'quoted' \"arg1\"", true, "quoted");
    checkBN(" 'leading space'   arg1", true, "leading space");

    KURL::List l0;
    KURL::List l1;
    l1 << "file:/tmp";
    KURL::List l2;
    l2 << "http://localhost/foo";
    KURL::List l3;
    l3 << "file:/local/file"
       << "http://remotehost.org/bar";

    static const char
        *execs[] = {"Exec=date -u", "Exec=echo $$PWD"},
        *terms[] = {"Terminal=false", "Terminal=true\nTerminalOptions=-T \"%f - %c\""},
        *sus[] = {"X-KDE-SubstituteUID=false", "X-KDE-SubstituteUID=true\nX-KDE-Username=sprallo"},
        *rslts[] = {
            "'date' '-u'",                                                                                                                 // 0
            "'/bin/sh' '-c' 'echo $PWD '",                                                                                                 // 1
            "'x-term' '-T' ' - just_a_test' '-e' 'date' '-u'",                                                                             // 2
            "'x-term' '-T' ' - just_a_test' '-e' '/bin/sh' '-c' 'echo $PWD '",                                                             // 3
            "'kdesu' '-u' 'sprallo' '-c' 'date -u '",                                                                                      // 4
            "'kdesu' '-u' 'sprallo' '-c' '/bin/sh -c '\\''echo $PWD '\\'''",                                                               // 5
            "'x-term' '-T' ' - just_a_test' '-e' 'su' 'sprallo' '-c' 'date -u '",                                                          // 6
            "'x-term' '-T' ' - just_a_test' '-e' 'su' 'sprallo' '-c' '/bin/sh -c '\\''echo $PWD '\\'''",                                   // 7
            "'date -u '",                                                                                                                  // 8
            "'echo $PWD '",                                                                                                                // 9
            "'x-term -T \" - just_a_test\"' '-e' 'date -u '",                                                                              // a
            "'x-term -T \" - just_a_test\"' '-e' '/bin/sh -c '\\''echo $PWD '\\'''",                                                       // b
            "'kdesu' '-u' 'sprallo' '-c' ''\\''date -u '\\'''",                                                                            // c
            "'kdesu' '-u' 'sprallo' '-c' ''\\''/bin/sh -c '\\''\\'\\'''\\''echo $PWD '\\''\\'\\'''\\'''\\'''",                             // d
            "'x-term -T \" - just_a_test\"' '-e' 'su' 'sprallo' '-c' ''\\''date -u '\\'''",                                                // e
            "'x-term -T \" - just_a_test\"' '-e' 'su' 'sprallo' '-c' ''\\''/bin/sh -c '\\''\\'\\'''\\''echo $PWD '\\''\\'\\'''\\'''\\'''", // f
        };
    for(int hs = 0; hs < 2; hs++)
        for(int su = 0; su < 2; su++)
            for(int te = 0; te < 2; te++)
                for(int ex = 0; ex < 2; ex++)
                {
                    int fd = creat("kruntest.desktop", 0666);
                    FILE *f;
                    if(fd < 0)
                        abort();
                    f = KDE_fdopen(fd, "w");
                    fprintf(f,
                            "[Desktop Entry]\n"
                            "Type=Application\n"
                            "Name=just_a_test\n"
                            "Icon=~/icon.png\n"
                            "%s\n%s\n%s\n",
                            execs[ex], terms[te], sus[su]);
                    close(fd);
                    fclose(f);
                    KService s(QDir::currentDirPath() + "/kruntest.desktop");
                    unlink("kruntest.desktop");
                    checkPDE(s, l0, hs, false, rslts[ex + te * 2 + su * 4 + hs * 8]);
                }

    KService s1("dummy", "kate %U", "app");
    checkPDE(s1, l0, false, false, "'kate'");
    checkPDE(s1, l1, false, false, "'kate' '/tmp'");
    checkPDE(s1, l2, false, false, "'kate' 'http://localhost/foo'");
    checkPDE(s1, l3, false, false, "'kate' '/local/file' 'http://remotehost.org/bar'");
    KService s2("dummy", "kate %u", "app");
    checkPDE(s2, l0, false, false, "'kate'");
    checkPDE(s2, l1, false, false, "'kate' '/tmp'");
    checkPDE(s2, l2, false, false, "'kate' 'http://localhost/foo'");
    checkPDE(s2, l3, false, false, "'kate'");
    KService s3("dummy", "kate %F", "app");
    checkPDE(s3, l0, false, false, "'kate'");
    checkPDE(s3, l1, false, false, "'kate' '/tmp'");
    checkPDE(s3, l2, false, false, "'kfmexec' 'kate %F' 'http://localhost/foo'");
    checkPDE(s3, l3, false, false, "'kfmexec' 'kate %F' 'file:/local/file' 'http://remotehost.org/bar'");

    checkPDE(s3, l1, false, true, "'kfmexec' '--tempfiles' 'kate %F' 'file:/tmp'");
    checkPDE(s3, l1, true, true, "''\\''kfmexec'\\'' '\\''--tempfiles'\\'' '\\''kate %F'\\'' '\\''file:/tmp'\\'''");

    KService s4("dummy", "sh -c \"kate \"'\\\"'\"%F\"'\\\"'", "app");
    checkPDE(s4, l1, false, false, "'kate' '\"/tmp\"'");

    Receiver receiver;
    app.setMainWidget(&receiver);
    return app.exec();
}
Ejemplo n.º 2
0
/* antlarr: KDE 4.0:  make the first parameter "const QString &" */
bool KConfigINIBackEnd::writeConfigFile(QString filename, bool bGlobal, bool bMerge)
{
    // is the config object read-only?
    if(pConfig->isReadOnly())
        return true; // pretend we wrote it

    KEntryMap aTempMap;
    QFile *mergeFile = (bMerge ? new QFile(filename) : 0);
    bool bEntriesLeft = getEntryMap(aTempMap, bGlobal, mergeFile);
    delete mergeFile;
    if(bFileImmutable)
        return true; // pretend we wrote it

    // OK now the temporary map should be full of ALL entries.
    // write it out to disk.

    // Check if file exists:
    int fileMode = -1;
    bool createNew = true;

    KDE_struct_stat buf;
    if(KDE_stat(QFile::encodeName(filename), &buf) == 0)
    {
        if(buf.st_uid == getuid())
        {
            // Preserve file mode if file exists and is owned by user.
            fileMode = buf.st_mode & 0777;
        }
        else
        {
            // File is not owned by user:
            // Don't create new file but write to existing file instead.
            createNew = false;
        }
    }

    KSaveFile *pConfigFile = 0;
    FILE *pStream = 0;

    if(createNew)
    {
        pConfigFile = new KSaveFile(filename, 0600);

        if(pConfigFile->status() != 0)
        {
            delete pConfigFile;
            return bEntriesLeft;
        }

        if(!bGlobal && (fileMode == -1))
            fileMode = mFileMode;

        if(fileMode != -1)
        {
            fchmod(pConfigFile->handle(), fileMode);
        }

        pStream = pConfigFile->fstream();
    }
    else
    {
        // Open existing file.
        // We use open() to ensure that we call without O_CREAT.
        int fd = KDE_open(QFile::encodeName(filename), O_WRONLY | O_TRUNC);
        if(fd < 0)
        {
            return bEntriesLeft;
        }
        pStream = KDE_fdopen(fd, "w");
        if(!pStream)
        {
            close(fd);
            return bEntriesLeft;
        }
    }

    writeEntries(pStream, aTempMap);

    if(pConfigFile)
    {
        bool bEmptyFile = (ftell(pStream) == 0);
        if(bEmptyFile && ((fileMode == -1) || (fileMode == 0600)))
        {
            // File is empty and doesn't have special permissions: delete it.
            ::unlink(QFile::encodeName(filename));
            pConfigFile->abort();
        }
        else
        {
            // Normal case: Close the file
            pConfigFile->close();
        }
        delete pConfigFile;
    }
    else
    {
        fclose(pStream);
    }

    return bEntriesLeft;
}