summaryrefslogtreecommitdiff
path: root/install.c
blob: ecccfe0968eb51b0d0fb377169d845cadce3f66c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "install.h"
#include "lib/rpmlib.h"
#include "messages.h"
#include "query.h"

static int hashesPrinted = 0;

static void printHash(const unsigned long amount, const unsigned long total);
static void printPercent(const unsigned long amount, const unsigned long total);

static void printHash(const unsigned long amount, const unsigned long total) {
    int hashesNeeded;

    if (hashesPrinted != 50) {
	hashesNeeded = 50 * (((float) amount) / total);
	while (hashesNeeded > hashesPrinted) {
	    printf("#");
	    hashesPrinted++;
	}
	fflush(stdout);
	hashesPrinted = hashesNeeded;

	if (hashesPrinted == 50)
	    printf("\n");
    }
}

static void printPercent(const unsigned long amount, const unsigned long total) 
{
    printf("%%%% %f\n", (float) (((float) amount) / total) * 100);
}

void doInstall(char * prefix, char * arg, int installFlags, int interfaceFlags) {
    rpmdb db;
    int fd;
    int mode, rc;
    char * chptr;
    notifyFunction fn;

    hashesPrinted = 0;

    if (installFlags & INSTALL_TEST) 
	mode = O_RDONLY;
    else
	mode = O_RDWR | O_EXCL;

    if (interfaceFlags & RPMINSTALL_PERCENT)
	fn = printPercent;
    else if (interfaceFlags & RPMINSTALL_HASH)
	fn = printHash;
    else
	fn = NULL;
	
    if (!rpmdbOpen(prefix, &db, mode, 0644)) {
	/* try opening it O_CREAT */
	mode |= O_CREAT;
	if (!rpmdbOpen(prefix, &db, mode, 0644)) {
	    fprintf(stderr, "error: cannot open %s/var/lib/rpm/packages.rpm\n", 
			prefix);
	    exit(1);
	}
    }

    message(MESS_DEBUG, "installing %s\n", arg);
    fd = open(arg, O_RDONLY);
    if (fd < 0) {
	rpmdbClose(db);
	fprintf(stderr, "error: cannot open %s\n", arg);
	return;
    }

    if (interfaceFlags & RPMINSTALL_PERCENT) 
	printf("%%f %s\n", arg);
    else if (isVerbose() && (interfaceFlags & RPMINSTALL_HASH)) {
	chptr = strrchr(arg, '/');
	if (!chptr)
	    chptr = arg;
	else
	    chptr++;

	printf("%-28s", chptr);
    } else if (isVerbose())
	printf("Installing %s\n", arg);

    rc = rpmInstallPackage(prefix, db, fd, installFlags, fn);
    if (rc == 1) {
	fprintf(stderr, "error: %s cannot be installed\n", arg);
    }

    close(fd);
    rpmdbClose(db);
}

void doUninstall(char * prefix, char * arg, int test, int uninstallFlags) {
    rpmdb db;
    dbIndexSet matches;
    int i;
    int mode;
    int rc;
    int count;

    if (test) 
	mode = O_RDONLY;
    else
	mode = O_RDWR | O_EXCL;
	
    if (!rpmdbOpen(prefix, &db, mode, 0644)) {
	fprintf(stderr, "cannot open %s/var/lib/rpm/packages.rpm\n", prefix);
	exit(1);
    }
   
    rc = findPackageByLabel(db, arg, &matches);
    if (rc == 1) 
	fprintf(stderr, "package %s is not installed\n", arg);
    else if (rc == 2) 
	fprintf(stderr, "error searching for package %s\n", arg);
    else {
	count = 0;
	for (i = 0; i < matches.count; i++)
	    if (matches.recs[i].recOffset) count++;

	if (count > 1) {
	    fprintf(stderr, "\"%s\" specifies multiple packages\n", arg);
	}
	else { 
	    for (i = 0; i < matches.count; i++) {
		if (matches.recs[i].recOffset) {
		    message(MESS_DEBUG, "uninstalling record number %d\n",
				matches.recs[i].recOffset);
		    rpmRemovePackage(prefix, db, matches.recs[i].recOffset, 
				     test);
		}
	    }
	}

	freeDBIndexRecord(matches);
    }

    rpmdbClose(db);
}

int doSourceInstall(char * prefix, char * arg, char ** specFile) {
    int fd;
    int rc;

    fd = open(arg, O_RDONLY);
    if (fd < 0) {
	fprintf(stderr, "error: cannot open %s\n", arg);
	return 1;
    }

    if (isVerbose())
	printf("Installing %s\n", arg);

    rc = rpmInstallSourcePackage(prefix, fd, specFile);
    if (rc == 1) {
	fprintf(stderr, "error: %s cannot be installed\n", arg);
    }

    close(fd);

    return rc;
}