summaryrefslogtreecommitdiff
path: root/src/pacman/query.c
blob: f5862a2d6128c91e963a3912cfc7f7812c08b8f9 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
 *  query.c
 *
 *  Copyright (c) 2006-2013 Pacman Development Team <pacman-dev@archlinux.org>
 *  Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#include <alpm.h>
#include <alpm_list.h>

/* pacman */
#include "pacman.h"
#include "package.h"
#include "check.h"
#include "conf.h"
#include "util.h"

#define LOCAL_PREFIX "local/"

/* check if filename exists in PATH */
static int search_path(char **filename, struct stat *bufptr)
{
  char *envpath, *envpathsplit, *path, *fullname;
  size_t flen;

  if((envpath = getenv("PATH")) == NULL) {
    return -1;
  }
  if((envpath = envpathsplit = strdup(envpath)) == NULL) {
    return -1;
  }

  flen = strlen(*filename);

  while((path = strsep(&envpathsplit, ":")) != NULL) {
    size_t plen = strlen(path);

    /* strip the trailing slash if one exists */
    while(path[plen - 1] == '/') {
      path[--plen] = '\0';
    }

    fullname = malloc(plen + flen + 2);
    if(!fullname) {
      free(envpath);
      return -1;
    }
    sprintf(fullname, "%s/%s", path, *filename);

    if(lstat(fullname, bufptr) == 0) {
      free(*filename);
      *filename = fullname;
      free(envpath);
      return 0;
    }
    free(fullname);
  }
  free(envpath);
  return -1;
}

static void print_query_fileowner(const char *filename, alpm_pkg_t *info)
{
  if(!config->quiet) {
    printf(_("%s is owned by %s %s\n"), filename,
        alpm_pkg_get_name(info), alpm_pkg_get_version(info));
  } else {
    printf("%s\n", alpm_pkg_get_name(info));
  }
}

/** Resolve the canonicalized absolute path of a symlink.
 * @param path path to resolve
 * @param resolved_path destination for the resolved path, will be malloc'd if
 * NULL
 * @return the resolved path
 */
static char *lrealpath(const char *path, char *resolved_path)
{
  const char *bname = mbasename(path);
  char *rpath = NULL, *dname = NULL;
  int success = 0;

  if(strcmp(path, ".") == 0 || strcmp(path, "..") == 0) {
    /* the entire path needs to be resolved */
    return realpath(path, resolved_path);
  }

  if(!(dname = mdirname(path))) {
    goto cleanup;
  }
  if(!(rpath = realpath(dname, NULL))) {
    goto cleanup;
  }
  if(!resolved_path) {
    if(!(resolved_path = malloc(strlen(rpath) + strlen(bname) + 2))) {
      goto cleanup;
    }
  }

  strcpy(resolved_path, rpath);
  if(resolved_path[strlen(resolved_path) - 1] != '/') {
    strcat(resolved_path, "/");
  }
  strcat(resolved_path, bname);
  success = 1;

cleanup:
  free(dname);
  free(rpath);

  return (success ? resolved_path : NULL);
}

