aboutsummaryrefslogtreecommitdiffstats
path: root/src/repo/s6-rc-set-copy.c
blob: 13e14a5324d0473ffd82ade88a25084182831125 (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
/* ISC license. */

#include <string.h>
#include <unistd.h>

#include <skalibs/uint64.h>
#include <skalibs/envexec.h>
#include <skalibs/djbunix.h>

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

#define USAGE "s6-rc-set-copy [ -v verbosity ] [ -r repo ] [ -f ] srcset dstset"
#define dieusage() strerr_dieusage(100, USAGE)

enum golb_e
{
  GOLB_FORCE = 0x01
} ;

enum gola_e
{
  GOLA_VERBOSITY,
  GOLA_REPODIR,
  GOLA_N
} ;

static gol_bool const rgolb[] =
{
  { .so = 'f', .lo = "force", .clear = 0, .set = GOLB_FORCE }
} ;

static gol_arg const rgola[] =
{
  { .so = 'v', .lo = "verbosity", .i = GOLA_VERBOSITY },
  { .so = 'r', .lo = "repodir", .i = GOLA_REPODIR }
} ;

static inline void docopy (char const *repo, char const *srcname, char const *dstname, uint64_t flags)
{
  int e = s6rc_repo_setcopy(repo, srcname, dstname, !!(flags & GOLB_FORCE)) ;
  switch (e)
  {
    case -2 : strerr_dief(102, "internal layout error: symlink inside ", srcname, " doesn't point to a valid name") ;
    case -1 : strerr_dief(1, "set ", dstname, " already exists in repository ", repo) ;
    case 0 : break ;
    default : strerr_diefusys(111, "copy set ", srcname, " to set ", dstname) ;
  }
}

int main (int argc, char const *const *argv)
{
  int fdlock ;
  unsigned int verbosity = 1 ;
  uint64_t wgolb = 0 ;
  char const *wgola[GOLA_N] = { 0 } ;
  unsigned int golc ;

  PROG = "s6-rc-set-copy" ;
  wgola[GOLA_REPODIR] = S6RC_REPODIR ;

  golc = GOL_main(argc, argv, rgolb, rgola, &wgolb, wgola) ;
  argc -= golc ; argv += golc ;
  if (wgola[GOLA_VERBOSITY] && !uint0_scan(wgola[GOLA_VERBOSITY], &verbosity))
    strerr_dief1x(100, "verbosity needs to be an unsigned integer") ;
  if (argc < 2) dieusage() ;
  if (!strcmp(argv[0], argv[1]))
    strerr_dief1x(100, "source and destination sets are the same") ;
  s6rc_repo_sanitize_setname(argv[0]) ;
  s6rc_repo_sanitize_setname(argv[1]) ;

  tain_now_g() ;
  fdlock = s6rc_repo_lock(wgola[GOLA_REPODIR], 1) ;
  if (fdlock == -1) strerr_diefu2sys(111, "lock ", wgola[GOLA_REPODIR]) ;
  docopy(wgola[GOLA_REPODIR], argv[0], argv[1], wgolb) ;
  _exit(0) ;
}