summaryrefslogtreecommitdiff
path: root/src/install.c
blob: e7d1a82037283bec7539a6f10221273ae81ef024 (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
#include <unistd.h>
#include <sys/stat.h>
#include "buffer.h"
#include "logmsg.h"
#include "open.h"
#include "exit.h"
#include "fifo.h"

extern void hier();

#define WHO "install"

int fdsourcedir = -1;

void h(char *home,char *queue,int uid,int gid,int mode)
{
  if (mkdir(home,0700) == -1)
    if (errno != EEXIST)
      logmsg(WHO,111,FATAL,B("unable to mkdir: ",home));
  if (chown(home,uid,gid) == -1)
    logmsg(WHO,111,FATAL,B("unable to chown: ",home));
  if (chmod(home,mode) == -1)
    logmsg(WHO,111,FATAL,B("unable to chmod: ",home));

  if (mkdir(queue,0700) == -1) {
    if (errno != EEXIST)
      logmsg(WHO,111,FATAL,B("unable to mkdir: ",queue));
  } else {
    if (chown(queue,uid,gid) == -1)
      logmsg(WHO,111,FATAL,B("unable to chown: ",queue));
    if (chmod(queue,mode) == -1)
      logmsg(WHO,111,FATAL,B("unable to chmod: ",queue));
  }
}

void d(char *home,char *subdir,int uid,int gid,int mode)
{
  if (chdir(home) == -1)
    logmsg(WHO,110,FATAL,B("unable to switch to: ",home));
  if (mkdir(subdir,0700) == -1)
    if (errno != EEXIST)
      logmsg(WHO,111,FATAL,B("unable to mkdir: ",home,"/",subdir));
  if (chown(subdir,uid,gid) == -1)
    logmsg(WHO,111,FATAL,B("unable to chown: ",home,"/",subdir));
  if (chmod(subdir,mode) == -1)
    logmsg(WHO,111,FATAL,B("unable to chmod: ",home,"/",subdir));
}

void p(char *queue,char *fifo,int uid,int gid,int mode)
{
  if (chdir(queue) == -1)
    logmsg(WHO,110,FATAL,B("unable to switch to: ",queue,"/queue"));
  if (fifo_make(fifo,0700) == -1)
    if (errno != EEXIST)
      logmsg(WHO,111,FATAL,B("unable to mkfifo: ",queue,"/queue/",fifo));
  if (chown(fifo,uid,gid) == -1)
    logmsg(WHO,111,FATAL,B("unable to chown: ",queue,"/queue/",fifo));
  if (chmod(fifo,mode) == -1)
    logmsg(WHO,111,FATAL,B("unable to chmod: ",queue,"/queue/",fifo));
}

char inbuf[BUFFER_INSIZE];
buffer bi;
char outbuf[BUFFER_OUTSIZE];
buffer bo;

void c(char *home,char *subdir,char *file,int uid,int gid,int mode)
{
  int fdin;
  int fdout;

  if (fchdir(fdsourcedir) == -1)
    logmsg(WHO,110,FATAL,"unable to switch back to source directory: ");

  fdin = open_read(file);
  if (fdin == -1)
    logmsg(WHO,111,FATAL,B("unable to read: ",file));
  buffer_init(&bi,read,fdin,inbuf,sizeof(inbuf));

  if (chdir(home) == -1)
    logmsg(WHO,110,FATAL,B("unable to switch to: ",home));
  if (chdir(subdir) == -1)
    logmsg(WHO,110,FATAL,B("unable to switch to: ",home,"/",subdir));

  fdout = open_trunc(file);
  if (fdout == -1)
    logmsg(WHO,111,FATAL,B("unable to write .../",subdir,"/",file));
  buffer_init(&bo,write,fdout,outbuf,sizeof(outbuf));

  switch (buffer_copy(&bo,&bi)) {
    case -2:
      logmsg(WHO,111,FATAL,B("unable to read: ",file));
    case -3:
      logmsg(WHO,111,FATAL,B("unable to write .../",subdir,"/",file));
  }

  close(fdin);
  if (buffer_flush(&bo) == -1)
    logmsg(WHO,111,FATAL,B("unable to write .../",subdir,"/",file));
  if (fsync(fdout) == -1)
    logmsg(WHO,111,FATAL,B("unable to write .../",subdir,"/",file));
  if (close(fdout) == -1) /* NFS silliness */
    logmsg(WHO,111,FATAL,B("unable to write .../",subdir,"/",file));

  if (chown(file,uid,gid) == -1)
    logmsg(WHO,111,FATAL,B("unable to chown .../",subdir,"/",file));
  if (chmod(file,mode) == -1)
    logmsg(WHO,111,FATAL,B("unable to chmod .../",subdir,"/",file));
}

void z(char *home,char *file,int len,int uid,int gid,int mode)
{
  int fdout;

  if (chdir(home) == -1)
    logmsg(WHO,110,FATAL,B("unable to switch to: ",home));

  fdout = open_trunc(file);
  if (fdout == -1)
    logmsg(WHO,111,FATAL,B("unable to write: ",home,"/",file));
  buffer_init(&bo,write,fdout,outbuf,sizeof(outbuf));

  while (len-- > 0)
    if (buffer_put(&bo,"",1) == -1)
      logmsg(WHO,111,FATAL,B("unable to write: ",home,"/",file));

  if (buffer_flush(&bo) == -1)
    logmsg(WHO,111,FATAL,B("unable to write: ",home,"/",file));
  if (fsync(fdout) == -1)
    logmsg(WHO,111,FATAL,B("unable to write: ",home,"/",file));
  if (close(fdout) == -1) /* NFS silliness */
    logmsg(WHO,111,FATAL,B("unable to write: ",home,"/",file));

  if (chown(file,uid,gid) == -1)
    logmsg(WHO,111,FATAL,B("unable to chown: ",home,"/",file));
  if (chmod(file,mode) == -1)
    logmsg(WHO,111,FATAL,B("unable to chmod: ",home,"/",file));
}

int main()
{
  fdsourcedir = open_read(".");
  if (fdsourcedir == -1)
    logmsg(WHO,110,FATAL,"unable to open current directory: ");

  umask(077);
  hier();
  _exit(0);
}