File: cmdutils.c

package info (click to toggle)
tla 1.3.5%2Bdfsg1-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 22,292 kB
  • ctags: 11,952
  • sloc: ansic: 149,771; sh: 16,009; xml: 2,689; lisp: 1,927; makefile: 1,064; cpp: 363; tcl: 230; awk: 48; sed: 25
file content (643 lines) | stat: -rw-r--r-- 17,239 bytes parent folder | download | duplicates (4)
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
/* cmdutils.c: 
 *
 ****************************************************************
 * Copyright (C) 2004 Tom Lord
 * 
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */


#include "hackerlab/bugs/panic.h"
#include "hackerlab/vu/safe.h"
#include "hackerlab/vu/vu.h"
#include "hackerlab/os/errno.h"
#include "hackerlab/os/errno-to-string.h"
#include "hackerlab/char/str.h"
#include "tla/libarch/cmdutils.h"
#include "tla/libarch/libraries.h"
#include "tla/libarch/local-cache.h"
#include "tla/libarch/project-tree.h"
#include "tla/libarch/patch-logs.h"
#include "tla/libarch/pfs.h"
#include "tla/libfsutils/dir-as-cwd.h"


/* __STDC__ prototypes for static functions */
static int category_exists (struct arch_archive *arch, t_uchar * package);
static int package_exists (struct arch_archive * arch, t_uchar * package);
static int version_exists (struct arch_archive * arch, t_uchar * version_spec);
static t_uchar * missing_string (enum arch_valid_package_name_types type);
static t_uchar * name_string (enum arch_valid_package_name_types type);
static enum arch_parse_package_name_type valid_to_parse (enum arch_valid_package_name_types type);



/* Gather a fully-qualified revision name from the current tree
 * and input (e.g. patch-10)
 */
t_uchar *
arch_fqrvsn_from_tree_and_input (t_uchar *cmd,
                                 t_uchar * string,
                                 t_uchar * dir)
{
  t_uchar * tree_root = 0;
  t_uchar * tree_version = 0;
  t_uchar * tree_arch = 0;
  t_uchar * ret = 0;
  
  if (arch_valid_package_name (string, arch_req_archive, arch_req_patch_level, 0))
    return str_save (0, string);

  tree_root = arch_tree_root (0, dir, 0);
  if (!tree_root)
    {
      safe_printfmt (2, "%s: not in a project tree\n  dir: %s\n", cmd, directory_as_cwd (dir));
      exit (2);
    }

  tree_version = arch_tree_version (tree_root);
  if (!tree_version)
    {
      safe_printfmt (2, "%s: no tree-version set\n  tree: %s\n", cmd, tree_root);
      exit (2);
    }

  tree_arch = arch_parse_package_name (arch_ret_archive, 0, tree_version);
  
  if (arch_valid_package_name (string, arch_maybe_archive, arch_req_patch_level, 0))
    {
      ret = arch_fully_qualify (tree_arch, string);
    }
  else if (arch_valid_package_name (string, arch_maybe_archive, arch_req_version, 0))
    {
      t_uchar * archive = arch_parse_package_name (arch_ret_archive, tree_arch, string);
      t_uchar * version = arch_parse_package_name (arch_ret_package_version, 0, string);
      t_uchar * patch_level = arch_highest_patch_level (tree_root, archive, version);
      t_uchar * revision = str_alloc_cat_many (0, version, "--", patch_level, str_end);

      ret = arch_fully_qualify (archive, revision);

      lim_free (0, archive);
      lim_free (0, version);
      lim_free (0, patch_level);
      lim_free (0, revision);
    }
  else 
    {
      ret = str_alloc_cat_many (0, tree_version, "--", string, str_end); 
      if (!arch_valid_package_name (ret, arch_req_archive, arch_req_patch_level, 0))
        {
          safe_printfmt (2, "%s: invalid revision or patch name (%s)\n", cmd, string);
          exit (2);
        }
    }
  lim_free (0, tree_root);
  lim_free (0, tree_version); 
  lim_free (0, tree_arch); 
  return ret;
}


static int
category_exists (struct arch_archive *arch, t_uchar * package)
{
  t_uchar * category = 0;
  rel_table categories = rel_table_nil;
  int x;
  
  category = arch_parse_package_name (arch_ret_category, 0, package);
  categories = arch_archive_categories (arch);
  for (x = 0; x < rel_n_records (categories); ++x)
    {
      if (!str_cmp (category, rel_peek_str (categories, x, 0)))
      {
        lim_free (0, category);
        rel_free_table (categories);
        return 1;
      }
    }
  return 0;
}