static int query_fileowner(alpm_list_t *targets)
{
  int ret = 0;
  char path[PATH_MAX];
  size_t rootlen;
  alpm_list_t *t;
  alpm_db_t *db_local;
  alpm_list_t *packages;

  /* This code is here for safety only */
  if(targets == NULL) {
    pm_printf(ALPM_LOG_ERROR, _("no file was specified for --owns\n"));
    return 1;
  }

  /* Set up our root path buffer. We only need to copy the location of root in
   * once, then we can just overwrite whatever file was there on the previous
   * iteration. */

  /* resolve root now so any symlinks in it will only have to be resolved once */
  if(!realpath(alpm_option_get_root(config->handle), path)) {
    pm_printf(ALPM_LOG_ERROR, _("cannot determine real path for '%s': %s\n"),
        path, strerror(errno));
    return 1;
  }

  /* make sure there's enough room to append the package file to path */
  rootlen = strlen(path);
  if(rootlen + 2 > PATH_MAX) {
    pm_printf(ALPM_LOG_ERROR, _("path too long: %s%s\n"), path, "");
    return 1;
  }

  /* append trailing '/' removed by realpath */
  if(path[rootlen - 1] != '/') {
    path[rootlen++] = '/';
    path[rootlen] = '\0';
  }

  db_local = alpm_get_localdb(config->handle);
  packages = alpm_db_get_pkgcache(db_local);

  for(t = targets; t; t = alpm_list_next(t)) {
    char *filename = NULL;
    char rpath[PATH_MAX], *rel_path;
    struct stat buf;
    alpm_list_t *i;
    size_t len, is_dir;
    unsigned int found = 0;

    if((filename = strdup(t->data)) == NULL) {
      goto targcleanup;
    }

    /* trailing '/' causes lstat to dereference directory symlinks */
    len = strlen(filename) - 1;
    while(len > 0 && filename[len] == '/') {
      filename[len--] = '\0';
    }

    if(lstat(filename, &buf) == -1) {
      /*  if it is not a path but a program name, then check in PATH */
      if(strchr(filename, '/') == NULL) {
        if(search_path(&filename, &buf) == -1) {
          pm_printf(ALPM_LOG_ERROR, _("failed to find '%s' in PATH: %s\n"),
              filename, strerror(errno));
          goto targcleanup;
        }
      } else {
        pm_printf(ALPM_LOG_ERROR, _("failed to read file '%s': %s\n"),
            filename, strerror(errno));
        goto targcleanup;
      }
    }

    if(!lrealpath(filename, rpath)) {
      pm_printf(ALPM_LOG_ERROR, _("cannot determine real path for '%s': %s\n"),
          filename, strerror(errno));
      goto targcleanup;
    }

    if(strncmp(rpath, path, rootlen) != 0) {
      /* file is outside root, we know nothing can own it */
      pm_printf(ALPM_LOG_ERROR, _("No package owns %s\n"), filename);
      goto targcleanup;
    }

    rel_path = rpath + rootlen;

    if((is_dir = S_ISDIR(buf.st_mode))) {
      size_t rlen = strlen(rpath);
      if(rlen + 2 >= PATH_MAX) {
          pm_printf(ALPM_LOG_ERROR, _("path too long: %s/\n"), rpath);
      }
      strcat(rpath + rlen, "/");
    }

    for(i = packages; i && (!found || is_dir); i = alpm_list_next(i)) {
      if(alpm_filelist_contains(alpm_pkg_get_files(i->data), rel_path)) {
        print_query_fileowner(rpath, i->data);
        found = 1;
      }
    }
    if(!found) {
      pm_printf(ALPM_LOG_ERROR, _("No package owns %s\n"), filename);
    }

targcleanup:
    if(!found) {
      ret++;
    }
    free(filename);
  }

  return ret;
}

/* search the local database for a matching package */
static int query_search(alpm_list_t *targets)
{
  alpm_db_t *db_local = alpm_get_localdb(config->handle);
  return dump_pkg_search(db_local, targets, 0);
}

static int query_group(alpm_list_t *targets)
{
  alpm_list_t *i, *j;
  const char *grpname = NULL;
  int ret = 0;
  alpm_db_t *db_local = alpm_get_localdb(config->handle);

  if(targets == NULL) {
    for(j = alpm_db_get_groupcache(db_local); j; j = alpm_list_next(j)) {
      alpm_group_t *grp = j->data;
      const alpm_list_t *p;

      for(p = grp->packages; p; p = alpm_list_next(p)) {
        alpm_pkg_t *pkg = p->data;
        printf("%s %s\n", grp->name, alpm_pkg_get_name(pkg));
      }
    }
  } else {
    for(i = targets; i; i = alpm_list_next(i)) {
      alpm_group_t *grp;
      grpname = i->data;
      grp = alpm_db_get_group(db_local, grpname);
      if(grp) {
        const alpm_list_t *p;
        for(p = grp->packages; p; p = alpm_list_next(p)) {
          if(!config->quiet) {
            printf("%s %s\n", grpname,
                alpm_pkg_get_name(p->data));
          } else {
            printf("%s\n", alpm_pkg_get_name(p->data));
          }
        }
      } else {
        pm_printf(ALPM_LOG_ERROR, _("group '%s' was not found\n"), grpname);
        ret++;
      }
    }
  }
  return ret;
}

static unsigned short pkg_get_locality(alpm_pkg_t *pkg)
{
  const char *pkgname = alpm_pkg_get_name(pkg);
  alpm_list_t *j;
  alpm_list_t *sync_dbs = alpm_get_syncdbs(config->handle);

  for(j = sync_dbs; j; j = alpm_list_next(j)) {
    if(alpm_db_get_pkg(j->data, pkgname)) {
      return PKG_LOCALITY_LOCAL;
    }
  }
  return PKG_LOCALITY_FOREIGN;
}

static int is_unrequired(alpm_pkg_t *pkg)
{
  alpm_list_t *requiredby = alpm_pkg_compute_requiredby(pkg);
  if(requiredby == NULL) {
    return 1;
  }
  FREELIST(requiredby);
  return 0;
}

