Пример #1
0
static void PopulateAvailable(const char *arg)
{
    Package *p = DeserializePackage(arg);
    PackagePattern *pattern = MatchSame(p);

    if (FindPackages(AVAILABLE_PACKAGES_FILE_NAME, pattern) != NULL)
    {
        fprintf(stderr, "Skipping already available package %s\n", SerializePackage(p));
        return;
    }

    Rlist *available_packages = ReadPackageEntries(AVAILABLE_PACKAGES_FILE_NAME);

    RlistAppendAlien(&available_packages, p);
    SavePackages(AVAILABLE_PACKAGES_FILE_NAME, available_packages);
}
Пример #2
0
static Seq *FindPackages(const char *database_filename, PackagePattern *pattern)
{
    Seq *db = ReadPackageEntries(database_filename);
    Seq *matching = SeqNew(1000, NULL);

    for (size_t i = 0; i < SeqLength(db); i++)
    {
        Package *package = SeqAt(db, i);

        if (MatchPackage(pattern, package))
        {
            SeqAppend(matching, package);
        }
    }

    return matching;
}
Пример #3
0
static Rlist *FindPackages(const char *database_filename, PackagePattern *pattern)
{
    Rlist *db = ReadPackageEntries(database_filename);
    Rlist *matching = NULL;

    Rlist *rp = NULL;

    for (rp = db; rp != NULL; rp = rp->next)
    {
        Package *package = (Package *) rp->item;

        if (MatchPackage(pattern, package))
        {
            RlistAppendAlien(&matching, package);
        }
    }

    return matching;
}
Пример #4
0
static void AddPackage(PackagePattern *pattern)
{
    fprintf(stderr, "Trying to install all packages matching pattern %s\n", SerializePackagePattern(pattern));

    Rlist *matching_available = FindPackages(AVAILABLE_PACKAGES_FILE_NAME, pattern);

    if (matching_available == NULL)
    {
        fprintf(stderr, "Unable to find any package matching %s\n", SerializePackagePattern(pattern));
        exit(1);
    }

    Rlist *rp;

    for (rp = matching_available; rp; rp = rp->next)
    {
        Package *p = (Package *) rp->item;

        PackagePattern *pat = MatchAllVersions((Package *) rp->item);

        if (FindPackages(INSTALLED_PACKAGES_FILE_NAME, pat) != NULL)
        {
            fprintf(stderr, "Package %s is already installed.\n", SerializePackage(p));
            exit(1);
        }
    }

    Rlist *installed_packages = ReadPackageEntries(INSTALLED_PACKAGES_FILE_NAME);

    for (rp = matching_available; rp; rp = rp->next)
    {
        Package *p = (Package *) rp->item;

        RlistAppendAlien(&installed_packages, p);
        fprintf(stderr, "Succesfully installed package %s\n", SerializePackage(p));
    }

    SavePackages(INSTALLED_PACKAGES_FILE_NAME, installed_packages);
    exit(0);
}
Пример #5
0
static void AddPackage(PackagePattern *pattern)
{
    fprintf(stderr, "Trying to install all packages matching pattern %s\n", SerializePackagePattern(pattern));

    Seq *matching_available = FindPackages(AVAILABLE_PACKAGES_FILE_NAME, pattern);

    if (SeqLength(matching_available) == 0)
    {
        fprintf(stderr, "Unable to find any package matching %s\n", SerializePackagePattern(pattern));
        exit(EXIT_FAILURE);
    }

    for (size_t i = 0; i < SeqLength(matching_available); i++)
    {
        Package *p = SeqAt(matching_available, i);

        PackagePattern *pat = MatchAllVersions(p);

        if (SeqLength(FindPackages(INSTALLED_PACKAGES_FILE_NAME, pat)) > 0)
        {
            fprintf(stderr, "Package %s is already installed.\n", SerializePackage(p));
            exit(EXIT_FAILURE);
        }
    }

    Seq *installed_packages = ReadPackageEntries(INSTALLED_PACKAGES_FILE_NAME);

    for (size_t i = 0; i < SeqLength(matching_available); i++)
    {
        Package *p = SeqAt(matching_available, i);

        SeqAppend(installed_packages, p);
        fprintf(stderr, "Successfully installed package %s\n", SerializePackage(p));
    }

    SavePackages(INSTALLED_PACKAGES_FILE_NAME, installed_packages);
    exit(EXIT_SUCCESS);
}
Пример #6
0
int main(int argc, char *argv[])
{
    extern char *optarg;
    int option_index = 0;
    int c;

    char *workdir = getenv("CFENGINE_TEST_OVERRIDE_WORKDIR");

    snprintf(AVAILABLE_PACKAGES_FILE_NAME, 256,
             "%s/cfengine-mock-package-manager-available", workdir ? workdir : "/tmp");
    snprintf(INSTALLED_PACKAGES_FILE_NAME, 256,
             "%s/cfengine-mock-package-manager-installed", workdir ? workdir : "/tmp");

    while ((c = getopt_long(argc, argv, "", OPTIONS, &option_index)) != EOF)
    {
        PackagePattern *pattern = NULL;

        switch (c)
        {
        case 'c':
            ClearInstalledPackages();
            break;

        case 'C':
            ClearAvailablePackages();
            break;

        case 'l':
        {
            Rlist *installed_packages = ReadPackageEntries(INSTALLED_PACKAGES_FILE_NAME);

            ShowPackages(stdout, installed_packages);
        }
            break;

        case 'L':
        {
            Rlist *available_packages = ReadPackageEntries(AVAILABLE_PACKAGES_FILE_NAME);

            ShowPackages(stdout, available_packages);
        }
            break;

        case 'a':
            pattern = DeserializePackagePattern(optarg);
            AddPackage(pattern);
            break;

        case 'P':
            PopulateAvailable(optarg);
            break;

            /* case 'd': */
            /*         DeletePackage(pattern); */
            /*         break; */

            /* case 'r': */
            /*    ReinstallPackage(pattern); */
            /*    break; */

            /* case 'u': */
            /*    UpdatePackage(pattern); */
            /*    break; */

            /* case 'U': */
            /*    AddUpdatePackage(pattern); */
            /*    break; */

            /* case 'v': */
            /*         VerifyPackage(pattern); */
            /*         break; */

        default:
            Syntax("mock-package-manager - pretend that you are managing packages!", OPTIONS, HINTS, ID, false);
            exit(1);
        }

    }

    return 0;
}
Пример #7
0
int main(int argc, char *argv[])
{
    extern char *optarg;
    int option_index = 0;
    int c;

#ifdef __MINGW32__
    InitializeWindows();
#endif

    char *workdir = getenv("CFENGINE_TEST_OVERRIDE_WORKDIR");
    char *tempdir = getenv("TEMP");

    if (!workdir && !tempdir)
    {
        fprintf(stderr, "Please set either CFENGINE_TEST_OVERRIDE_WORKDIR or TEMP environment variables\n"
                "to a valid directory.\n");
        return 2;
    }

    xsnprintf(AVAILABLE_PACKAGES_FILE_NAME, 256,
             "%s/cfengine-mock-package-manager-available", workdir ? workdir : tempdir);
    xsnprintf(INSTALLED_PACKAGES_FILE_NAME, 256,
             "%s/cfengine-mock-package-manager-installed", workdir ? workdir : tempdir);

    while ((c = getopt_long(argc, argv, "", OPTIONS, &option_index)) != EOF)
    {
        PackagePattern *pattern = NULL;

        switch (c)
        {
        case 'c':
            ClearInstalledPackages();
            break;

        case 'C':
            ClearAvailablePackages();
            break;

        case 'l':
            {
                Seq *installed_packages = ReadPackageEntries(INSTALLED_PACKAGES_FILE_NAME);
                ShowPackages(stdout, installed_packages);
            }
            break;

        case 'L':
            {
                Seq *available_packages = ReadPackageEntries(AVAILABLE_PACKAGES_FILE_NAME);
                ShowPackages(stdout, available_packages);
            }
            break;

        case 'a':
            pattern = DeserializePackagePattern(optarg);
            AddPackage(pattern);
            break;

        case 'P':
            PopulateAvailable(optarg);
            break;

            /* case 'd': */
            /*         DeletePackage(pattern); */
            /*         break; */

            /* case 'r': */
            /*    ReinstallPackage(pattern); */
            /*    break; */

            /* case 'u': */
            /*    UpdatePackage(pattern); */
            /*    break; */

            /* case 'U': */
            /*    AddUpdatePackage(pattern); */
            /*    break; */

            /* case 'v': */
            /*         VerifyPackage(pattern); */
            /*         break; */

        default:
            {
                Writer *w = FileWriter(stdout);
                WriterWriteHelp(w, "mock-package-manager - pretend that you are managing packages!", OPTIONS, HINTS, false);
                FileWriterDetach(w);
            }
            exit(EXIT_FAILURE);
        }

    }

    return 0;
}