File: epminstall.c

package info (click to toggle)
epm 3.0-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,020 kB
  • ctags: 371
  • sloc: ansic: 6,199; cpp: 1,188; makefile: 201; perl: 134
file content (527 lines) | stat: -rw-r--r-- 12,489 bytes parent folder | download
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
/*
 * "$Id: epminstall.c,v 1.1.1.1 2001/09/18 01:47:13 jeff Exp $"
 *
 *   Install program replacement for the ESP Package Manager (EPM).
 *
 *   Copyright 1999-2001 by Easy Software Products.
 *
 *   This program 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 2, or (at your option)
 *   any later version.
 *
 *   This program 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.
 *
 * Contents:
 *
 *   main()       - Add or replace files, directories, and symlinks.
 *   find_file()  - Find a file in the distribution...
 *   info()       - Show the EPM copyright and license.
 *   usage()      - Show command-line usage instructions.
 *   write_dist() - Write a distribution list file...
 */

/*
 * Include necessary headers...
 */

#include "epm.h"


/*
 * Global variable used by dist functions...
 */

int		Verbosity = 0;


/*
 * Local functions...
 */

static file_t	*find_file(dist_t *dist, const char *dst);
static void	info(void);
static void	usage(void);
static int	write_dist(const char *listname, dist_t *dist);


/*
 * 'main()' - Add or replace files, directories, and symlinks.
 */

