| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | #include <sys/wait.h> |
| 6 | #include "repos.h" |
| 7 | |
| 8 | int |
| 9 | runCmd (char *const argv[]) |
| 10 | { |
| 11 | pid_t pid = fork (); |
| 12 | if (pid == 0) |
| 13 | { |
| 14 | execvp (argv[0], argv); |
| 15 | perror ("execvp failed"); |
| 16 | _exit (1); |
| 17 | } |
| 18 | else if (pid > 0) |
| 19 | { |
| 20 | int status; |
| 21 | waitpid (pid, &status, 0); |
| 22 | return WIFEXITED (status) && WEXITSTATUS (status) == 0; |
| 23 | } |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | int |
| 28 | runShellString (const char *cmd) |
| 29 | { |
| 30 | if (!cmd || strlen (cmd) == 0) |
| 31 | return 1; |
| 32 | |
| 33 | if (strcmp (cmd, "true") == 0) |
| 34 | return 1; |
| 35 | |
| 36 | printf ("[EXEC] %s\n", cmd); |
| 37 | char *const argv[] = { "/bin/sh", "-c", (char *) cmd, NULL }; |
| 38 | return runCmd (argv); |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | install_repo (const Repository *repo) |
| 43 | { |
| 44 | printf ("=== Installing %s ===\n", repo->name); |
| 45 | |
| 46 | char repoDir[256]; |
| 47 | snprintf (repoDir, sizeof (repoDir), "/tmp/slpkg-%s", repo->name); |
| 48 | |
| 49 | char *const cloneArgv[] = |
| 50 | { "git", "clone", (char *) repo->url, repoDir, NULL }; |
| 51 | printf ("[EXEC] git clone %s %s\n", repo->url, repoDir); |
| 52 | if (!runCmd (cloneArgv)) |
| 53 | { |
| 54 | fprintf (stderr, "Failed to clone repository.\n"); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if (chdir (repoDir) != 0) |
| 59 | { |
| 60 | perror ("chdir failed"); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (!runShellString (repo->buildCmd)) |
| 65 | { |
| 66 | fprintf (stderr, "Build failed.\n"); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (!runShellString (repo->installCmd)) |
| 71 | { |
| 72 | fprintf (stderr, "Install failed.\n"); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | printf ("Successfully installed %s!\n", repo->name); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | uninstall_repo (const Repository *repo) |
| 81 | { |
| 82 | char repoDir[256]; |
| 83 | snprintf (repoDir, sizeof (repoDir), "/tmp/slpkg-%s", repo->name); |
| 84 | |
| 85 | if (chdir (repoDir) != 0) |
| 86 | { |
| 87 | perror ("chdir failed"); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | printf ("=== Uninstalling %s ===\n", repo->name); |
| 92 | if (!runShellString (repo->uninstallCmd)) |
| 93 | { |
| 94 | fprintf (stderr, "Uninstall failed.\n"); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | int |
| 99 | main (int argc, char *argv[]) |
| 100 | { |
| 101 | if (argc < 3) |
| 102 | { |
| 103 | printf ("Usage: %s <install|uninstall> <repo_name>\n", argv[0]); |
| 104 | printf ("\nAvailable repositories:\n"); |
| 105 | for (size_t i = 0; i < REPO_COUNT; i++) |
| 106 | { |
| 107 | printf (" - %s\n", REPOS[i].name); |
| 108 | } |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | const char *action = argv[1]; |
| 113 | const char *target = argv[2]; |
| 114 | |
| 115 | for (size_t i = 0; i < REPO_COUNT; i++) |
| 116 | { |
| 117 | if (strcmp (REPOS[i].name, target) == 0) |
| 118 | { |
| 119 | if (strcmp (action, "install") == 0) |
| 120 | { |
| 121 | install_repo (&REPOS[i]); |
| 122 | } |
| 123 | else if (strcmp (action, "uninstall") == 0) |
| 124 | { |
| 125 | uninstall_repo (&REPOS[i]); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | fprintf (stderr, "Unknown action: %s\n", action); |
| 130 | } |
| 131 | return 0; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | fprintf (stderr, "Repository '%s' not found.\n", target); |
| 136 | return 1; |
| 137 | } |