static int 
package_exists (struct arch_archive * arch, t_uchar * package)
{
  t_uchar * category = 0;
  t_uchar * branch = 0;
  rel_table categories = rel_table_nil;
  rel_table branches = rel_table_nil;
  int x;
  int found_it = 0;

  category = arch_parse_package_name (arch_ret_category, 0, package);
  branch = arch_parse_package_name (arch_ret_package, 0, package);

  categories = arch_archive_categories (arch);
  for (x = 0; x < rel_n_records (categories); ++x)
    {
      if (!str_cmp (category, rel_peek_str (categories, x, 0)))
        break;
    }

  if (x < rel_n_records (categories))
    {
      branches = arch_archive_branches (arch, category);

      for (x = 0; x < rel_n_records (branches); ++x)
        {
          if (!str_cmp (branch, rel_peek_str (branches, x, 0)))
            {
              found_it = 1;
              break;
            }
        }
    }

  lim_free (0, category);
  lim_free (0, branch);
  rel_free_table (categories);
  rel_free_table (branches);
  return found_it;
}

static int 
version_exists (struct arch_archive * arch, t_uchar * version_spec)
{
  t_uchar * branch = 0;
  t_uchar * version = 0;
  rel_table versions = rel_table_nil;
  int x;
  int found_it = 0;

  branch = arch_parse_package_name (arch_ret_package, 0, version_spec);
  version = arch_parse_package_name (arch_ret_package_version, 0, version_spec);
  versions = arch_archive_versions (arch, branch);

  for (x = 0; x < rel_n_records (versions); ++x)
    {
      if (!str_cmp (version, rel_peek_str (versions, x, 0)))
        {
          found_it = 1;
          break;
        }
    }
  lim_free (0, branch);
  lim_free (0, version);
  rel_free_table (versions);
  return found_it;
}

/* Checks whether the specified item (and all its components) exist
 */
void
arch_check_for (struct arch_archive * arch,
                enum arch_valid_package_name_types type,
                t_uchar * package)
{
  t_uchar * spec = arch_parse_package_name (arch_ret_non_archive, "", package);
  int missing = -1;
  /* note fall-through behavior */
  switch (type)
    {
      case arch_req_patch_level:
        if (arch_revision_exists (arch, spec))
          break;
        else
          missing = arch_req_patch_level;

      default:
        if (!category_exists (arch, spec))
          {
            missing=arch_req_category;
            break;
          }
        if (type == arch_req_category)
          break;

        if (!package_exists (arch, spec))
          {
            missing = arch_req_package;
            break;
          }
        if (type == arch_req_package)
          break;

        if (!version_exists (arch, spec))
          missing = arch_req_version;
    };
  if (missing != -1)
    {
      arch_print_missing (arch, missing, type, spec);
      exit (2);
    }
  lim_free (0, spec);
}

void
arch_print_missing (struct arch_archive * arch,
                    enum arch_valid_package_name_types type,
                    enum arch_valid_package_name_types supplied_type,
                    t_uchar * spec)
{
  t_uchar * name = missing_string (type);
  t_uchar * supplied_name = name_string (supplied_type);
  t_uchar * portion = arch_parse_package_name (valid_to_parse(type), arch->official_name, spec);
  t_uchar * nonarch = arch_parse_package_name (arch_ret_non_archive, "", spec);
  safe_printfmt (2, "No such %s (%s)\n", name, portion);
  safe_printfmt (2, "  name: %s\n  location: %s\n  %s: %s\n", arch->name, arch->location, supplied_name, nonarch);
  lim_free (0, portion);
  lim_free (0, nonarch);
}

static t_uchar * 
missing_string (enum arch_valid_package_name_types type)
{
  switch (type)
    {
      case arch_req_category:
        return "category";
        break;

      case arch_req_package:
        return "package";
        break;

      case arch_req_version:
        return "version";
        break;

      case arch_req_patch_level:
        return "patchlevel";
        break;
      
      default:
        panic ("missing_string: bad argument.");
        return 0;
    };
}

static t_uchar * 
name_string (enum arch_valid_package_name_types type)
{
  switch (type)
    {
      case arch_req_category:
        return "category";
        break;

      case arch_req_package:
        return "package";
        break;

      case arch_req_version:
        return "package-version";
        break;

      case arch_req_patch_level:
        return "revision";
        break;
      
      default:
        panic ("name_string: bad argument.");
        return 0;
    };
}


static enum arch_parse_package_name_type
valid_to_parse (enum arch_valid_package_name_types type)
{
  switch (type)
  {
    case arch_req_category:
      return arch_ret_category;
      break;
    case arch_req_package:
      return arch_ret_package;
      break;
    case arch_req_version:
      return arch_ret_version;
      break;
    case arch_req_patch_level:
      return arch_ret_patch_level;
      break;
    default:
      panic ("valid_to_parse: bad argument.");
      return 0;  
  };
}



