Imported Upstream version 0.6.13
[platform/upstream/libsolv.git] / examples / solv / repoinfo_system_debian.c
1 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <sys/stat.h>
10
11 #include "pool.h"
12 #include "repo.h"
13 #include "repo_deb.h"
14 #include "transaction.h"
15
16 #include "repoinfo.h"
17 #include "repoinfo_cache.h"
18 #include "repoinfo_system_debian.h"
19
20 static void
21 rundpkg(const char *arg, const char *name, int dupfd3, const char *rootdir)
22 {
23   pid_t pid;
24   int status;
25
26   if ((pid = fork()) == (pid_t)-1)
27     {
28       perror("fork");
29       exit(1);
30     }
31   if (pid == 0)
32     {
33       if (!rootdir)
34         rootdir = "/";
35       if (dupfd3 != -1 && dupfd3 != 3)
36         {
37           dup2(dupfd3, 3);
38           close(dupfd3);
39         }
40       if (dupfd3 != -1)
41         fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
42       if (strcmp(arg, "--install") == 0)
43         execlp("dpkg", "dpkg", "--install", "--root", rootdir, "--force", "all", name, (char *)0);
44       else
45         execlp("dpkg", "dpkg", "--remove", "--root", rootdir, "--force", "all", name, (char *)0);
46       perror("dpkg");
47       _exit(0);
48     }
49   while (waitpid(pid, &status, 0) != pid)
50     ;
51   if (status)
52     {
53       printf("dpkg failed\n");
54       exit(1);
55     }
56 }
57
58 int
59 read_installed_debian(struct repoinfo *cinfo)
60 {
61   struct stat stb;
62   Repo *repo = cinfo->repo;
63   Pool *pool = repo->pool;
64
65   memset(&stb, 0, sizeof(stb));
66   printf("dpgk database:");
67   if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/dpkg/status"), &stb))
68     memset(&stb, 0, sizeof(stb));
69   calc_cookie_stat(&stb, REPOKEY_TYPE_SHA256, 0, cinfo->cookie);
70   cinfo->cookieset = 1;
71   if (usecachedrepo(cinfo, 0, 0))
72     {
73       printf(" cached\n");
74       return 1;
75     }
76   if (repo_add_debdb(repo, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE | REPO_USE_ROOTDIR))
77     {
78       fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
79       return 0;
80     }
81   repo_internalize(repo);
82   writecachedrepo(cinfo, 0, 0);
83   return 1;
84 }
85
86 void
87 commit_transactionelement_debian(Pool *pool, Id type, Id p, FILE *fp)
88 {
89   Solvable *s = pool_id2solvable(pool, p);
90   const char *rootdir = pool_get_rootdir(pool);
91
92   switch(type)
93     {   
94     case SOLVER_TRANSACTION_ERASE:
95       rundpkg("--remove", pool_id2str(pool, s->name), 0, rootdir);
96       break;
97     case SOLVER_TRANSACTION_INSTALL:
98     case SOLVER_TRANSACTION_MULTIINSTALL:
99       rewind(fp);
100       lseek(fileno(fp), 0, SEEK_SET);
101       rundpkg("--install", "/dev/fd/3", fileno(fp), rootdir);
102       break;
103     default:
104       break;
105     }   
106 }
107
108 #endif