aboutsummaryrefslogtreecommitdiffstats
path: root/src/repo/s6rc_repo_listcontents.c
blob: e3ffc845cfd4ffd94fab3601002caf4c12f1b2ba (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
/* ISC license. */

#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <skalibs/posixplz.h>
#include <skalibs/types.h>
#include <skalibs/strerr.h>
#include <skalibs/stralloc.h>
#include <skalibs/genalloc.h>
#include <skalibs/cspawn.h>
#include <skalibs/djbunix.h>

#include <s6-rc/config.h>
#include <s6-rc/repo.h>

int s6rc_repo_listcontents (char const *repo, char const *bundle, stralloc *storage, genalloc *indices)
{
  int swasnull = !storage->s ;
  int gwasnull = !indices->s ;
  size_t sabase = storage->len ;
  size_t gabase = genalloc_len(size_t, indices) ;
  size_t repolen = strlen(repo) ;
  pid_t pid ;
  int fd ;
  int wstat ;
  char refdb[repolen + 15] ;
  char const *argv[8] = { S6RC_BINPREFIX "s6-rc-db", "-c", refdb, "-b", "--", "contents", bundle, 0 } ;

  memcpy(refdb, repo, repolen) ;
  memcpy(refdb + repolen, "/compiled/.ref", 15) ;
  pid = child_spawn1_pipe(argv[0], argv, (char const *const *)environ, &fd, 1) ;
  if (!pid) { strerr_warnfu2sys("spawn ", argv[0]) ; return -1 ; }
  if (!slurpn(fd, storage, 0)) { strerr_warnfu2sys("read output from ", argv[0]) ; return -1 ; }
  fd_close(fd) ;
  if (wait_pid(pid, &wstat) == -1)
  {
    strerr_warnfu2sys("wait for ", argv[0]) ;
    return -1 ;
  }
  if (WIFSIGNALED(wstat))
  {
    char fmt[UINT_FMT] ;
    fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ;
     strerr_warnf3x(argv[0], " crashed with signal ", fmt) ;
    return -1 ;
  }
  if (WEXITSTATUS(wstat))
  {
    char fmt[UINT_FMT] ;
    fmt[uint_fmt(fmt, WEXITSTATUS(wstat))] = 0 ;
    strerr_warnf3x(argv[0], " exited with code ", fmt) ;
    return (WEXITSTATUS(wstat) < 99) - 1 ;
  }

 if (!s6rc_repo_nlto0(storage->s + sabase, sabase, storage->len, indices)) goto err ;
 return 1 ;

 err:
  if (gwasnull) genalloc_free(size_t, indices) ;
  else genalloc_setlen(size_t, indices, gabase) ;
  if (swasnull) stralloc_free(storage) ;
  else storage->len = sabase ;
  return -1 ;
}