summaryrefslogtreecommitdiffabout
path: root/src/triplet.c
blob: 6bd9e6a03c7e4dcbf514c61105a056f011e046da (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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2008, 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"
#include <time.h>

static struct grecs_symtab *triplet_table;

static unsigned
hash_triplet_hasher (void *data, unsigned long n_buckets)
{
  struct file_triplet const *t = data;
  return grecs_hash_string (t->name, n_buckets);
}

/* Compare two strings for equality.  */
static int
hash_triplet_compare (void const *data1, void const *data2)
{
  struct file_triplet const *t1 = data1;
  struct file_triplet const *t2 = data2;
  return strcmp (t1->name, t2->name);
}

/* Reclaim memory storage associated with a table entry */
void
hash_triplet_free (void *data)
{
  int i;
  struct file_triplet *tp = data;
  struct uploader_info *up;
  
  free (tp->name);
  
  for (i = 0; i < FILE_TYPE_COUNT; i++)
    {
      if (tp->file[i].name)
	free (tp->file[i].name);
    }

  free (tp->directive);
  free (tp->blurb);
  free (tp->tmp);
  obstack_free (&tp->obstk, NULL);

  /* Free uploader list */
  for (up = tp->uploader_list; up; )
    {
      struct uploader_info *next = up->next;
      free (up);
      up = next;
    }
  
  free (tp);
}

char *
triplet_strdup (struct file_triplet *tp, const char *str)
{
  size_t len = strlen (str);
  obstack_grow (&tp->obstk, str, len + 1);
  return obstack_finish (&tp->obstk);
}

/* Register a file in the triplet table */
void
register_file (struct file_info *finfo, const struct spool *spool)
{
  struct file_triplet key, *ret;
  int install = 1;

  if (!triplet_table)
    {
      triplet_table = grecs_symtab_create (sizeof (struct file_triplet),
					   hash_triplet_hasher,
					   hash_triplet_compare,
					   NULL,
					   NULL,
					   hash_triplet_free);
      if (!triplet_table)
	grecs_alloc_die ();
    }

  key.name = xmalloc (finfo->root_len + 1);
  memcpy (key.name, finfo->name, finfo->root_len);
  key.name[finfo->root_len] = 0;

  ret = grecs_symtab_lookup_or_install (triplet_table, &key, &install);
  if (!ret)
    grecs_alloc_die ();
  free (key.name);
  if (install)
    {
      ret->spool = spool;
      obstack_init (&ret->obstk);
    }
  ret->file[finfo->type] = *finfo;
}

/* Return true if any part of the triplet TRP was modified more than
   TTL seconds ago */
static int
triplet_expired_p (struct file_triplet *trp, time_t ttl)
{
  int i;
  time_t now = time (NULL);

  if (ttl == 0)
    return 0;

  for (i = 0; i < FILE_TYPE_COUNT; i++)
    {
      if (trp->file[i].name
	  && (now - trp->file[i].sb.st_mtime) > ttl)
	{
	  if (debug_level)
	    logmsg (LOG_DEBUG, _("file %s expired"), trp->file[i].name);
	  return 1;
	}
    }
  return 0;
}

enum triplet_state
  {
    triplet_directive,      /* Short triplet: only a directive is present,
			       but nothing more is required */
    triplet_complete,       /* A complete triplet: all three files are present
			       and have the same owner */
    triplet_incomplete,     /* Incomplete triplet: some files are missing */
    triplet_bad,            /* Bad triplet. Should be removed immediately. */
  };


static enum triplet_state
check_triplet_state (struct file_triplet *trp)
{
  if (trp->file[file_directive].name)
    {
      if (verify_directive_file (trp))
	return triplet_bad;

      if (trp->file[file_dist].name == 0
	  && trp->file[file_signature].name == 0)
	{
	  if (directive_get_value (trp, "filename", NULL))
	    return triplet_directive;
	}
      else if (trp->file[file_dist].name
	       && trp->file[file_signature].name)
	{
	  if (trp->file[file_dist].sb.st_uid ==
	                trp->file[file_signature].sb.st_uid
	      && trp->file[file_dist].sb.st_uid ==
	                trp->file[file_directive].sb.st_uid)
	    return triplet_complete;
	  else
	    {
	      if (debug_level)
		logmsg (LOG_DEBUG, _("%s: invalid triplet: UIDs differ"),
			trp->name);
	      return triplet_bad;
	    }
	}
    }

  return triplet_incomplete;
}

/* Unlink all parts of the triplet TRP */
static void
remove_triplet (struct file_triplet *trp)
{
  int i;

  for (i = 0; i < FILE_TYPE_COUNT; i++)
    {
      if (trp->file[i].name)
	{
	  logmsg (LOG_NOTICE, _("removing %s"), trp->file[i].name);
	  if (!dry_run_mode && unlink (trp->file[i].name))
	    logmsg (LOG_ERR, _("cannot remove %s: %s"),
		    trp->file[i].name, strerror (errno));
	}
    }
}

/* Process a single triplet from the table */
static int
triplet_processor (void *data, void *proc_data)
{
  struct file_triplet *trp = data;

  if (debug_level)
    logmsg (LOG_DEBUG, "FILE %s, DIST=%s, SIG=%s, DIRECTIVE=%s",
	    trp->name,
	    SP (trp->file[file_dist].name),
	    SP (trp->file[file_signature].name),
	    SP (trp->file[file_directive].name));

  switch (check_triplet_state (trp))
    {
    case triplet_directive:
    case triplet_complete:
      if (debug_level)
	logmsg (LOG_DEBUG, _("processing triplet `%s'"), trp->name);
      if (process_directives (trp))
	remove_triplet (trp);
      return 0;

    case triplet_incomplete:
      if (debug_level)
	logmsg (LOG_DEBUG, _("%s: incomplete triplet"), trp->name);
      /* ignore unless expired (see below); */
      UPDATE_STATS (STAT_INCOMPLETE_TRIPLETS);
      break;

    case triplet_bad:
      UPDATE_STATS (STAT_BAD_TRIPLETS);
      remove_triplet (trp);
      return 0;
    }

  if (triplet_expired_p (trp, trp->spool->file_sweep_time))
    {
      UPDATE_STATS (STAT_EXPIRED_TRIPLETS);
      remove_triplet (trp);
    }

  return 0;
}

/* Process all triplets from the table according to the SPOOL */
void
enumerate_triplets (const struct spool *spool)
{
  if (debug_level)
    logmsg (LOG_DEBUG, _("processing spool %s (%s)"),
	    spool->tag, mu_url_to_string (spool->dest_url));
  if (triplet_table)
    {
      grecs_symtab_enumerate (triplet_table, triplet_processor, NULL);
      grecs_symtab_clear (triplet_table);
    }
}

size_t
count_collected_triplets ()
{
  return triplet_table ? grecs_symtab_count_entries (triplet_table) : 0;
}


static const char *
expand_project_base (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  return trp->project;
}

static const char *
expand_tag (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  return trp->spool->tag;
}

static const char *
expand_url (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  return trp->spool->url;
}

static const char *
expand_relative_dir (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  directive_get_value (trp, "directory", (const char**) &def->value);
  return def->value;
}

static const char *
expand_dest_dir (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  return trp->spool->dest_dir;
}

static const char *
expand_source_dir (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  return trp->spool->source_dir;
}

static void
decode_file_mode (mode_t mode, char *string)
{
  *string++ = mode & S_IRUSR ? 'r' : '-';
  *string++ = mode & S_IWUSR ? 'w' : '-';
  *string++ = (mode & S_ISUID
	       ? (mode & S_IXUSR ? 's' : 'S')
	       : (mode & S_IXUSR ? 'x' : '-'));
  *string++ = mode & S_IRGRP ? 'r' : '-';
  *string++ = mode & S_IWGRP ? 'w' : '-';
  *string++ = (mode & S_ISGID
	       ? (mode & S_IXGRP ? 's' : 'S')
	       : (mode & S_IXGRP ? 'x' : '-'));
  *string++ = mode & S_IROTH ? 'r' : '-';
  *string++ = mode & S_IWOTH ? 'w' : '-';
  *string++ = (mode & S_ISVTX
	       ? (mode & S_IXOTH ? 't' : 'T')
	       : (mode & S_IXOTH ? 'x' : '-'));
  *string = '\0';
}

/* Width of "user/group size", with initial value chosen
   heuristically.  This grows as needed, though this may cause some
   stairstepping in the output.  Make it too small and the output will
   almost always look ragged.  Make it too large and the output will
   be spaced out too far.  */
static int ugswidth = 19;

static int
format_file_data (struct file_triplet *trp, enum file_type type, char **pret)
{
  char modes[11];
  struct file_info *info = trp->file + type;
  char timebuf[sizeof "YYYY-MM-DD HH:MM:SS +0000"];
  struct passwd *pw;
  struct group *grp;
  char sbuf[INT_BUFSIZE_BOUND (uintmax_t)];
  char *sptr;
  size_t slen;
  int pad;
  char *user_name;
  char *group_name;
  struct tm *tm;
  char *buf;
  
  if (!info->name)
    return 1;

  /* MODE OWNER GROUP SIZE MTIME FILE_NAME MD5SUM? */

  modes[0] = '-'; /* Only regular files are allowed */
  decode_file_mode (info->sb.st_mode, modes + 1);

  /* File time */
  tm = localtime (&info->sb.st_mtime);
  strftime (timebuf, sizeof timebuf, "%Y-%m-%d %H:%M:%S %z", tm);

  pw = getpwuid (TRIPLET_UID (trp));
  if (!pw)
	  user_name = "unknown";
  else
	  user_name = pw->pw_name;

  grp = getgrgid (TRIPLET_GID (trp));
  if (!grp)
    group_name = "unknown"; /* should not happen */
  else
    group_name = grp->gr_name;

  /* Size */
  sptr = umaxtostr (info->sb.st_size, sbuf);

  /* Figure out padding and format the buffer */
  slen = strlen (sptr);
  pad = strlen (user_name) + 1 + strlen (group_name) + 1 + slen;
  if (pad > ugswidth)
    ugswidth = pad;

  asprintf (&buf,
	    "%s %s %s %*s %s %s",
	    modes, user_name, group_name, ugswidth - pad + slen, sptr,
	    timebuf, info->name);
  *pret = buf;
  return 0;
}

static const char *
expand_triplet_ls_full (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  char *buf[FILE_TYPE_COUNT] =  { NULL, NULL, NULL };
  size_t size = 0;

  if (format_file_data (trp, file_dist, &buf[file_dist]) == 0)
    size += strlen (buf[file_dist]) + 1;
  if (format_file_data (trp, file_signature, &buf[file_signature]) == 0)
    size += strlen (buf[file_signature]) + 1;
  if (format_file_data (trp, file_directive, &buf[file_directive]) == 0)
    size += strlen (buf[file_directive]) + 1;

  def->value = def->storage = xmalloc (size + 1);
  def->value[0] = 0;
  if (buf[file_dist])
    {
      strcat (def->value, buf[file_dist]);
      strcat (def->value, "\n");
    }
  if (buf[file_signature])
    {
      strcat (def->value, buf[file_signature]);
      strcat (def->value, "\n");
    }
  if (buf[file_directive])
    {
      strcat (def->value, buf[file_directive]);
      strcat (def->value, "\n");
    }
  free (buf[file_dist]);
  free (buf[file_signature]);
  free (buf[file_directive]);
  return def->value;
}

static const char *
expand_triplet_ls_upload (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  char *buf[2] = { NULL, NULL };
  size_t size = 0;

  if (format_file_data (trp, file_dist, &buf[file_dist]) == 0)
    size += strlen (buf[file_dist]) + 1;
  if (format_file_data (trp, file_signature, &buf[file_signature]) == 0)
    size += strlen (buf[file_signature]) + 1;

  def->value = def->storage = xmalloc (size + 1);
  def->value[0] = 0;
  if (buf[file_dist])
    {
      strcat (def->value, buf[file_dist]);
      strcat (def->value, "\n");
    }
  if (buf[file_signature])
    {
      strcat (def->value, buf[file_signature]);
      strcat (def->value, "\n");
    }

  free (buf[file_dist]);
  free (buf[file_signature]);

  return def->value;
}

static const char *
expand_triplet_dist (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  return trp->file[file_dist].name;
}

static const char *
expand_triplet_sig (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  return trp->file[file_signature].name;
}

static const char *
expand_triplet_dir (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  return trp->file[file_directive].name;
}

static const char *
expand_triplet_ls_dist (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  format_file_data (trp, file_dist, &def->storage);
  def->value = def->storage;
  return def->value;
}

static const char *
expand_triplet_ls_sig (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  format_file_data (trp, file_signature, &def->storage);
  def->value = def->storage;
  return def->value;
}

static const char *
expand_triplet_ls_directive (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  format_file_data (trp, file_directive, &def->storage);
  def->value = def->storage;
  return def->value;
}

static const char *
expand_user_name (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  if (trp->uploader)
    return trp->uploader->name;
  def->value = "UNKNOWN";
  return def->value;
}

static const char *
expand_user_real_name (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  if (trp->uploader)
    return trp->uploader->realname;
  def->value = "UNKNOWN";
  return def->value;
}

static const char *
expand_user_email (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  if (trp->uploader)
    return trp->uploader->email;
  def->value = "UNKNOWN";
  return def->value;
}

static const char *
expand_email_user (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  if (trp->uploader)
    {
      asprintf (&def->storage, "\"%s\" <%s>",
		trp->uploader->realname, trp->uploader->email);
      def->value = def->storage;
    }
  return def->value;
}

static const char *
expand_report (struct metadef *def, void *data)
{
  return report_string;
}

static const char *
expand_comment (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  if (directive_get_value (trp, "comment", (const char**) &def->value))
    def->value = "";
  return def->value;
}

static const char *
expand_check_diagn (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  return trp->check_diag;
}

static const char *
expand_check_result (struct metadef *def, void *data)
{
  struct file_triplet *trp = data;
  int status = trp->check_result;
  char sbuf[INT_BUFSIZE_BOUND (uintmax_t)];

  if (status == 0)
    return def->value = "0";
  else if (WIFEXITED (status))
    {
      char *p = umaxtostr (WEXITSTATUS (status), sbuf);
      def->storage = xstrdup (p);
    }
  else if (WIFSIGNALED (status))
    {
      char *p = umaxtostr (WTERMSIG (status), sbuf);
      asprintf (&def->storage, "SIG+%s", p);
    }
  else
    def->storage = "[unrecognized return code]";
  return def->value = def->storage;
}

#define DECL_EXPAND_TIMER(what)			                        \
static const char *					                \
__cat2__(expand_timer_,what) (struct metadef *def, void *data)          \
{                                                                       \
  wydawca_timer_t t = timer_stop ((char*)def->data);	                \
  def->storage = timer_format_time (__cat2__(timer_get_,what) (t));	\
  return def->value = def->storage;                                     \
}

DECL_EXPAND_TIMER(real)
DECL_EXPAND_TIMER(user)
DECL_EXPAND_TIMER(system)

#define DECL_TIMER(name,t)						\
  { "timer:" #name ":" #t, NULL, __cat2__(expand_timer_,t), NULL, #name }
#define DECL_FULL_TIMER(name)			\
  DECL_TIMER(name, real),			\
  DECL_TIMER(name, user),			\
  DECL_TIMER(name, system)  

struct metadef triplet_default_meta[] = {
  { "u",              NULL, expand_user_name, NULL },
  { "user",           NULL, expand_user_name, NULL },
  { "user:name",      NULL, expand_user_name, NULL },
  { "p",              NULL, expand_project_base, NULL },
  { "project",        NULL, expand_project_base, NULL },
  { "url",            NULL, expand_url, NULL },
  { "spool",          NULL, expand_tag, NULL },
  { "dir",            NULL, expand_relative_dir, NULL },
  { "dest-dir",       NULL, expand_dest_dir, NULL },
  { "source-dir",     NULL, expand_source_dir, NULL },
  { "comment",        NULL, expand_comment, NULL },
  { NULL }
};

char *
triplet_expand_dictionary_query (struct dictionary *dict, void *handle,
			         struct file_triplet *trp)
{
  char *p = meta_expand_string (dict->query, triplet_default_meta, trp,
				dict, handle);
  meta_free (triplet_default_meta);
  return p;
}

struct metadef triplet_meta[] = {
  { "project",           NULL, expand_project_base, NULL },
  { "url",               NULL, expand_url, NULL },
  { "spool",             NULL, expand_tag, NULL },
  { "dir",               NULL, expand_relative_dir, NULL },
  { "dest-dir",          NULL, expand_dest_dir, NULL },
  { "source-dir",        NULL, expand_source_dir, NULL },
  { "triplet:dist",      NULL, expand_triplet_dist, NULL },
  { "triplet:sig",       NULL, expand_triplet_sig, NULL },
  { "triplet:dir",       NULL, expand_triplet_dir, NULL },
  { "triplet:ls:full",   NULL, expand_triplet_ls_full, NULL },
  { "triplet:ls:upload", NULL, expand_triplet_ls_upload, NULL },
  { "triplet:ls:dist",   NULL, expand_triplet_ls_dist, NULL },
  { "triplet:ls:sig",    NULL, expand_triplet_ls_sig, NULL },
  { "triplet:ls:dir",    NULL, expand_triplet_ls_directive, NULL },
  { "check:result",      NULL, expand_check_result, NULL },
  { "check:diagn",       NULL, expand_check_diagn, NULL },
  { "user",              NULL, expand_user_name, NULL },
  { "user:name",         NULL, expand_user_name, NULL },
  { "user:real-name",    NULL, expand_user_real_name, NULL },
  { "user:email",        NULL, expand_user_email, NULL },
  { "email:user",        NULL, expand_email_user, NULL },
  { "email:admin",       NULL, expand_email_admin, NULL },
  { "email:owner",       NULL, expand_email_owner, NULL },
  { "report",            NULL, expand_report, NULL },
  { "comment",           NULL, expand_comment, NULL },
  DECL_FULL_TIMER(wydawca),
  DECL_FULL_TIMER(triplet),
  DECL_FULL_TIMER(spool),
  { NULL }
};

char *
triplet_expand_param (const char *tmpl, struct file_triplet *trp)
{
  char *p = meta_expand_string (tmpl, triplet_meta, trp, NULL, NULL);
  meta_free (triplet_meta);
  return p;
}

Return to:

Send suggestions and report system problems to the System administrator.