void
arch_check_library_for_revision (t_uchar * archive,
                                 t_uchar * revision)
{
  t_uchar * loc = arch_library_find (rel_table_nil, archive, revision, 0);
  if (loc)
     {
   lim_free (0, loc);
    }
  else
    {
      safe_printfmt (2, "Could not find revision in any library:\n%s/%s\n", archive, revision);
      exit (2);
    }
}

extern t_uchar * 
safe_tree_version (t_uchar * cmd_name)
{
  t_uchar * tree_version = 0;
  t_uchar * tree_root = arch_tree_root (0, ".", 0);

  if (!tree_root)
    {
      safe_printfmt (2, "%s: not in a project tree\n  dir: %s\n", cmd_name, directory_as_cwd ("."));
      exit (2);
    }
  tree_version = arch_tree_version (tree_root);
  if (!tree_version)
    {
      safe_printfmt (2, "%s: no tree-version set\n  tree: %s\n", cmd_name, tree_root);
      exit (2);
    }
  lim_free (0, tree_root);
  return tree_version;
}

t_uchar *
arch_determine_revision (struct arch_archive ** arch,
                         t_uchar * default_archive,
                         t_uchar * revision_spec,
                         t_uchar * cmd_name)
{
  t_uchar * revision = 0;
  t_uchar * archive = 0;
  t_uchar * new_revision_spec = 0;
  if (!arch_valid_package_name (revision_spec, arch_maybe_archive, arch_req_version, 1))
    {
      if (!arch_valid_patch_level_name (revision_spec))
        {
          safe_printfmt (2, "%s: illegal revision spec (%s)\n", cmd_name, revision_spec);
          exit (1);
        }
      else
        {
          t_uchar * tree_version = safe_tree_version (cmd_name);
          new_revision_spec = str_alloc_cat_many (0, tree_version, "--", revision_spec, str_end);
          revision_spec = new_revision_spec;
        }
    }

  archive = arch_parse_package_name (arch_ret_archive, default_archive, revision_spec);

  *arch = arch_archive_connect (archive, 0);
  lim_free (0, archive);

  if (arch_valid_package_name (revision_spec, arch_maybe_archive, arch_req_patch_level, 0))
    {
      arch_check_for (*arch, arch_req_patch_level, revision_spec);
      revision = arch_parse_package_name (arch_ret_non_archive, 0, revision_spec);
    }
  else
    {
      t_uchar * version = 0;
      arch_check_for (*arch, arch_req_version, revision_spec);
      version = arch_parse_package_name (arch_ret_package_version, 0, revision_spec);
      revision = arch_archive_latest_revision (*arch, version, 2);

      if (!revision)
        {
          safe_printfmt (1, "%s: version has no revisions (%s)\n", cmd_name, version);
          exit (1);
        }

      lim_free (0, version);
    }
  lim_free (0, new_revision_spec);
  return revision;
}


void 
arch_check_directory (t_uchar *path, int check_write)
{
  int errn;
  int mode = R_OK+X_OK;
  struct stat statb;
  if (check_write) mode+=W_OK;
  if (vu_stat (&errn, path, &statb) == -1)
    {
       if (errn == ENOENT)
         safe_printfmt (2, "Specified directory does not exist\nPath: %s\n", path);
       else
         safe_printfmt (2, "Error encountered accessing directory (%s)\nPath: %s\n", errno_to_string (errn), path);
       
       exit(2);
    }
  if (!S_ISDIR(statb.st_mode))
    {
      safe_printfmt (2, "Specified path is not a directory\nPath: %s\n", path);
      exit (2);
    }
  if (access (path, mode) == -1)
    {
      safe_printfmt (2, "Error accessing specified directory (%s)\nDirectory: %s\n", errno_to_string (errno), path);
      exit (2);
    }
}

void 
arch_check_uri (t_uchar * uri)
{
  if (arch_valid_uri (uri))
    return;

  safe_printfmt (2, "URL invalid or unsupported: %s\n", uri);
  exit (2);
}


/**
 * Check whether the revision exists, using local data first, before
 * trying the archive
 * archive: the archive containg the revision
 * revision: the unqualified revision
 */
extern void
arch_check_revision_local (t_uchar * archive,
                           t_uchar * revision)
{
  struct arch_archive * arch = 0;
  t_uchar * loc = arch_library_find (rel_table_nil, archive, revision, 0);
  if (loc)
    {
      lim_free (0, loc);
      return;
    }
  arch = arch_archive_connect (archive, 0);
  arch_check_for (arch, arch_req_patch_level, revision);
  arch_archive_close (arch);
}


