aboutsummaryrefslogtreecommitdiff
path: root/src/process.c
blob: 23641cd566b70fa68fd565aa3ec6a6addb429cd0 (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
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2009, 2010 Sergey Poznyakoff

   Wydawca 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 3 of the License, or (at your
   option) any later version.

   Wydawca 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 wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"

struct spool_list
{
  struct spool_list *next;
  struct spool spool;
};

static struct spool_list *spool_list;

void
register_spool (struct spool *spool)
{
  struct spool_list *sp = xmalloc (sizeof *sp);
  sp->spool = *spool;
  sp->next = spool_list;
  spool_list = sp;
}

static int
spool_check_alias (struct spool *spool, const char *name)
{
  if (spool->aliases && gl_list_search (spool->aliases, name))
    return 1;
  return 0;
}

struct spool *
wydawca_find_spool (const char *name)
{
  struct spool_list *sp;

  for (sp = spool_list; sp; sp = sp->next)
    {
      if (strcmp (sp->spool.tag, name) == 0
	  || spool_check_alias (&sp->spool, name))
	return &sp->spool;
    }
  return NULL;
}

/* Return true if NAME is a directory. If stat fails, return the error
   code in EC */
int
test_dir (const char *name, int *ec)
{
  struct stat st;

  *ec = 0;
  if (stat (name, &st))
    {
      *ec = errno;
      return 1;
    }
  return S_ISDIR (st.st_mode) == 0;
}

/* Return a textual representation of a file TYPE */
const char *
file_type_str (enum file_type type)
{
  switch (type)
    {
    case file_dist:
      return "distributive";

    case file_signature:
      return "detached signature";

    case file_directive:
      return "signed upload directive";
    }
  return "UNKNOWN";
}

/* Parse file NAME: determine its type and root name and store this
   information in FINFO */
void
parse_file_name (const char *name, struct file_info *finfo)
{
  static struct suffix
  {
    const char *suf;
    unsigned len;
    enum file_type type;
  } suftab[] = {
    { SUF_SIG, SUF_SIG_LEN, file_signature },
    { SUF_DIR, SUF_DIR_LEN, file_directive },
    { "", 0, file_dist }
  };
  int i;
  unsigned len = strlen (name);

  for (i = 0; i < sizeof suftab / sizeof suftab[0]; i++)
    {
      if (len >= suftab[i].len
	  && memcmp (name + len - suftab[i].len,
		     suftab[i].suf, suftab[i].len) == 0)
	{
	  finfo->name = xstrdup (name);
	  finfo->type = suftab[i].type;
	  finfo->root_len = len - suftab[i].len;
	  return;
	}
    }
  abort (); /* should not happen */
}

int
match_uid_p (uid_t uid, int uc, uid_t *uv)
{
  int i;
  if (!uv)
    return 1;
  for (i = 0; i < uc; i++)
    if (uv[i] == uid)
      return 1;
  return 0;
}

/* Scan upload directory from the DPAIR and register all files found
   there, forming triplets when possible */
void
scan_spool_unlocked (const struct spool *spool, int uc, uid_t *uv)
{
  DIR *dir;
  struct dirent *ent;

  if (debug_level)
    logmsg (LOG_DEBUG, "%s -> %s", spool->source_dir,
	    mu_url_to_string (spool->dest_url));

  if (chdir (spool->source_dir))
    {
      logmsg (LOG_ERR, _("cannot chdir to %s: %s"), spool->source_dir,
	      strerror (errno));
      return;
    }

  dir = opendir (".");
  if (!dir)
    {
      logmsg (LOG_ERR, _("cannot open directory %s: %s"), spool->source_dir,
	      strerror (errno));
      return;
    }

  timer_start ("spool");
  /* FIXME: prefix spool tag with something */
  timer_start (spool->tag);
  while ((ent = readdir (dir)))
    {
      struct stat st;
      struct file_info finfo;

      if (strcmp (ent->d_name, ".") == 0 || strcmp (ent->d_name, "..") == 0)
	continue;

      if (stat (ent->d_name, &st))
	{
	  logmsg (LOG_ERR, _("cannot stat file %s/%s: %s"),
		  spool->source_dir, ent->d_name,
		  strerror (errno));
	  continue;
	}

      if (!S_ISREG (st.st_mode))
	{
	  logmsg (LOG_NOTICE, _("not a regular file: %s/%s"),
		  spool->source_dir, ent->d_name);
	  continue;
	}

      if (!match_uid_p (st.st_uid, uc, uv))
	{
	  if (debug_level)
	    logmsg (LOG_DEBUG, _("ignoring file: %s/%s"),
		    spool->source_dir, ent->d_name);
	  continue;
	}
      
      finfo.sb = st;
      parse_file_name (ent->d_name, &finfo);

      if (debug_level)
	logmsg (LOG_DEBUG, _("found file %s: %s, stem: %.*s"), ent->d_name,
		  file_type_str (finfo.type), finfo.root_len, finfo.name);

      register_file (&finfo);
    }

  closedir (dir);

  if (count_collected_triplets () > 0)
    {
      int i;

      for (i = 0; i < dictionary_count; i++)
        {
          if (dictionary_init (spool->dictionary[i]))
            {
              logmsg (LOG_ERR, _("failed to initialize dictionary %d"), i);
              return;
            }
        }
      enumerate_triplets (spool);
   }
  timer_stop (spool->tag);
  timer_stop ("spool");
}

int
scan_spool (const struct spool *spool, int uc, uid_t *uv)
{
  char *lockfile = wydawca_lockname (spool->tag);
  int rc = wydawca_lock (lockfile);
  switch (rc)
    {
    case LOCK_OK:
      scan_spool_unlocked (spool, uc, uv);
      wydawca_unlock (lockfile);
      break;

    case LOCK_FAILURE:
      logmsg (LOG_ERR, _("cannot lock spool %s"), spool->tag);
      break;

    case LOCK_RETRY:
      logmsg (LOG_WARNING, _("timed out while looking spool %s"), spool->tag);
      break;
    }
  free (lockfile);
  return rc;
}

static void
close_dictionaries (struct spool *spool)
{
  int i;
  for (i = 0; i < NITEMS (spool->dictionary); i++)
    dictionary_done (spool->dictionary[i]);
}

/* Scan all configured update directories */
int
scan_all_spools (int uidc, uid_t *uidv)
{
  struct spool_list *sp;
  int rc = 0;
  
  timer_start ("wydawca");
  for (sp = spool_list; sp; sp = sp->next)
    if (enabled_spool_p (&sp->spool))
      if (scan_spool (&sp->spool, uidc, uidv))
	rc++;

  for (sp = spool_list; sp; sp = sp->next)
    close_dictionaries (&sp->spool);
  timer_stop ("wydawca");
  return rc;
}

void
spool_create_timers ()
{
  struct spool_list *sp;

  for (sp = spool_list; sp; sp = sp->next)
    timer_start (sp->spool.tag);
}

Return to:

Send suggestions and report system problems to the System administrator.