int				/* O - Exit status */
main(int  argc,			/* I - Number of command-line arguments */
     char *argv[])		/* I - Command-line arguments */
{
  int		i;		/* Looping var */
  int		mode,		/* Permissions */
		directories;	/* Installing directories? */
  char		*user,		/* Owner */
		*group,		/* Group */
		*listname,	/* List filename */
		*src,		/* Source filename */
		dst[1024],	/* Destination filename */
		linkname[1024];	/* Symlink name */
  int		linklen;	/* Length of symlink */
  int		num_files;	/* Number of files to install */
  char		*files[1000];	/* Files to install */
  struct stat	fileinfo;	/* File information */
  dist_t	*dist;		/* Distribution */
  file_t	*file;		/* File in distribution */
  struct utsname platform;	/* Platform information */


 /*
  * Parse the command-line arguments...
  */

  num_files   = 0;
  mode        = 0;
  user        = "root";
  group       = "sys";
  listname    = "epm.list";
  directories = 0;

  for (i = 1; i < argc; i ++)
    if (strcmp(argv[i], "-b") == 0)
      continue;
    else if (strcmp(argv[i], "-c") == 0)
      continue;
    else if (strcmp(argv[i], "-d") == 0)
      directories = 1;
    else if (strcmp(argv[i], "-g") == 0)
    {
      i ++;
      if (i < argc)
	group = argv[i];
      else
	usage();
    }
    else if (strcmp(argv[i], "-m") == 0)
    {
      i ++;
      if (i < argc)
	mode = strtol(argv[i], NULL, 8);
      else
	usage();
    }
    else if (strcmp(argv[i], "-o") == 0)
    {
      i ++;
      if (i < argc)
	user = argv[i];
      else
	usage();
    }
    else if (strcmp(argv[i], "-s") == 0)
      continue;
    else if (strcmp(argv[i], "--list-file") == 0)
    {
      i ++;
      if (i < argc)
	listname = argv[i];
      else
	usage();
    }
    else if (argv[i][0] == '-')
      usage();
    else if (num_files < (sizeof(files) / sizeof(files[0])))
    {
      files[num_files] = argv[i];
      num_files ++;
    }
    else
    {
      fputs("epminstall: Too many filenames on command-line!\n", stderr);
      usage();
    }

  if (num_files == 0 || (num_files < 2 && !directories))
    usage();

  get_platform(&platform);

  if ((dist = read_dist(listname, &platform, "")) == NULL)
  {
    fprintf(stderr, "epminstall: Unable to read list file \"%s\": %s\n",
            listname, strerror(errno));
    return (1);
  }

 /*
  * Check to see if we are installing files or directories...
  */

  if (directories)
  {
    if (!mode)
      mode = 0755;

    for (i = 0; i < num_files; i ++)
    {
     /*
      * Copy the directory name into a new file entry, which is cleared
      * by add_file()...
      */

      if ((file = find_file(dist, files[i])) == NULL)
        file = add_file(dist);

      file->type = 'd';
      file->mode = mode & 07777;
      strncpy(file->user, user, sizeof(file->user) - 1);
      strncpy(file->group, group, sizeof(file->group) - 1);
      strncpy(file->dst, files[i], sizeof(file->dst) - 1);
      strcpy(file->src, "-");
    }
  }
  else
  {
   /*
    * Check to see if we have 1 or more files and a directory or
    * a source and destination file...
    */

    if (num_files == 2)
    {
      file = find_file(dist, files[1]);

      if (file == NULL || file->type != 'd')
      {
        if (!file)
	  file = add_file(dist);

        if (stat(files[0], &fileinfo))
	{
	  fprintf(stderr, "epminstall: Unable to stat \"%s\": %s\n",
	          files[0], strerror(errno));
          fileinfo.st_mode = mode;
        }

        if (S_ISLNK(fileinfo.st_mode))
	{
	  file->type = 'l';

	  if ((linklen = readlink(files[0], linkname, sizeof(linkname) - 1)) < 0)
	  {
	    fprintf(stderr, "epminstall: Unable to read symlink \"%s\": %s\n",
	            files[0], strerror(errno));
            files[0] = "BROKEN-LINK";
	  }
	  else
	  {
	    linkname[linklen] = '\0';
	    files[0]          = linkname;
	  }
	}
	else
	  file->type = 'f';

        if (mode)
	  file->mode = mode & 07777;
	else if (fileinfo.st_mode & 0111)
	  file->mode = 0755;
	else
	  file->mode = 0644;

	strncpy(file->user, user, sizeof(file->user) - 1);
	strncpy(file->group, group, sizeof(file->group) - 1);
	strncpy(file->dst, files[1], sizeof(file->dst) - 1);
	strncpy(file->src, files[0], sizeof(file->src) - 1);
      }
      else
        num_files --;
    }

    if (num_files != 2)
    {
      if (num_files > 1)
        num_files --;

      if ((file = find_file(dist, files[num_files])) == NULL)
      {
       /*
        * Add the installation directory to the file list...
	*/

	file = add_file(dist);

	file->type = 'd';
	file->mode = 0755;
	strncpy(file->user, user, sizeof(file->user) - 1);
	strncpy(file->group, group, sizeof(file->group) - 1);
	strncpy(file->dst, files[num_files], sizeof(file->dst) - 1);
	strcpy(file->src, "-");
      }
      else if (file->type != 'd')
      {
        fprintf(stderr, "epminstall: Destination path \"%s\" is not a directory!\n",
	        files[num_files]);
        return (1);
      }

      for (i = 0; i < num_files; i ++)
      {
       /*
	* Add/update the file in the distribution...
	*/

        if ((src = strrchr(files[i], '/')) != NULL)
	  src ++;
	else
	  src = files[i];

        snprintf(dst, sizeof(dst), "%s/%s", files[num_files], src);

	if ((file = find_file(dist, dst)) == NULL)
          file = add_file(dist);

        if (stat(files[i], &fileinfo))
	{
	  fprintf(stderr, "epminstall: Unable to stat \"%s\": %s\n",
	          files[i], strerror(errno));
          fileinfo.st_mode = mode;
        }

        if (S_ISLNK(fileinfo.st_mode))
	{
	  file->type = 'l';

	  if ((linklen = readlink(files[i], linkname, sizeof(linkname) - 1)) < 0)
	  {
	    fprintf(stderr, "epminstall: Unable to read symlink \"%s\": %s\n",
	            files[i], strerror(errno));
            files[i] = "BROKEN-LINK";
	  }
	  else
	  {
	    linkname[linklen] = '\0';
	    files[i]          = linkname;
	  }
	}
	else
	  file->type = 'f';

        if (mode)
	  file->mode = mode & 07777;
	else if (fileinfo.st_mode & 0111)
	  file->mode = 0755;
	else
	  file->mode = 0644;

	strncpy(file->user, user, sizeof(file->user) - 1);
	strncpy(file->group, group, sizeof(file->group) - 1);
	strncpy(file->dst, dst, sizeof(file->dst) - 1);
	strncpy(file->src, files[i], sizeof(file->src) - 1);
      }
    }
  }

 /*
  * Sort the files to make the final list file easier to check...
  */

  sort_dist_files(dist);

 /*
  * Write the distribution file...
  */

  if (write_dist(listname, dist))
  {
    fprintf(stderr, "epminstall: Unable to read list file \"%s\": %s\n",
            listname, strerror(errno));
    return (1);
  }

 /*
  * Return with no errors...
  */

  return (0);
}


/*
 * 'find_file()' - Find a file in the distribution...
 */

static file_t *			/* O - File entry or NULL */
find_file(dist_t     *dist,	/* I - Distribution to search */
          const char *dst)	/* I - Destination filename */
{
  int		i;		/* Looping var */
  file_t	*file;		/* Current file */


  for (i = dist->num_files, file = dist->files; i > 0; i --, file ++)
    if (strcmp(file->dst, dst) == 0)
      return (file);

  return (NULL);
}


