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
|
/*
* "$Id: pkg.c 833 2010-12-30 00:00:50Z mike $"
*
* AT&T package gateway for the ESP Package Manager (EPM).
*
* Copyright 1999-2010 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:
*
* make_pkg() - Make an AT&T software distribution package.
* pkg_path() - Return an absolute path for the prototype file.
*/
/*
* Include necessary headers...
*/
#include "epm.h"
/*
* Local functions...
*/
static const char *pkg_path(const char *filename, const char *dirname);
/*
* 'make_pkg()' - Make an AT&T software distribution package.
*/
int /* O - 0 = success, 1 = fail */
make_pkg(const char *prodname, /* I - Product short name */
const char *directory, /* I - Directory for distribution files */
const char *platname, /* I - Platform name */
dist_t *dist, /* I - Distribution information */
struct utsname *platform) /* I - Platform information */
{
int i; /* Looping var */
FILE *fp; /* Control file */
char name[1024]; /* Full product name */
char filename[1024], /* Destination filename */
preinstall[1024], /* Pre install script */
postinstall[1024], /* Post install script */
preremove[1024], /* Pre remove script */
postremove[1024], /* Post remove script */
request[1024]; /* Request script */
char current[1024]; /* Current directory */
file_t *file; /* Current distribution file */
command_t *c; /* Current command */
depend_t *d; /* Current dependency */
tarf_t *tarfile; /* Distribution file */
time_t curtime; /* Current time info */
struct tm *curdate; /* Current date info */
const char *runlevels; /* Run levels */
if (Verbosity)
puts("Creating PKG distribution...");
if (dist->release[0])
{
if (platname[0])
snprintf(name, sizeof(name), "%s-%s-%s-%s", prodname, dist->version, dist->release,
platname);
else
snprintf(name, sizeof(name), "%s-%s-%s", prodname, dist->version, dist->release);
}
else if (platname[0])
snprintf(name, sizeof(name), "%s-%s-%s", prodname, dist->version, platname);
else
snprintf(name, sizeof(name), "%s-%s", prodname, dist->version);
getcwd(current, sizeof(current));
/*
* Write the pkginfo file for pkgmk...
*/
if (Verbosity)
puts("Creating package information file...");
snprintf(filename, sizeof(filename), "%s/%s.pkginfo", directory, prodname);
if ((fp = fopen(filename, "w")) == NULL)
{
fprintf(stderr, "epm: Unable to create package information file \"%s\" - %s\n", filename,
strerror(errno));
return (1);
}
curtime = time(NULL);
curdate = gmtime(&curtime);
fprintf(fp, "PKG=%s\n", prodname);
fprintf(fp, "NAME=%s\n", dist->product);
fprintf(fp, "VERSION=%s\n", dist->version);
fprintf(fp, "VENDOR=%s\n", dist->vendor);
fprintf(fp, "PSTAMP=epm%04d%02d%02d%02d%02d%02d\n",
curdate->tm_year + 1900, curdate->tm_mon + 1, curdate->tm_mday,
curdate->tm_hour, curdate->tm_min, curdate->tm_sec);
if (dist->num_descriptions > 0)
fprintf(fp, "DESC=%s\n", dist->descriptions[0].description);
fputs("CATEGORY=application\n", fp);
fputs("CLASSES=none", fp);
for (i = 0; i < dist->num_subpackages; i ++)
fprintf(fp, " %s", dist->subpackages[i]);
putc('\n', fp);
if (strcmp(platform->machine, "intel") == 0)
fputs("ARCH=i86pc\n", fp);
else
fputs("ARCH=sparc\n", fp);
fclose(fp);
/*
* Write the depend file for pkgmk...
*/
if (Verbosity)
puts("Creating package dependency file...");
snprintf(filename, sizeof(filename), "%s/%s.depend", directory, prodname);
if ((fp = fopen(filename, "w")) == NULL)
{
fprintf(stderr, "epm: Unable to create package dependency file \"%s\" - %s\n", filename,
strerror(errno));
return (1);
}
for (i = dist->num_depends, d = dist->depends; i > 0; i --, d ++)
if (!strcmp(d->product, "_self"))
continue;
else if (d->type == DEPEND_REQUIRES)
fprintf(fp, "P %s\n", d->product);
else
fprintf(fp, "I %s\n", d->product);
fclose(fp);
/*
* Write the preinstall file for pkgmk...
*/
for (i = dist->num_commands, c = dist->commands; i > 0; i --, c ++)
if (c->type == COMMAND_PRE_INSTALL)
break;
if (i)
{
/*
* Write the preinstall file for pkgmk...
*/
if (Verbosity)
puts("Creating preinstall script...");
snprintf(preinstall, sizeof(preinstall), "%s/%s.preinstall", directory,
prodname);
if ((fp = fopen(preinstall, "w")) == NULL)
{
fprintf(stderr, "epm: Unable to create script file \"%s\" - %s\n", preinstall,
strerror(errno));
return (1);
}
fchmod(fileno(fp), 0755);
fputs("#!/bin/sh\n", fp);
fputs("# " EPM_VERSION "\n", fp);
for (; i > 0; i --, c ++)
if (c->type == COMMAND_PRE_INSTALL)
fprintf(fp, "%s\n", c->command);
fclose(fp);
}
else
preinstall[0] = '\0';
/*
* Write the postinstall file for pkgmk...
*/
for (i = dist->num_commands, c = dist->commands; i > 0; i --, c ++)
if (c->type == COMMAND_POST_INSTALL)
break;
if (!i)
for (i = dist->num_files, file = dist->files; i > 0; i --, file ++)
if (tolower(file->type) == 'i')
break;
if (i)
{
/*
* Write the postinstall file for pkgmk...
*/
if (Verbosity)
puts("Creating postinstall script...");
snprintf(postinstall, sizeof(postinstall), "%s/%s.postinstall", directory,
prodname);
if ((fp = fopen(postinstall, "w")) == NULL)
{
fprintf(stderr, "epm: Unable to create script file \"%s\" - %s\n", postinstall,
strerror(errno));
return (1);
}
fchmod(fileno(fp), 0755);
fputs("#!/bin/sh\n", fp);
fputs("# " EPM_VERSION "\n", fp);
for (i = dist->num_commands, c = dist->commands; i > 0; i --, c ++)
if (c->type == COMMAND_POST_INSTALL)
fprintf(fp, "%s\n", c->command);
for (i = dist->num_files, file = dist->files; i > 0; i --, file ++)
if (tolower(file->type) == 'i')
qprintf(fp, "/etc/init.d/%s start\n", file->dst);
fclose(fp);
}
else
postinstall[0] = '\0';
/*
* Write the preremove script...
*/
for (i = dist->num_commands, c = dist->commands; i > 0; i --, c ++)
if (c->type == COMMAND_PRE_REMOVE)
break;
if (!i)
for (i = dist->num_files, file = dist->files; i > 0; i --, file ++)
if (tolower(file->type) == 'i')
break;
if (i)
{
/*
* Write the preremove file for pkgmk...
*/
if (Verbosity)
puts("Creating preremove script...");
snprintf(preremove, sizeof(preremove), "%s/%s.preremove", directory, prodname);
if ((fp = fopen(preremove, "w")) == NULL)
{
fprintf(stderr, "epm: Unable to create script file \"%s\" - %s\n", preremove,
strerror(errno));
return (1);
}
fchmod(fileno(fp), 0755);
fputs("#!/bin/sh\n", fp);
fputs("# " EPM_VERSION "\n", fp);
for (i = dist->num_files, file = dist->files; i > 0; i --, file ++)
if (tolower(file->type) == 'i')
qprintf(fp, "/etc/init.d/%s stop\n", file->dst);
for (i = dist->num_commands, c = dist->commands; i > 0; i --, c ++)
if (c->type == COMMAND_PRE_REMOVE)
fprintf(fp, "%s\n", c->command);
fclose(fp);
}
else
preremove[0] = '\0';
/*
* Write the postremove file for pkgmk...
*/
for (i = dist->num_commands, c = dist->commands; i > 0; i --, c ++)
if (c->type == COMMAND_POST_REMOVE)
break;
if (i)
{
/*
* Write the postremove file for pkgmk...
*/
if (Verbosity)
puts("Creating postremove script...");
snprintf(postremove, sizeof(postremove), "%s/%s.postremove", directory,
prodname);
if ((fp = fopen(postremove, "w")) == NULL)
{
fprintf(stderr, "epm: Unable to create script file \"%s\" - %s\n", postremove,
strerror(errno));
return (1);
}
fchmod(fileno(fp), 0755);
fputs("#!/bin/sh\n", fp);
fputs("# " EPM_VERSION "\n", fp);
for (; i > 0; i --, c ++)
if (c->type == COMMAND_POST_REMOVE)
fprintf(fp, "%s\n", c->command);
fclose(fp);
}
else
postremove[0] = '\0';
/*
* Write the request file for pkgmk...
*/
for (i = dist->num_commands, c = dist->commands; i > 0; i --, c ++)
if (c->type == COMMAND_LITERAL && !strcmp(c->section, "request"))
break;
if (i)
{
/*
* Write the request file for pkgmk...
*/
if (Verbosity)
puts("Creating request script...");
snprintf(request, sizeof(request), "%s/%s.request", directory,
prodname);
if ((fp = fopen(request, "w")) == NULL)
{
fprintf(stderr, "epm: Unable to create script file \"%s\" - %s\n",
request, strerror(errno));
return (1);
}
fchmod(fileno(fp), 0755);
fputs("#!/bin/sh\n", fp);
fputs("# " EPM_VERSION "\n", fp);
for (; i > 0; i --, c ++)
if (c->type == COMMAND_LITERAL && !strcmp(c->section, "request"))
fprintf(fp, "%s\n", c->command);
fclose(fp);
}
else
request[0] = '\0';
/*
* Add symlinks for init scripts...
*/
for (i = 0; i < dist->num_files; i ++)
if (tolower(dist->files[i].type) == 'i')
{
/*
* Make symlinks for all of the selected run levels...
*/
for (runlevels = get_runlevels(dist->files + i, "023");
isdigit(*runlevels & 255);
runlevels ++)
{
file = add_file(dist, dist->files[i].subpackage);
file->type = 'l';
file->mode = 0;
strcpy(file->user, "root");
strcpy(file->group, "sys");
snprintf(file->src, sizeof(file->src), "../init.d/%s",
dist->files[i].dst);
if (*runlevels == '0')
snprintf(file->dst, sizeof(file->dst), "/etc/rc0.d/K%02d%s",
get_stop(dist->files + i, 0), dist->files[i].dst);
else
snprintf(file->dst, sizeof(file->dst), "/etc/rc%c.d/S%02d%s",
*runlevels, get_start(dist->files + i, 99),
dist->files[i].dst);
}
/*
* Then send the original file to /etc/init.d...
*/
file = dist->files + i;
snprintf(filename, sizeof(filename), "/etc/init.d/%s", file->dst);
strcpy(file->dst, filename);
}
/*
* Write the prototype file for pkgmk...
*/
if (Verbosity)
puts("Creating prototype file...");
snprintf(filename, sizeof(filename), "%s/%s.prototype", directory, prodname);
if ((fp = fopen(filename, "w")) == NULL)
{
fprintf(stderr, "epm: Unable to create prototype file \"%s\" - %s\n",
filename, strerror(errno));
return (1);
}
#if 0 /* apparently does not work on Solaris 7... */
fprintf(fp, "!search %s\n", current);
#endif /* 0 */
if (dist->license[0])
fprintf(fp, "i copyright=%s\n", pkg_path(dist->license, current));
fprintf(fp, "i depend=%s/%s.depend\n", pkg_path(directory, current), prodname);
fprintf(fp, "i pkginfo=%s/%s.pkginfo\n", pkg_path(directory, current),
prodname);
if (preinstall[0])
fprintf(fp, "i preinstall=%s\n", pkg_path(preinstall, current));
if (postinstall[0])
fprintf(fp, "i postinstall=%s\n", pkg_path(postinstall, current));
if (preremove[0])
fprintf(fp, "i preremove=%s\n", pkg_path(preremove, current));
if (postremove[0])
fprintf(fp, "i postremove=%s\n", pkg_path(postremove, current));
if (request[0])
fprintf(fp, "i request=%s\n", pkg_path(request, current));
for (i = dist->num_files, file = dist->files; i > 0; i --, file ++)
switch (tolower(file->type))
{
case 'c' :
qprintf(fp, "e %s %s=%s %04o %s %s\n",
file->subpackage ? file->subpackage : "none",
file->dst, pkg_path(file->src, current),
file->mode, file->user, file->group);
break;
case 'd' :
qprintf(fp, "d %s %s %04o %s %s\n",
file->subpackage ? file->subpackage : "none",
file->dst, file->mode, file->user, file->group);
break;
case 'f' :
case 'i' :
qprintf(fp, "f %s %s=%s %04o %s %s\n",
file->subpackage ? file->subpackage : "none",
file->dst, pkg_path(file->src, current),
file->mode, file->user, file->group);
break;
case 'l' :
qprintf(fp, "s %s %s=%s\n",
file->subpackage ? file->subpackage : "none",
file->dst, file->src);
break;
}
fclose(fp);
/*
* Build the distribution from the prototype file...
*/
if (Verbosity)
puts("Building PKG binary distribution...");
if (run_command(NULL, "pkgmk -o -f %s/%s.prototype -d %s/%s",
directory, prodname, current, directory))
return (1);
/*
* Tar and compress the distribution...
*/
if (Verbosity)
puts("Creating .pkg.tgz file for distribution...");
snprintf(filename, sizeof(filename), "%s/%s.pkg.tgz", directory, name);
if ((tarfile = tar_open(filename, 1)) == NULL)
return (1);
snprintf(filename, sizeof(filename), "%s/%s", directory, prodname);
if (tar_directory(tarfile, filename, prodname))
{
tar_close(tarfile);
return (1);
}
tar_close(tarfile);
/*
* Make a package stream file...
*/
if (Verbosity)
puts("Copying into package stream file...");
if (run_command(directory, "pkgtrans -s %s/%s %s.pkg %s",
current, directory, name, prodname))
return (1);
/*
* Compress the package stream file...
*/
snprintf(filename, sizeof(filename), "%s.pkg.gz", name);
unlink(filename);
if (run_command(directory, EPM_GZIP " -vf9 %s.pkg", name))
return (1);
/*
* Remove temporary files...
*/
if (!KeepFiles)
{
if (Verbosity)
puts("Removing temporary distribution files...");
snprintf(filename, sizeof(filename), "%s/%s.pkginfo", directory, prodname);
unlink(filename);
snprintf(filename, sizeof(filename), "%s/%s.depend", directory, prodname);
unlink(filename);
snprintf(filename, sizeof(filename), "%s/%s.prototype", directory, prodname);
unlink(filename);
if (preinstall[0])
unlink(preinstall);
if (postinstall[0])
unlink(postinstall);
if (preremove[0])
unlink(preremove);
if (postremove[0])
unlink(postremove);
if (request[0])
unlink(request);
snprintf(filename, sizeof(filename), "%s/%s", directory, prodname);
unlink_directory(filename);
}
return (0);
}
/*
* 'pkg_path()' - Return an absolute path for the prototype file.
*/
static const char * /* O - Absolute filename */
pkg_path(const char *filename, /* I - Source filename */
const char *dirname) /* I - Source directory */
{
static char absname[1024]; /* Absolute filename */
if (filename[0] == '/')
return (filename);
snprintf(absname, sizeof(absname), "%s/%s", dirname, filename);
return (absname);
}
/*
* End of "$Id: pkg.c 833 2010-12-30 00:00:50Z mike $".
*/
|