static int filter(alpm_pkg_t *pkg)
{
  /* check if this package was explicitly installed */
  if(config->op_q_explicit &&
      alpm_pkg_get_reason(pkg) != ALPM_PKG_REASON_EXPLICIT) {
    return 0;
  }
  /* check if this package was installed as a dependency */
  if(config->op_q_deps &&
      alpm_pkg_get_reason(pkg) != ALPM_PKG_REASON_DEPEND) {
    return 0;
  }
  /* check if this pkg is or isn't in a sync DB */
  if(config->op_q_locality && config->op_q_locality & pkg_get_locality(pkg)) {
    return 0;
  }
  /* check if this pkg is unrequired */
  if(config->op_q_unrequired && !is_unrequired(pkg)) {
    return 0;
  }
  /* check if this pkg is outdated */
  if(config->op_q_upgrade && (alpm_sync_newversion(pkg,
          alpm_get_syncdbs(config->handle)) == NULL)) {
    return 0;
  }
  return 1;
}

static int display(alpm_pkg_t *pkg)
{
  int ret = 0;

  if(config->op_q_info) {
    if(config->op_q_isfile) {
      dump_pkg_full(pkg, 0);
    } else {
      dump_pkg_full(pkg, config->op_q_info > 1);
    }
  }
  if(config->op_q_list) {
    dump_pkg_files(pkg, config->quiet);
  }
  if(config->op_q_changelog) {
    dump_pkg_changelog(pkg);
  }
  if(config->op_q_check) {
    if(config->op_q_check == 1) {
      ret = check_pkg_fast(pkg);
    } else {
      ret = check_pkg_full(pkg);
    }
  }
  if(!config->op_q_info && !config->op_q_list
      && !config->op_q_changelog && !config->op_q_check) {
    if(!config->quiet) {
      const colstr_t *colstr = &config->colstr;
      printf("%s%s %s%s%s\n", colstr->title, alpm_pkg_get_name(pkg),
          colstr->version, alpm_pkg_get_version(pkg), colstr->nocolor);
    } else {
      printf("%s\n", alpm_pkg_get_name(pkg));
    }
  }
  return ret;
}

int pacman_query(alpm_list_t *targets)
{
  int ret = 0;
  int match = 0;
  alpm_list_t *i;
  alpm_pkg_t *pkg = NULL;
  alpm_db_t *db_local;

  /* First: operations that do not require targets */

  /* search for a package */
  if(config->op_q_search) {
    ret = query_search(targets);
    return ret;
  }

  /* looking for groups */
  if(config->group) {
    ret = query_group(targets);
    return ret;
  }

  if(config->op_q_locality || config->op_q_upgrade) {
    if(check_syncdbs(11)) {
      return 1;
    }
  }

  db_local = alpm_get_localdb(config->handle);

  /* operations on all packages in the local DB
   * valid: no-op (plain -Q), list, info, check
   * invalid: isfile, owns */
  if(targets == NULL) {
    if(config->op_q_isfile || config->op_q_owns) {
      pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
      return 1;
    }

    for(i = alpm_db_get_pkgcache(db_local); i; i = alpm_list_next(i)) {
      pkg = i->data;
      if(filter(pkg)) {
        int value = display(pkg);
        if(value != 0) {
          ret = 1;
        }
        match = 1;
      }
    }
    if(!match) {
      ret = 1;
    }
    return ret;
  }

  /* Second: operations that require target(s) */

  /* determine the owner of a file */
  if(config->op_q_owns) {
    ret = query_fileowner(targets);
    return ret;
  }

  /* operations on named packages in the local DB
   * valid: no-op (plain -Q), list, info, check */
  for(i = targets; i; i = alpm_list_next(i)) {
    const char *strname = i->data;

    /* strip leading part of "local/pkgname" */
    if(strncmp(strname, LOCAL_PREFIX, strlen(LOCAL_PREFIX)) == 0) {
      strname += strlen(LOCAL_PREFIX);
    }

    if(config->op_q_isfile) {
      alpm_pkg_load(config->handle, strname, 10, &pkg);
    } else {
      pkg = alpm_db_get_pkg(db_local, strname);
    }

    if(pkg == NULL) {
      switch(alpm_errno(config->handle)) {
        case ALPM_ERR_PKG_NOT_FOUND:
          pm_printf(ALPM_LOG_ERROR,
              _("package '%s' was not found\n"), strname);
          if(!config->op_q_isfile && access(strname, R_OK) == 0) {
            pm_printf(ALPM_LOG_WARNING,
                _("'%s' is a file, you might want to use %s.\n"),
                strname, "-p/--file");
          }
          break;
        default:
          pm_printf(ALPM_LOG_ERROR,
              _("could not load package '%s': %s\n"), strname,
              alpm_strerror(alpm_errno(config->handle)));
          break;
      }
      ret = 1;
      continue;
    }

    if(filter(pkg)) {
      int value = display(pkg);
      if(value != 0) {
        ret = 1;
      }
      match = 1;
    }

    if(config->op_q_isfile) {
      alpm_pkg_free(pkg);
      pkg = NULL;
    }
  }

  if(!match) {
    ret = 1;
  }

  return ret;
}

/* vim: set ts=2 sw=2 noet: */