/**
 * Check the archive to make sure it's suitable for normal use, and not just
 * for mirroring.
 */
extern int
arch_check_arch (struct arch_archive * arch)
{
  if (str_cmp (arch->name, arch->official_name))
    {
      safe_printfmt (2, ("This archive should only be used as a mirror source or target.\n"
                         "registered with the wrong name.  To use it for other purposes, you should\n"
                         "register it using the official name.\n"
                         "  name: %s\n"
                         "  official name: %s\n"
                         "  location: %s\n"),
                     arch->name, arch->official_name, arch->location);
		return 1;
    }
	return 0;
}


/**
 * \brief free current and return replacement
 *
 * Factors out blocks of temporary variables into one shot.
 * \return replacement
 * \param current the string to be freed.
 * \param replacement the string to be returned
 */
t_uchar *
str_replace (t_uchar *current, t_uchar *replacement)
{
    lim_free (0, current);
    return replacement;
} 

int 
arch_separate_arch_name_from_fqrvsn(const t_uchar *name, t_uchar **archive, t_uchar **fqrvsn)
{

    t_uchar *slash = 0;
    t_uchar *tmp = str_save(0, name);
    
    lim_free(0, *archive);
    lim_free(0, *fqrvsn);
    if (0 == (slash = str_chr_index (tmp, '/')))
      {
        *archive = str_save(0, "");
        *fqrvsn = str_save(0, name);
        lim_free(0, tmp);
        return 1;
      }
    else
      { 
        *slash = 0;
        *archive = str_save(0, tmp);
        *fqrvsn = str_save(0, slash + 1);
        lim_free(0, tmp);
        return 1;
      }
    return 0;
}



extern t_uchar *
arch_interpret_delta_path (t_uchar ** arch, t_uchar ** rev, t_uchar * scratch_dir, t_uchar * default_archive, t_uchar * spec, t_uchar * cache_dir)
{
  t_uchar * answer = 0;

  if (arch_valid_package_name (spec, arch_maybe_archive, arch_req_patch_level, 0))
    {
      t_uchar * archive = 0;
      t_uchar * revision = 0;
      struct arch_archive *arch_struct = 0;

      archive = arch_parse_package_name (arch_ret_archive, default_archive, spec);
      revision = arch_parse_package_name (arch_ret_non_archive, 0, spec);
      
      arch_struct = arch_archive_connect (archive, 0);
      arch_check_for (arch_struct, arch_req_patch_level, revision);

      safe_printfmt (1, "* finding or making %s\n", spec);

      answer = arch_find_or_make_tmp_local_copy (1, scratch_dir, 0, cache_dir, arch_struct, archive, revision);
      arch_archive_close (arch_struct);

      if (arch)
        *arch = archive;
      else
        lim_free (0, archive);

      if (rev)
        *rev = revision;
      else
        lim_free (0, revision);
    }
  else if (arch_valid_patch_level_name (spec))
    {
      t_uchar * archive = 0;
      t_uchar * revision = 0;
      t_uchar * fqvsn = 0;
      t_uchar * tree_version = 0;
      t_uchar * tree_root = 0;
      
      tree_root = arch_tree_root (0, ".", 0);

      if (!tree_root)
        {
          safe_printfmt (2, "tla delta: not in a project tree\n  dir: %s\n", directory_as_cwd ("."));
          exit (2);
        }

      fqvsn = arch_tree_version (tree_root);
      if (!fqvsn)
        {
          safe_printfmt (2, "tla delta: no tree-version set\n  tree: %s\n", tree_root);
          exit (2);
        }
      
      archive = arch_parse_package_name (arch_ret_archive, 0, fqvsn);
      tree_version = arch_parse_package_name (arch_ret_non_archive, 0, fqvsn);
      revision = str_alloc_cat_many (0, tree_version, "--", spec, str_end);

      safe_printfmt (1, "* finding or making %s\n", spec);
      safe_flush (1);

      answer = arch_find_or_make_tmp_local_copy (1, scratch_dir, 0, cache_dir, 0, archive, revision);

      if (arch)
        *arch = archive;
      else
        lim_free (0, archive);

      if (rev)
        *rev = revision;
      else
        lim_free (0, revision);

      lim_free (0, fqvsn);
      lim_free (0, tree_version);
      lim_free (0, tree_root);
    }
  else
    {
      arch_check_directory (spec, 0);
      answer = directory_as_cwd (spec);
    }

  return answer;
}



/* tag: Aaron Bentley Tue Dec 23 10:46:00 2003 (cmdutils.c)
 */