/*
 * 'info()' - Show the EPM copyright and license.
 */

static void
info(void)
{
  puts(EPM_VERSION);
  puts("Copyright 1999-2001 by Easy Software Products.");
  puts("");
  puts("EPM is free software and comes with ABSOLUTELY NO WARRANTY; for details");
  puts("see the GNU General Public License in the file COPYING or at");
  puts("\"http://www.fsf.org/gpl.html\".  Report all problems to \"epm@easysw.com\".");
  puts("");
}


/*
 * 'usage()' - Show command-line usage instructions.
 */

static void
usage(void)
{
  info();

  puts("Usage: epminstall [options] file1 file2 ... fileN directory");
  puts("       epminstall [options] file1 file2");
  puts("       epminstall [options] -d directory1 directory2 ... directoryN");
  puts("Options:");
  puts("-g group");
  puts("    Set group of installed file(s).");
  puts("-m mode");
  puts("    Set permissions of installed file(s).");
  puts("-u owner");
  puts("    Set owner of installed file(s).");

  exit(1);
}


/*
 * 'write_dist()' - Write a distribution list file...
 */

static int				/* O - 0 on success, -1 on failure */
write_dist(const char *listname,	/* I - File to write to */
           dist_t     *dist)		/* I - Distribution to write */
{
  int		i;			/* Looping var */
  char		listbck[1024],		/* Backup filename */
		*ptr;			/* Pointer into command string */
  FILE		*listfile;		/* Output file */
  file_t	*file;			/* Current file entry */
  static const char *commands[] =	/* Command strings */
		{
		  "%preinstall",
		  "%postinstall",
		  "%prepatch",
		  "%postpatch",
		  "%preremove",
		  "%postremove"
		},
		*depends[] =		/* Dependency strings */
		{
		  "%requires",
		  "%incompat",
		  "%replaces"
		};


 /*
  * Make a backup of the list file...
  */

  snprintf(listbck, sizeof(listbck), "%s.O", listname);

  rename(listname, listbck);

 /*
  * Open the list file...
  */

  if ((listfile = fopen(listname, "w")) == NULL)
  {
    rename(listbck, listname);
    return (-1);
  }

 /*
  * Write the list file...
  */

  fputs("# List file created by epminstall\n", listfile);
  fputs("# " EPM_VERSION "\n", listfile);

  if (dist->product[0])
    fprintf(listfile, "%%product %s\n", dist->product);
  if (dist->version[0])
    fprintf(listfile, "%%version %s %d\n", dist->version, dist->vernumber);
  if (dist->relnumber)
    fprintf(listfile, "%%release %d\n", dist->relnumber);
  if (dist->copyright[0])
    fprintf(listfile, "%%copyright %s\n", dist->copyright);
  if (dist->vendor[0])
    fprintf(listfile, "%%vendor %s\n", dist->vendor);
  if (dist->packager[0])
    fprintf(listfile, "%%packager %s\n", dist->packager);
  if (dist->license[0])
    fprintf(listfile, "%%license %s\n", dist->license);
  if (dist->readme[0])
    fprintf(listfile, "%%readme %s\n", dist->readme);

  for (i = 0; i < dist->num_descriptions; i ++)
    fprintf(listfile, "%%description %s\n", dist->descriptions[i]);

  for (i = 0; i < dist->num_depends; i ++)
  {
    fprintf(listfile, "%s %s", depends[(int)dist->depends[i].type],
            dist->depends[i].product);

    if (dist->depends[i].version[0][0] ||
        dist->depends[i].version[1][0])
    {
      fprintf(listfile, " %s %d %s %d\n",
              dist->depends[i].version[0],
	      dist->depends[i].vernumber[0],
              dist->depends[i].version[1],
	      dist->depends[i].vernumber[1]);
    }
    else
      putc('\n', listfile);
  }

  for (i = 0; i < dist->num_commands; i ++)
  {
    fputs(commands[(int)dist->commands[i].type], listfile);
    putc(' ', listfile);
    for (ptr = dist->commands[i].command; *ptr; ptr ++)
    {
      if (*ptr == '$')
        putc(*ptr, listfile);

      putc(*ptr, listfile);
    }
    putc('\n', listfile);
  }

  for (i = dist->num_files, file = dist->files; i > 0; i --, file ++)
    fprintf(listfile, "%c %04o %s %s %s %s\n",
	    file->type, file->mode, file->user, file->group,
	    file->dst, file->src);

  return (fclose(listfile));
}


/*
 * End of "$Id: epminstall.c,v 1.1.1.1 2001/09/18 01:47:13 jeff Exp $".
 */