Radix cross Linux package tools

Package Tools – is a set of utilities to create, install, and update RcL packages

3 Commits   0 Branches   2 Tags
11c606a6 (kx 2023-04-11 01:18:34 +0300    1) 
11c606a6 (kx 2023-04-11 01:18:34 +0300    2) /**********************************************************************
11c606a6 (kx 2023-04-11 01:18:34 +0300    3) 
11c606a6 (kx 2023-04-11 01:18:34 +0300    4)   Copyright 2019 Andrey V.Kosteltsev
11c606a6 (kx 2023-04-11 01:18:34 +0300    5) 
11c606a6 (kx 2023-04-11 01:18:34 +0300    6)   Licensed under the Radix.pro License, Version 1.0 (the "License");
11c606a6 (kx 2023-04-11 01:18:34 +0300    7)   you may not use this file  except  in compliance with the License.
11c606a6 (kx 2023-04-11 01:18:34 +0300    8)   You may obtain a copy of the License at
11c606a6 (kx 2023-04-11 01:18:34 +0300    9) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   10)      https://radix.pro/licenses/LICENSE-1.0-en_US.txt
11c606a6 (kx 2023-04-11 01:18:34 +0300   11) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   12)   Unless required by applicable law or agreed to in writing, software
11c606a6 (kx 2023-04-11 01:18:34 +0300   13)   distributed under the License is distributed on an "AS IS" BASIS,
11c606a6 (kx 2023-04-11 01:18:34 +0300   14)   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11c606a6 (kx 2023-04-11 01:18:34 +0300   15)   implied.
11c606a6 (kx 2023-04-11 01:18:34 +0300   16) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   17)  **********************************************************************/
11c606a6 (kx 2023-04-11 01:18:34 +0300   18) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   19) #include <config.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   20) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   21) #include <stdlib.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   22) #include <stdio.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   23) #include <sys/types.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   24) #include <stdint.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   25) #include <dirent.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   26) #include <sys/stat.h> /* chmod(2)    */
11c606a6 (kx 2023-04-11 01:18:34 +0300   27) #include <fcntl.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   28) #include <linux/limits.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   29) #include <alloca.h>   /* alloca(3)   */
11c606a6 (kx 2023-04-11 01:18:34 +0300   30) #include <string.h>   /* strdup(3)   */
11c606a6 (kx 2023-04-11 01:18:34 +0300   31) #include <libgen.h>   /* basename(3) */
11c606a6 (kx 2023-04-11 01:18:34 +0300   32) #include <ctype.h>    /* tolower(3)  */
11c606a6 (kx 2023-04-11 01:18:34 +0300   33) #include <errno.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   34) #include <time.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   35) #include <sys/time.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   36) #include <pwd.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   37) #include <grp.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   38) #include <stdarg.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   39) #include <unistd.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   40) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   41) #include <sys/resource.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   42) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   43) #include <signal.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   44) #if !defined SIGCHLD && defined SIGCLD
11c606a6 (kx 2023-04-11 01:18:34 +0300   45) # define SIGCHLD SIGCLD
11c606a6 (kx 2023-04-11 01:18:34 +0300   46) #endif
11c606a6 (kx 2023-04-11 01:18:34 +0300   47) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   48) #define _GNU_SOURCE
11c606a6 (kx 2023-04-11 01:18:34 +0300   49) #include <getopt.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   50) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   51) #include <msglog.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   52) #include <wrapper.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   53) #include <system.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   54) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   55) #define PROGRAM_NAME "pkglog"
11c606a6 (kx 2023-04-11 01:18:34 +0300   56) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   57) #include <defs.h>
11c606a6 (kx 2023-04-11 01:18:34 +0300   58) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   59) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   60) char *program     = PROGRAM_NAME;
11c606a6 (kx 2023-04-11 01:18:34 +0300   61) char *destination = NULL, *srcdir = NULL, *pkginfo_fname = NULL, *output_fname = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300   62) int   exit_status = EXIT_SUCCESS; /* errors counter */
11c606a6 (kx 2023-04-11 01:18:34 +0300   63) char *selfdir     = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300   64) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   65) int   mkgroupdir  = 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300   66) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   67) int   rm_srcdir_at_exit = 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300   68) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   69) char           *pkgname = NULL,
11c606a6 (kx 2023-04-11 01:18:34 +0300   70)                 *pkgver = NULL,
11c606a6 (kx 2023-04-11 01:18:34 +0300   71)                   *arch = NULL,
11c606a6 (kx 2023-04-11 01:18:34 +0300   72)             *distroname = NULL,
11c606a6 (kx 2023-04-11 01:18:34 +0300   73)              *distrover = NULL,
11c606a6 (kx 2023-04-11 01:18:34 +0300   74)                  *group = NULL,
11c606a6 (kx 2023-04-11 01:18:34 +0300   75)                    *url = NULL,
11c606a6 (kx 2023-04-11 01:18:34 +0300   76)                *license = NULL,
11c606a6 (kx 2023-04-11 01:18:34 +0300   77)      *uncompressed_size = NULL,
11c606a6 (kx 2023-04-11 01:18:34 +0300   78)            *total_files = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300   79) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   80) FILE *pkginfo = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300   81) FILE *output  = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300   82) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   83) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   84) #define FREE_PKGLOG_VARIABLES() \
11c606a6 (kx 2023-04-11 01:18:34 +0300   85)   if( pkgname )           { free( pkgname );           } pkgname = NULL;            \
11c606a6 (kx 2023-04-11 01:18:34 +0300   86)   if( pkgver )            { free( pkgver );            } pkgver = NULL;             \
11c606a6 (kx 2023-04-11 01:18:34 +0300   87)   if( arch )              { free( arch );              } arch = NULL;               \
11c606a6 (kx 2023-04-11 01:18:34 +0300   88)   if( distroname )        { free( distroname );        } distroname = NULL;         \
11c606a6 (kx 2023-04-11 01:18:34 +0300   89)   if( distrover )         { free( distrover );         } distrover = NULL;          \
11c606a6 (kx 2023-04-11 01:18:34 +0300   90)   if( group )             { free( group );             } group = NULL;              \
11c606a6 (kx 2023-04-11 01:18:34 +0300   91)   if( url )               { free( url );               } url = NULL;                \
11c606a6 (kx 2023-04-11 01:18:34 +0300   92)   if( license )           { free( license );           } license = NULL;            \
11c606a6 (kx 2023-04-11 01:18:34 +0300   93)   if( uncompressed_size ) { free( uncompressed_size ); } uncompressed_size = NULL;  \
11c606a6 (kx 2023-04-11 01:18:34 +0300   94)   if( total_files )       { free( total_files );       } total_files = NULL
11c606a6 (kx 2023-04-11 01:18:34 +0300   95) 
11c606a6 (kx 2023-04-11 01:18:34 +0300   96) void free_resources()
11c606a6 (kx 2023-04-11 01:18:34 +0300   97) {
11c606a6 (kx 2023-04-11 01:18:34 +0300   98)   if( selfdir )       { free( selfdir );       selfdir       = NULL; }
11c606a6 (kx 2023-04-11 01:18:34 +0300   99)   if( srcdir )        { free( srcdir );        srcdir        = NULL; }
11c606a6 (kx 2023-04-11 01:18:34 +0300  100)   if( destination )   { free( destination );   destination   = NULL; }
11c606a6 (kx 2023-04-11 01:18:34 +0300  101)   if( pkginfo_fname ) { free( pkginfo_fname ); pkginfo_fname = NULL; }
11c606a6 (kx 2023-04-11 01:18:34 +0300  102)   if( output_fname )
11c606a6 (kx 2023-04-11 01:18:34 +0300  103)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  104)     if( output ) { (void)fflush( output ); fclose( output ); output = NULL; }
11c606a6 (kx 2023-04-11 01:18:34 +0300  105)     free( output_fname ); output_fname = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  106)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  107) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  108)   FREE_PKGLOG_VARIABLES();
11c606a6 (kx 2023-04-11 01:18:34 +0300  109) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  110) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  111) void usage()
11c606a6 (kx 2023-04-11 01:18:34 +0300  112) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  113)   free_resources();
11c606a6 (kx 2023-04-11 01:18:34 +0300  114) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  115)   fprintf( stdout, "\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  116)   fprintf( stdout, "Usage: %s [options] <dir|pkginfo|package>\n", program );
11c606a6 (kx 2023-04-11 01:18:34 +0300  117)   fprintf( stdout, "\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  118)   fprintf( stdout, "Read information from package's service files and create PKGLOG file\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  119)   fprintf( stdout, "in the destination directory. <pkginfo> is a full name of '.PKGINFO'\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  120)   fprintf( stdout, "service file of package.  Rest of package's  serfice files should be\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  121)   fprintf( stdout, "present in  the  directory of '.PKGINFO'.  If last argument is <dir>\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  122)   fprintf( stdout, "then pkglog utility will try to read '.PKGINFO' and rest of service\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  123)   fprintf( stdout, "files from directory given by <dir> argument.\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  124)   fprintf( stdout, "\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  125)   fprintf( stdout, "Options:\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  126)   fprintf( stdout, "  -h,--help                  Display this information.\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  127)   fprintf( stdout, "  -v,--version               Display the version of %s utility.\n", program );
11c606a6 (kx 2023-04-11 01:18:34 +0300  128)   fprintf( stdout, "  -d,--destination=<DIR>     Target directory to save output PKGLOG.\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  129)   fprintf( stdout, "  -m,--mkgroupdir            Create group subdirectory in the PKGLOG\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  130)   fprintf( stdout, "                             target directory.\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  131)   fprintf( stdout, "\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  132)   fprintf( stdout, "Parameter:\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  133)   fprintf( stdout, "  <dir|pkginfo|package>      Directory wich contains the package's\n"  );
11c606a6 (kx 2023-04-11 01:18:34 +0300  134)   fprintf( stdout, "                             service files or path to .PKGINFO file\n"  );
11c606a6 (kx 2023-04-11 01:18:34 +0300  135)   fprintf( stdout, "                             or package tarball.\n"  );
11c606a6 (kx 2023-04-11 01:18:34 +0300  136)   fprintf( stdout, "\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  137) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  138)   exit( EXIT_FAILURE );
11c606a6 (kx 2023-04-11 01:18:34 +0300  139) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  140) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  141) void to_lowercase( char *s )
11c606a6 (kx 2023-04-11 01:18:34 +0300  142) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  143)   char *p = s;
11c606a6 (kx 2023-04-11 01:18:34 +0300  144)   while( p && *p ) { int c = *p; *p = tolower( c ); ++p; }
11c606a6 (kx 2023-04-11 01:18:34 +0300  145) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  146) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  147) void to_uppercase( char *s )
11c606a6 (kx 2023-04-11 01:18:34 +0300  148) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  149)   char *p = s;
11c606a6 (kx 2023-04-11 01:18:34 +0300  150)   while( p && *p ) { int c = *p; *p = toupper( c ); ++p; }
11c606a6 (kx 2023-04-11 01:18:34 +0300  151) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  152) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  153) void version()
11c606a6 (kx 2023-04-11 01:18:34 +0300  154) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  155)   char *upper = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  156) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  157)   upper = (char *)alloca( strlen( program ) + 1 );
11c606a6 (kx 2023-04-11 01:18:34 +0300  158) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  159)   strcpy( (char *)upper, (const char *)program );
11c606a6 (kx 2023-04-11 01:18:34 +0300  160)   to_uppercase( upper );
11c606a6 (kx 2023-04-11 01:18:34 +0300  161) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  162)   fprintf( stdout, "%s (%s) %s\n", program, upper, PROGRAM_VERSION );
11c606a6 (kx 2023-04-11 01:18:34 +0300  163) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  164)   fprintf( stdout, "Copyright (C) 2019 Andrey V.Kosteltsev.\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  165)   fprintf( stdout, "This is free software.   There is NO warranty; not even\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  166)   fprintf( stdout, "for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  167)   fprintf( stdout, "\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  168) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  169)   free_resources();
11c606a6 (kx 2023-04-11 01:18:34 +0300  170)   exit( EXIT_SUCCESS );
11c606a6 (kx 2023-04-11 01:18:34 +0300  171) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  172) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  173) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  174) static void remove_trailing_slash( char *dir )
11c606a6 (kx 2023-04-11 01:18:34 +0300  175) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  176)   char *s;
11c606a6 (kx 2023-04-11 01:18:34 +0300  177) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  178)   if( !dir || dir[0] == '\0' ) return;
11c606a6 (kx 2023-04-11 01:18:34 +0300  179) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  180)   s = dir + strlen( dir ) - 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  181)   while( *s == '/' )
11c606a6 (kx 2023-04-11 01:18:34 +0300  182)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  183)     *s = '\0'; --s;
11c606a6 (kx 2023-04-11 01:18:34 +0300  184)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  185) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  186) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  187) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  188) static int _mkdir_p( const char *dir, const mode_t mode )
11c606a6 (kx 2023-04-11 01:18:34 +0300  189) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  190)   char  *buf;
11c606a6 (kx 2023-04-11 01:18:34 +0300  191)   char  *p = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  192)   struct stat sb;
11c606a6 (kx 2023-04-11 01:18:34 +0300  193) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  194)   if( !dir ) return -1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  195) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  196)   buf = (char *)alloca( strlen( dir ) + 1 );
11c606a6 (kx 2023-04-11 01:18:34 +0300  197)   strcpy( buf, dir );
11c606a6 (kx 2023-04-11 01:18:34 +0300  198) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  199)   remove_trailing_slash( buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300  200) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  201)   /* check if path exists and is a directory */
11c606a6 (kx 2023-04-11 01:18:34 +0300  202)   if( stat( buf, &sb ) == 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  203)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  204)     if( S_ISDIR(sb.st_mode) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  205)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  206)       return 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300  207)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  208)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  209) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  210)   /* mkdir -p */
11c606a6 (kx 2023-04-11 01:18:34 +0300  211)   for( p = buf + 1; *p; ++p )
11c606a6 (kx 2023-04-11 01:18:34 +0300  212)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  213)     if( *p == '/' )
11c606a6 (kx 2023-04-11 01:18:34 +0300  214)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  215)       *p = 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300  216)       /* test path */
11c606a6 (kx 2023-04-11 01:18:34 +0300  217)       if( stat( buf, &sb ) != 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  218)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  219)         /* path does not exist - create directory */
11c606a6 (kx 2023-04-11 01:18:34 +0300  220)         if( mkdir( buf, mode ) < 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  221)         {
11c606a6 (kx 2023-04-11 01:18:34 +0300  222)           return -1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  223)         }
11c606a6 (kx 2023-04-11 01:18:34 +0300  224)       } else if( !S_ISDIR(sb.st_mode) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  225)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  226)         /* not a directory */
11c606a6 (kx 2023-04-11 01:18:34 +0300  227)         return -1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  228)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  229)       *p = '/';
11c606a6 (kx 2023-04-11 01:18:34 +0300  230)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  231)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  232) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  233)   /* test path */
11c606a6 (kx 2023-04-11 01:18:34 +0300  234)   if( stat( buf, &sb ) != 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  235)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  236)     /* path does not exist - create directory */
11c606a6 (kx 2023-04-11 01:18:34 +0300  237)     if( mkdir( buf, mode ) < 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  238)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  239)       return -1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  240)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  241)   } else if( !S_ISDIR(sb.st_mode) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  242)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  243)     /* not a directory */
11c606a6 (kx 2023-04-11 01:18:34 +0300  244)     return -1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  245)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  246) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  247)   return 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300  248) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  249) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  250) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  251) static void _rm_tmpdir( const char *dirpath )
11c606a6 (kx 2023-04-11 01:18:34 +0300  252) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  253)   DIR    *dir;
11c606a6 (kx 2023-04-11 01:18:34 +0300  254)   char   *path;
11c606a6 (kx 2023-04-11 01:18:34 +0300  255)   size_t  len;
11c606a6 (kx 2023-04-11 01:18:34 +0300  256) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  257)   struct stat    path_sb, entry_sb;
11c606a6 (kx 2023-04-11 01:18:34 +0300  258)   struct dirent *entry;
11c606a6 (kx 2023-04-11 01:18:34 +0300  259) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  260)   if( stat( dirpath, &path_sb ) == -1 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  261)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  262)     return; /* stat returns error code; errno is set */
11c606a6 (kx 2023-04-11 01:18:34 +0300  263)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  264) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  265)   if( S_ISDIR(path_sb.st_mode) == 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  266)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  267)     return; /* dirpath is not a directory */
11c606a6 (kx 2023-04-11 01:18:34 +0300  268)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  269) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  270)   if( (dir = opendir(dirpath) ) == NULL )
11c606a6 (kx 2023-04-11 01:18:34 +0300  271)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  272)     return; /* Cannot open direcroty; errno is set */
11c606a6 (kx 2023-04-11 01:18:34 +0300  273)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  274) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  275)   len = strlen( dirpath );
11c606a6 (kx 2023-04-11 01:18:34 +0300  276) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  277)   while( (entry = readdir( dir )) != NULL)
11c606a6 (kx 2023-04-11 01:18:34 +0300  278)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  279) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  280)     /* skip entries '.' and '..' */
11c606a6 (kx 2023-04-11 01:18:34 +0300  281)     if( ! strcmp( entry->d_name, "." ) || ! strcmp( entry->d_name, ".." ) ) continue;
11c606a6 (kx 2023-04-11 01:18:34 +0300  282) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  283)     /* determinate a full name of an entry */
11c606a6 (kx 2023-04-11 01:18:34 +0300  284)     path = alloca( len + strlen( entry->d_name ) + 2 );
11c606a6 (kx 2023-04-11 01:18:34 +0300  285)     strcpy( path, dirpath );
11c606a6 (kx 2023-04-11 01:18:34 +0300  286)     strcat( path, "/" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  287)     strcat( path, entry->d_name );
11c606a6 (kx 2023-04-11 01:18:34 +0300  288) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  289)     if( stat( path, &entry_sb ) == 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  290)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  291)       if( S_ISDIR(entry_sb.st_mode) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  292)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  293)         /* recursively remove a nested directory */
11c606a6 (kx 2023-04-11 01:18:34 +0300  294)         _rm_tmpdir( path );
11c606a6 (kx 2023-04-11 01:18:34 +0300  295)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  296)       else
11c606a6 (kx 2023-04-11 01:18:34 +0300  297)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  298)         /* remove a file object */
11c606a6 (kx 2023-04-11 01:18:34 +0300  299)         (void)unlink( path );
11c606a6 (kx 2023-04-11 01:18:34 +0300  300)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  301)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  302)     /* else { stat() returns error code; errno is set; and we have to continue the loop } */
11c606a6 (kx 2023-04-11 01:18:34 +0300  303) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  304)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  305) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  306)   /* remove the devastated directory and close the object of this directory */
11c606a6 (kx 2023-04-11 01:18:34 +0300  307)   (void)rmdir( dirpath );
11c606a6 (kx 2023-04-11 01:18:34 +0300  308) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  309)   closedir( dir );
11c606a6 (kx 2023-04-11 01:18:34 +0300  310) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  311) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  312) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  313) static char *_mk_tmpdir( void )
11c606a6 (kx 2023-04-11 01:18:34 +0300  314) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  315)   char   *buf = NULL, *p, *tmp = "/tmp";
11c606a6 (kx 2023-04-11 01:18:34 +0300  316)   size_t  len = 0, size = 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300  317) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  318)   (void)umask( S_IWGRP | S_IWOTH ); /* octal 022 */
11c606a6 (kx 2023-04-11 01:18:34 +0300  319) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  320)   /* Get preferred directory for tmp files */
11c606a6 (kx 2023-04-11 01:18:34 +0300  321)   if( (p = getenv( "TMP" )) != NULL ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  322)     tmp = p;
11c606a6 (kx 2023-04-11 01:18:34 +0300  323)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  324)   else if( (p = getenv( "TEMP" )) != NULL ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  325)     tmp = p;
11c606a6 (kx 2023-04-11 01:18:34 +0300  326)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  327) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  328)   size = strlen( tmp ) + strlen( DISTRO_NAME ) + strlen( program ) + 12;
11c606a6 (kx 2023-04-11 01:18:34 +0300  329) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  330)   buf = (char *)malloc( size );
11c606a6 (kx 2023-04-11 01:18:34 +0300  331)   if( !buf ) return NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  332) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  333)   len = snprintf( buf, size, (const char *)"%s/%s/%s-%.7u", tmp, DISTRO_NAME, program, getpid() );
11c606a6 (kx 2023-04-11 01:18:34 +0300  334)   if( len == 0 || len == size - 1 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  335)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  336)     free( buf ); return NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  337)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  338) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  339)   _rm_tmpdir( (const char *)&buf[0] );
11c606a6 (kx 2023-04-11 01:18:34 +0300  340) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  341)   if( _mkdir_p( buf, S_IRWXU | S_IRWXG | S_IRWXO ) == 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  342)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  343)     return buf;
11c606a6 (kx 2023-04-11 01:18:34 +0300  344)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  345) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  346)   free( buf ); return NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  347) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  348) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  349) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  350) void fatal_error_actions( void )
11c606a6 (kx 2023-04-11 01:18:34 +0300  351) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  352)   logmsg( errlog, MSG_NOTICE, "Free resources on FATAL error..." );
11c606a6 (kx 2023-04-11 01:18:34 +0300  353)   if( rm_srcdir_at_exit ) _rm_tmpdir( (const char *)srcdir );
11c606a6 (kx 2023-04-11 01:18:34 +0300  354)   if( output_fname )
11c606a6 (kx 2023-04-11 01:18:34 +0300  355)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  356)     if( output ) { (void)fflush( output ); fclose( output ); output = NULL; }
11c606a6 (kx 2023-04-11 01:18:34 +0300  357)     (void)unlink( output_fname );
11c606a6 (kx 2023-04-11 01:18:34 +0300  358)     free( output_fname ); output_fname = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  359)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  360)   free_resources();
11c606a6 (kx 2023-04-11 01:18:34 +0300  361) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  362) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  363) void sigint( int signum )
11c606a6 (kx 2023-04-11 01:18:34 +0300  364) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  365)   (void)signum;
11c606a6 (kx 2023-04-11 01:18:34 +0300  366) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  367)   if( rm_srcdir_at_exit ) _rm_tmpdir( (const char *)srcdir );
11c606a6 (kx 2023-04-11 01:18:34 +0300  368)   if( output_fname )
11c606a6 (kx 2023-04-11 01:18:34 +0300  369)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  370)     if( output ) { (void)fflush( output ); fclose( output ); output = NULL; }
11c606a6 (kx 2023-04-11 01:18:34 +0300  371)     (void)unlink( output_fname );
11c606a6 (kx 2023-04-11 01:18:34 +0300  372)     free( output_fname ); output_fname = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  373)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  374)   free_resources();
11c606a6 (kx 2023-04-11 01:18:34 +0300  375) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  376) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  377) static void set_signal_handlers()
11c606a6 (kx 2023-04-11 01:18:34 +0300  378) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  379)   struct sigaction  sa;
11c606a6 (kx 2023-04-11 01:18:34 +0300  380)   sigset_t          set;
11c606a6 (kx 2023-04-11 01:18:34 +0300  381) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  382)   memset( &sa, 0, sizeof( sa ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300  383)   sa.sa_handler = sigint;          /* TERM, INT */
11c606a6 (kx 2023-04-11 01:18:34 +0300  384)   sa.sa_flags = SA_RESTART;
11c606a6 (kx 2023-04-11 01:18:34 +0300  385)   sigemptyset( &set );
11c606a6 (kx 2023-04-11 01:18:34 +0300  386)   sigaddset( &set, SIGTERM );
11c606a6 (kx 2023-04-11 01:18:34 +0300  387)   sigaddset( &set, SIGINT );
11c606a6 (kx 2023-04-11 01:18:34 +0300  388)   sa.sa_mask = set;
11c606a6 (kx 2023-04-11 01:18:34 +0300  389)   sigaction( SIGTERM, &sa, NULL );
11c606a6 (kx 2023-04-11 01:18:34 +0300  390)   sigaction( SIGINT, &sa,  NULL );
11c606a6 (kx 2023-04-11 01:18:34 +0300  391) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  392)   memset( &sa, 0, sizeof( sa ) );  /* ignore SIGPIPE */
11c606a6 (kx 2023-04-11 01:18:34 +0300  393)   sa.sa_handler = SIG_IGN;
11c606a6 (kx 2023-04-11 01:18:34 +0300  394)   sa.sa_flags = 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300  395)   sigaction( SIGPIPE, &sa, NULL );
11c606a6 (kx 2023-04-11 01:18:34 +0300  396) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  397)   /* System V fork+wait does not work if SIGCHLD is ignored */
11c606a6 (kx 2023-04-11 01:18:34 +0300  398)   signal( SIGCHLD, SIG_DFL );
11c606a6 (kx 2023-04-11 01:18:34 +0300  399) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  400) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  401) enum _pkginfo_type
11c606a6 (kx 2023-04-11 01:18:34 +0300  402) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  403)   PKGINFO_TEXT = 0,
11c606a6 (kx 2023-04-11 01:18:34 +0300  404)   PKGINFO_GZ,
11c606a6 (kx 2023-04-11 01:18:34 +0300  405)   PKGINFO_BZ2,
11c606a6 (kx 2023-04-11 01:18:34 +0300  406)   PKGINFO_XZ,
11c606a6 (kx 2023-04-11 01:18:34 +0300  407)   PKGINFO_TAR,
11c606a6 (kx 2023-04-11 01:18:34 +0300  408) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  409)   PKGINFO_UNKNOWN
11c606a6 (kx 2023-04-11 01:18:34 +0300  410) };
11c606a6 (kx 2023-04-11 01:18:34 +0300  411) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  412) static enum _pkginfo_type pkginfo_type = PKGINFO_UNKNOWN;
11c606a6 (kx 2023-04-11 01:18:34 +0300  413) static char uncompress[2] = { 0, 0 };
11c606a6 (kx 2023-04-11 01:18:34 +0300  414) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  415) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  416) static enum _pkginfo_type check_pkginfo_file( const char *fname )
11c606a6 (kx 2023-04-11 01:18:34 +0300  417) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  418)   struct stat st;
11c606a6 (kx 2023-04-11 01:18:34 +0300  419)   size_t pkginfo_size = 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300  420)   unsigned char buf[8];
11c606a6 (kx 2023-04-11 01:18:34 +0300  421)   int rc, fd;
11c606a6 (kx 2023-04-11 01:18:34 +0300  422) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  423)   /* SIGNATURES: https://www.garykessler.net/library/file_sigs.html */
11c606a6 (kx 2023-04-11 01:18:34 +0300  424) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  425)   uncompress[0] = '\0';
11c606a6 (kx 2023-04-11 01:18:34 +0300  426) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  427)   if( stat( fname, &st ) == -1 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  428)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  429)     FATAL_ERROR( "Cannot access %s file: %s", basename( (char *)fname ), strerror( errno ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300  430)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  431) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  432)   pkginfo_size = st.st_size;
11c606a6 (kx 2023-04-11 01:18:34 +0300  433) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  434)   if( (fd = open( fname, O_RDONLY )) == -1 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  435)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  436)     FATAL_ERROR( "Cannot open %s file: %s", basename( (char *)fname ), strerror( errno ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300  437)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  438) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  439)   rc = (int)read( fd, (void *)&buf[0], 7 );
11c606a6 (kx 2023-04-11 01:18:34 +0300  440)   if( rc != 7 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  441)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  442)     FATAL_ERROR( "Unknown type of input file %s", basename( (char *)fname ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300  443)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  444)   buf[7] = '\0';
11c606a6 (kx 2023-04-11 01:18:34 +0300  445) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  446)   /* TEXT */
11c606a6 (kx 2023-04-11 01:18:34 +0300  447)   if( !strncmp( (const char *)&buf[0], "pkgname", 7 ) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  448)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  449)     close( fd ); return PKGINFO_TEXT;
11c606a6 (kx 2023-04-11 01:18:34 +0300  450)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  451) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  452)   /* GZ */
11c606a6 (kx 2023-04-11 01:18:34 +0300  453)   if( buf[0] == 0x1F && buf[1] == 0x8B && buf[2] == 0x08 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  454)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  455)     uncompress[0] = 'x';
11c606a6 (kx 2023-04-11 01:18:34 +0300  456)     close( fd ); return PKGINFO_GZ;
11c606a6 (kx 2023-04-11 01:18:34 +0300  457)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  458) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  459)   /* BZ2 */
11c606a6 (kx 2023-04-11 01:18:34 +0300  460)   if( buf[0] == 0x42 && buf[1] == 0x5A && buf[2] == 0x68 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  461)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  462)     uncompress[0] = 'j';
11c606a6 (kx 2023-04-11 01:18:34 +0300  463)     close( fd ); return PKGINFO_BZ2;
11c606a6 (kx 2023-04-11 01:18:34 +0300  464)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  465) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  466)   /* XZ */
11c606a6 (kx 2023-04-11 01:18:34 +0300  467)   if( buf[0] == 0xFD && buf[1] == 0x37 && buf[2] == 0x7A &&
11c606a6 (kx 2023-04-11 01:18:34 +0300  468)       buf[3] == 0x58 && buf[4] == 0x5A && buf[5] == 0x00   )
11c606a6 (kx 2023-04-11 01:18:34 +0300  469)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  470)     uncompress[0] = 'J';
11c606a6 (kx 2023-04-11 01:18:34 +0300  471)     close( fd ); return PKGINFO_XZ;
11c606a6 (kx 2023-04-11 01:18:34 +0300  472)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  473) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  474)   if( pkginfo_size > 262 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  475)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  476)     if( lseek( fd, 257, SEEK_SET ) == -1 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  477)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  478)       FATAL_ERROR( "Cannot check signature of %s file: %s", basename( (char *)fname ), strerror( errno ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300  479)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  480)     rc = (int)read( fd, &buf[0], 5 );
11c606a6 (kx 2023-04-11 01:18:34 +0300  481)     if( rc != 5 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  482)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  483)       FATAL_ERROR( "Cannot read signature of %s file", basename( (char *)fname ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300  484)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  485)     /* TAR */
11c606a6 (kx 2023-04-11 01:18:34 +0300  486)     if( buf[0] == 0x75 && buf[1] == 0x73 && buf[2] == 0x74 && buf[3] == 0x61 && buf[4] == 0x72 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  487)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  488)       close( fd ); return PKGINFO_TAR;
11c606a6 (kx 2023-04-11 01:18:34 +0300  489)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  490)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  491) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  492)   close( fd ); return PKGINFO_UNKNOWN;
11c606a6 (kx 2023-04-11 01:18:34 +0300  493) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  494) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  495) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  496) void get_args( int argc, char *argv[] )
11c606a6 (kx 2023-04-11 01:18:34 +0300  497) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  498)   const char* short_options = "hvmd:";
11c606a6 (kx 2023-04-11 01:18:34 +0300  499) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  500)   const struct option long_options[] =
11c606a6 (kx 2023-04-11 01:18:34 +0300  501)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  502)     { "help",        no_argument,       NULL, 'h' },
11c606a6 (kx 2023-04-11 01:18:34 +0300  503)     { "version",     no_argument,       NULL, 'v' },
11c606a6 (kx 2023-04-11 01:18:34 +0300  504)     { "destination", required_argument, NULL, 'd' },
11c606a6 (kx 2023-04-11 01:18:34 +0300  505)     { "mkgroupdir",  no_argument,       NULL, 'm' },
11c606a6 (kx 2023-04-11 01:18:34 +0300  506)     { NULL,          0,                 NULL,  0  }
11c606a6 (kx 2023-04-11 01:18:34 +0300  507)   };
11c606a6 (kx 2023-04-11 01:18:34 +0300  508) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  509)   int ret;
11c606a6 (kx 2023-04-11 01:18:34 +0300  510)   int option_index = 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300  511) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  512)   while( (ret = getopt_long( argc, argv, short_options, long_options, &option_index )) != -1 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  513)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  514)     switch( ret )
11c606a6 (kx 2023-04-11 01:18:34 +0300  515)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  516)       case 'h':
11c606a6 (kx 2023-04-11 01:18:34 +0300  517)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  518)         usage();
11c606a6 (kx 2023-04-11 01:18:34 +0300  519)         break;
11c606a6 (kx 2023-04-11 01:18:34 +0300  520)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  521)       case 'v':
11c606a6 (kx 2023-04-11 01:18:34 +0300  522)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  523)         version();
11c606a6 (kx 2023-04-11 01:18:34 +0300  524)         break;
11c606a6 (kx 2023-04-11 01:18:34 +0300  525)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  526) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  527)       case 'd':
11c606a6 (kx 2023-04-11 01:18:34 +0300  528)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  529)         if( optarg != NULL )
11c606a6 (kx 2023-04-11 01:18:34 +0300  530)         {
11c606a6 (kx 2023-04-11 01:18:34 +0300  531)           destination = xstrdup( (const char *)optarg );
11c606a6 (kx 2023-04-11 01:18:34 +0300  532)           remove_trailing_slash( destination );
11c606a6 (kx 2023-04-11 01:18:34 +0300  533)         }
11c606a6 (kx 2023-04-11 01:18:34 +0300  534)         else
11c606a6 (kx 2023-04-11 01:18:34 +0300  535)           /* option is present but without value */
11c606a6 (kx 2023-04-11 01:18:34 +0300  536)           usage();
11c606a6 (kx 2023-04-11 01:18:34 +0300  537)         break;
11c606a6 (kx 2023-04-11 01:18:34 +0300  538)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  539)       case 'm':
11c606a6 (kx 2023-04-11 01:18:34 +0300  540)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  541)         mkgroupdir = 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  542)         break;
11c606a6 (kx 2023-04-11 01:18:34 +0300  543)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  544) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  545)       case '?': default:
11c606a6 (kx 2023-04-11 01:18:34 +0300  546)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  547)         usage();
11c606a6 (kx 2023-04-11 01:18:34 +0300  548)         break;
11c606a6 (kx 2023-04-11 01:18:34 +0300  549)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  550)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  551)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  552) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  553)   if( destination == NULL )
11c606a6 (kx 2023-04-11 01:18:34 +0300  554)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  555)     char cwd[PATH_MAX];
11c606a6 (kx 2023-04-11 01:18:34 +0300  556)     if( getcwd( cwd, sizeof(cwd) ) != NULL )
11c606a6 (kx 2023-04-11 01:18:34 +0300  557)       destination = xstrdup( (const char *)cwd );
11c606a6 (kx 2023-04-11 01:18:34 +0300  558)     else
11c606a6 (kx 2023-04-11 01:18:34 +0300  559)       destination = xstrdup( "." );
11c606a6 (kx 2023-04-11 01:18:34 +0300  560)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  561) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  562)   /* last command line argument is the PKGLOG file */
11c606a6 (kx 2023-04-11 01:18:34 +0300  563)   if( optind < argc )
11c606a6 (kx 2023-04-11 01:18:34 +0300  564)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  565)     struct stat st;
11c606a6 (kx 2023-04-11 01:18:34 +0300  566)     char  *buf = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  567) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  568)     buf = (char *)malloc( strlen( (const char *)argv[optind] ) + 10 );
11c606a6 (kx 2023-04-11 01:18:34 +0300  569)     if( !buf )
11c606a6 (kx 2023-04-11 01:18:34 +0300  570)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  571)       FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  572)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  573) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  574)     (void)strcpy( buf, (const char *)argv[optind++] );
11c606a6 (kx 2023-04-11 01:18:34 +0300  575)     remove_trailing_slash( (char *)&buf[0] );
11c606a6 (kx 2023-04-11 01:18:34 +0300  576) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  577)     if( stat( (const char *)&buf[0], &st ) == -1 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  578)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  579)       FATAL_ERROR( "Cannot access '%s' file: %s", basename( buf ), strerror( errno ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300  580)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  581) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  582)     if( S_ISDIR(st.st_mode) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  583)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  584)       /* Add .PKGINFO to the input dir name: */
11c606a6 (kx 2023-04-11 01:18:34 +0300  585)       (void)strcat( buf, "/.PKGINFO" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  586)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  587) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  588)     pkginfo_fname = xstrdup( (const char *)&buf[0] );
11c606a6 (kx 2023-04-11 01:18:34 +0300  589)     if( pkginfo_fname == NULL )
11c606a6 (kx 2023-04-11 01:18:34 +0300  590)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  591)       usage();
11c606a6 (kx 2023-04-11 01:18:34 +0300  592)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  593) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  594)     free( buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300  595) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  596)     pkginfo_type = check_pkginfo_file( (const char *)pkginfo_fname );
11c606a6 (kx 2023-04-11 01:18:34 +0300  597)     if( pkginfo_type == PKGINFO_UNKNOWN )
11c606a6 (kx 2023-04-11 01:18:34 +0300  598)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  599)       ERROR( "%s: Unknown input file format", basename( pkginfo_fname ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300  600)       usage();
11c606a6 (kx 2023-04-11 01:18:34 +0300  601)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  602)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  603)   else
11c606a6 (kx 2023-04-11 01:18:34 +0300  604)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  605)     usage();
11c606a6 (kx 2023-04-11 01:18:34 +0300  606)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  607) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  608) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  609) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  610) /*
11c606a6 (kx 2023-04-11 01:18:34 +0300  611)   Especialy for pkginfo lines.
11c606a6 (kx 2023-04-11 01:18:34 +0300  612)   Remove leading spaces and take non-space characters only:
11c606a6 (kx 2023-04-11 01:18:34 +0300  613)  */
11c606a6 (kx 2023-04-11 01:18:34 +0300  614) static char *skip_spaces( char *s )
11c606a6 (kx 2023-04-11 01:18:34 +0300  615) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  616)   char *q, *p = (char *)0;
11c606a6 (kx 2023-04-11 01:18:34 +0300  617) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  618)   if( !s || *s == '\0' ) return p;
11c606a6 (kx 2023-04-11 01:18:34 +0300  619) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  620)   p = s;
11c606a6 (kx 2023-04-11 01:18:34 +0300  621) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  622)   while( (*p == ' ' || *p == '\t') && *p != '\0' ) { ++p; } q = p;
11c606a6 (kx 2023-04-11 01:18:34 +0300  623)   while(  *q != ' ' && *q != '\t'  && *q != '\0' ) { ++q; } *q = '\0';
11c606a6 (kx 2023-04-11 01:18:34 +0300  624) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  625)   if( *p == '\0' ) return (char *)0;
11c606a6 (kx 2023-04-11 01:18:34 +0300  626) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  627)   return( xstrdup( (const char *)p ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300  628) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  629) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  630) /*
11c606a6 (kx 2023-04-11 01:18:34 +0300  631)   remove spaces at end of line:
11c606a6 (kx 2023-04-11 01:18:34 +0300  632)  */
11c606a6 (kx 2023-04-11 01:18:34 +0300  633) static void skip_eol_spaces( char *s )
11c606a6 (kx 2023-04-11 01:18:34 +0300  634) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  635)   char *p = (char *)0;
11c606a6 (kx 2023-04-11 01:18:34 +0300  636) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  637)   if( !s || *s == '\0' ) return;
11c606a6 (kx 2023-04-11 01:18:34 +0300  638) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  639)   p = s + strlen( s ) - 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  640)   while( isspace( *p ) ) { *p-- = '\0'; }
11c606a6 (kx 2023-04-11 01:18:34 +0300  641) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  642) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  643) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  644) void write_pkginfo( void )
11c606a6 (kx 2023-04-11 01:18:34 +0300  645) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  646)   char *ln   = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  647)   char *line = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  648) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  649)   if( pkginfo_fname != NULL )
11c606a6 (kx 2023-04-11 01:18:34 +0300  650)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  651)     pkginfo = fopen( (const char *)pkginfo_fname, "r" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  652)     if( !pkginfo )
11c606a6 (kx 2023-04-11 01:18:34 +0300  653)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  654)       FATAL_ERROR( "Cannot open %s file", pkginfo_fname );
11c606a6 (kx 2023-04-11 01:18:34 +0300  655)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  656)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  657) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  658)   line = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300  659)   if( !line )
11c606a6 (kx 2023-04-11 01:18:34 +0300  660)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  661)     FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  662)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  663) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  664)   while( (ln = fgets( line, PATH_MAX, pkginfo )) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  665)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  666)     char *match = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  667) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  668)     ln[strlen(ln) - 1] = '\0'; /* replace new-line symbol      */
11c606a6 (kx 2023-04-11 01:18:34 +0300  669)     skip_eol_spaces( ln );     /* remove spaces at end-of-line */
11c606a6 (kx 2023-04-11 01:18:34 +0300  670) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  671)     if( (match = strstr( ln, "pkgname" )) && match == ln ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  672)       char *p = index( match, '=' ) + 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  673)       if( p != NULL ) pkgname = skip_spaces( p );
11c606a6 (kx 2023-04-11 01:18:34 +0300  674)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  675)     if( (match = strstr( ln, "pkgver" )) && match == ln ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  676)       char *p = index( match, '=' ) + 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  677)       if( p != NULL ) pkgver = skip_spaces( p );
11c606a6 (kx 2023-04-11 01:18:34 +0300  678)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  679)     if( (match = strstr( ln, "arch" )) && match == ln ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  680)       char *p = index( match, '=' ) + 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  681)       if( p != NULL ) arch = skip_spaces( p );
11c606a6 (kx 2023-04-11 01:18:34 +0300  682)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  683)     if( (match = strstr( ln, "distroname" )) && match == ln ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  684)       char *p = index( match, '=' ) + 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  685)       if( p != NULL ) distroname = skip_spaces( p );
11c606a6 (kx 2023-04-11 01:18:34 +0300  686)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  687)     if( (match = strstr( ln, "distrover" )) && match == ln ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  688)       char *p = index( match, '=' ) + 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  689)       if( p != NULL ) distrover = skip_spaces( p );
11c606a6 (kx 2023-04-11 01:18:34 +0300  690)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  691) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  692) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  693)     if( (match = strstr( ln, "group" )) && match == ln ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  694)       char *p = index( match, '=' ) + 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  695)       if( p != NULL ) group = skip_spaces( p );
11c606a6 (kx 2023-04-11 01:18:34 +0300  696)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  697)     /* variable short_description="..." is not stored in the PKGLOG file */
11c606a6 (kx 2023-04-11 01:18:34 +0300  698)     if( (match = strstr( ln, "url" )) && match == ln ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  699)       char *p = index( match, '=' ) + 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  700)       if( p != NULL ) url = skip_spaces( p );
11c606a6 (kx 2023-04-11 01:18:34 +0300  701)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  702)     if( (match = strstr( ln, "license" )) && match == ln ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  703)       char *p = index( match, '=' ) + 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  704)       if( p != NULL ) license = skip_spaces( p );
11c606a6 (kx 2023-04-11 01:18:34 +0300  705)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  706)     if( (match = strstr( ln, "uncompressed_size" )) && match == ln ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  707)       char *p = index( match, '=' ) + 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  708)       if( p != NULL ) uncompressed_size = skip_spaces( p );
11c606a6 (kx 2023-04-11 01:18:34 +0300  709)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  710)     if( (match = strstr( ln, "total_files" )) && match == ln ) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  711)       char *p = index( match, '=' ) + 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300  712)       if( p != NULL ) total_files = skip_spaces( p );
11c606a6 (kx 2023-04-11 01:18:34 +0300  713)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  714)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  715) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  716)   free( line );
11c606a6 (kx 2023-04-11 01:18:34 +0300  717) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  718)   if( pkgname && pkgver && arch && distroname && distrover )
11c606a6 (kx 2023-04-11 01:18:34 +0300  719)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  720)     int   len;
11c606a6 (kx 2023-04-11 01:18:34 +0300  721)     char *buf = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  722) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  723)     buf = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300  724)     if( !buf )
11c606a6 (kx 2023-04-11 01:18:34 +0300  725)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  726)       FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  727)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  728) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  729)     if( mkgroupdir && group )
11c606a6 (kx 2023-04-11 01:18:34 +0300  730)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  731)       len = snprintf( buf, PATH_MAX, "%s/%s",
11c606a6 (kx 2023-04-11 01:18:34 +0300  732)                                       destination, group );
11c606a6 (kx 2023-04-11 01:18:34 +0300  733)       if( len == 0 || len == PATH_MAX - 1 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  734)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  735)         FATAL_ERROR( "Cannot create output file" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  736)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  737) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  738)       if( _mkdir_p( buf, S_IRWXU | S_IRWXG | S_IRWXO ) != 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  739)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  740)         FATAL_ERROR( "Cannot create output directory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  741)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  742) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  743)       len = snprintf( buf, PATH_MAX, "%s/%s/%s-%s-%s-%s-%s",
11c606a6 (kx 2023-04-11 01:18:34 +0300  744)                                       destination, group, pkgname, pkgver, arch, distroname, distrover );
11c606a6 (kx 2023-04-11 01:18:34 +0300  745)       if( len == 0 || len == PATH_MAX - 1 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  746)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  747)         FATAL_ERROR( "Cannot create output file" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  748)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  749)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  750)     else
11c606a6 (kx 2023-04-11 01:18:34 +0300  751)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  752)       len = snprintf( buf, PATH_MAX, "%s/%s-%s-%s-%s-%s",
11c606a6 (kx 2023-04-11 01:18:34 +0300  753)                                       destination, pkgname, pkgver, arch, distroname, distrover );
11c606a6 (kx 2023-04-11 01:18:34 +0300  754)       if( len == 0 || len == PATH_MAX - 1 )
11c606a6 (kx 2023-04-11 01:18:34 +0300  755)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  756)         FATAL_ERROR( "Cannot create output file" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  757)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  758)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  759)     output_fname = xstrdup( (const char *)&buf[0] );
11c606a6 (kx 2023-04-11 01:18:34 +0300  760)     if( output_fname )
11c606a6 (kx 2023-04-11 01:18:34 +0300  761)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  762)       output = fopen( (const char *)output_fname, "w" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  763)       if( !output )
11c606a6 (kx 2023-04-11 01:18:34 +0300  764)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  765)         FATAL_ERROR( "Cannot create %s file", output_fname );
11c606a6 (kx 2023-04-11 01:18:34 +0300  766)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  767)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  768) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  769)     free( buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300  770) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  771)     fprintf( output, "PACKAGE NAME: %s\n",    pkgname    );
11c606a6 (kx 2023-04-11 01:18:34 +0300  772)     fprintf( output, "PACKAGE VERSION: %s\n", pkgver     );
11c606a6 (kx 2023-04-11 01:18:34 +0300  773)     fprintf( output, "ARCH: %s\n",            arch       );
11c606a6 (kx 2023-04-11 01:18:34 +0300  774)     fprintf( output, "DISTRO: %s\n",          distroname );
11c606a6 (kx 2023-04-11 01:18:34 +0300  775)     fprintf( output, "DISTRO VERSION: %s\n",  distrover  );
11c606a6 (kx 2023-04-11 01:18:34 +0300  776)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  777)   else
11c606a6 (kx 2023-04-11 01:18:34 +0300  778)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  779)     FATAL_ERROR( "Invalid input .PKGINFO file" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  780)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  781) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  782)   if( group )
11c606a6 (kx 2023-04-11 01:18:34 +0300  783)     fprintf( output, "GROUP: %s\n", group );
11c606a6 (kx 2023-04-11 01:18:34 +0300  784)   if( url )
11c606a6 (kx 2023-04-11 01:18:34 +0300  785)     fprintf( output, "URL: %s\n", url );
11c606a6 (kx 2023-04-11 01:18:34 +0300  786)   if( license )
11c606a6 (kx 2023-04-11 01:18:34 +0300  787)     fprintf( output, "LICENSE: %s\n", license );
11c606a6 (kx 2023-04-11 01:18:34 +0300  788)   if( uncompressed_size )
11c606a6 (kx 2023-04-11 01:18:34 +0300  789)     fprintf( output, "UNCOMPRESSED SIZE: %s\n", uncompressed_size );
11c606a6 (kx 2023-04-11 01:18:34 +0300  790)   if( total_files )
11c606a6 (kx 2023-04-11 01:18:34 +0300  791)     fprintf( output, "TOTAL FILES: %s\n", total_files );
11c606a6 (kx 2023-04-11 01:18:34 +0300  792) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  793)   /* reference counter of not installed package is always zero */
11c606a6 (kx 2023-04-11 01:18:34 +0300  794)   fprintf( output, "REFERENCE COUNTER: 0\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  795) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  796) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  797) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  798) void write_requires( void )
11c606a6 (kx 2023-04-11 01:18:34 +0300  799) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  800)   struct stat sb;
11c606a6 (kx 2023-04-11 01:18:34 +0300  801)   char  *buf = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  802) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  803)   if( output == NULL && output_fname == NULL )
11c606a6 (kx 2023-04-11 01:18:34 +0300  804)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  805)     FATAL_ERROR( "Unable to access output file" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  806)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  807) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  808)   buf = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300  809)   if( !buf )
11c606a6 (kx 2023-04-11 01:18:34 +0300  810)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  811)     FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  812)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  813) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  814)   fprintf( output, "REQUIRES:\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  815) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  816)   bzero( (void *)buf, PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300  817)   (void)sprintf( (char *)&buf[0], "%s/.REQUIRES", srcdir );
11c606a6 (kx 2023-04-11 01:18:34 +0300  818) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  819)   /* check if path exists and is a regular file */
11c606a6 (kx 2023-04-11 01:18:34 +0300  820)   if( stat( (const char *)&buf[0], &sb ) == 0 && S_ISREG(sb.st_mode) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  821)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  822)     char *ln   = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  823)     char *line = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  824) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  825)     FILE *input;
11c606a6 (kx 2023-04-11 01:18:34 +0300  826) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  827)     input = fopen( (const char *)&buf[0], "r" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  828)     if( !input )
11c606a6 (kx 2023-04-11 01:18:34 +0300  829)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  830)       FATAL_ERROR( "Unable to access %s file", (char *)&buf[0] );
11c606a6 (kx 2023-04-11 01:18:34 +0300  831)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  832) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  833)     line = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300  834)     if( !line )
11c606a6 (kx 2023-04-11 01:18:34 +0300  835)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  836)       FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  837)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  838) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  839)     /* cat .REQUIRES >> PKGLOG */
11c606a6 (kx 2023-04-11 01:18:34 +0300  840)     while( (ln = fgets( line, PATH_MAX, input )) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  841)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  842)       ln[strlen(ln) - 1] = '\0'; /* replace new-line symbol      */
11c606a6 (kx 2023-04-11 01:18:34 +0300  843)       skip_eol_spaces( ln );     /* remove spaces at end-of-line */
11c606a6 (kx 2023-04-11 01:18:34 +0300  844) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  845)       /* print non-empty lines */
11c606a6 (kx 2023-04-11 01:18:34 +0300  846)       if( *ln )
11c606a6 (kx 2023-04-11 01:18:34 +0300  847)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  848)         fprintf( output, "%s\n", ln );
11c606a6 (kx 2023-04-11 01:18:34 +0300  849)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  850)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  851)     free( line );
11c606a6 (kx 2023-04-11 01:18:34 +0300  852)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  853)   free( buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300  854) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  855) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  856) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  857) void write_description( void )
11c606a6 (kx 2023-04-11 01:18:34 +0300  858) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  859)   struct stat sb;
11c606a6 (kx 2023-04-11 01:18:34 +0300  860)   char  *buf = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  861) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  862)   if( output == NULL && output_fname == NULL )
11c606a6 (kx 2023-04-11 01:18:34 +0300  863)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  864)     FATAL_ERROR( "Unable to access output file" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  865)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  866) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  867)   buf = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300  868)   if( !buf )
11c606a6 (kx 2023-04-11 01:18:34 +0300  869)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  870)     FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  871)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  872) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  873)   fprintf( output, "PACKAGE DESCRIPTION:\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  874) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  875)   bzero( (void *)buf, PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300  876)   (void)sprintf( (char *)&buf[0], "%s/.DESCRIPTION", srcdir );
11c606a6 (kx 2023-04-11 01:18:34 +0300  877) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  878)   /* check if path exists and is a regular file */
11c606a6 (kx 2023-04-11 01:18:34 +0300  879)   if( stat( (const char *)&buf[0], &sb ) == 0 && S_ISREG(sb.st_mode) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  880)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  881)     char *ln      = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  882)     char *line    = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  883)     char *pattern = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  884)     int   n = 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300  885) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  886)     FILE *input;
11c606a6 (kx 2023-04-11 01:18:34 +0300  887) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  888)     input = fopen( (const char *)&buf[0], "r" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  889)     if( !input )
11c606a6 (kx 2023-04-11 01:18:34 +0300  890)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  891)       FATAL_ERROR( "Unable to access %s file", (char *)&buf[0] );
11c606a6 (kx 2023-04-11 01:18:34 +0300  892)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  893) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  894)     line = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300  895)     if( !line )    { FATAL_ERROR( "Cannot allocate memory" ); }
11c606a6 (kx 2023-04-11 01:18:34 +0300  896) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  897)     pattern = (char *)malloc( (size_t)strlen( pkgname ) + 2 );
11c606a6 (kx 2023-04-11 01:18:34 +0300  898)     if( !pattern ) { FATAL_ERROR( "Cannot allocate memory" ); }
11c606a6 (kx 2023-04-11 01:18:34 +0300  899) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  900)     (void)sprintf( pattern, "%s:", pkgname );
11c606a6 (kx 2023-04-11 01:18:34 +0300  901) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  902)     /* cat .DESCRIPTION >> PKGLOG */
11c606a6 (kx 2023-04-11 01:18:34 +0300  903)     while( (ln = fgets( line, PATH_MAX, input )) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  904)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  905)       char *match = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  906) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  907)       ln[strlen(ln) - 1] = '\0'; /* replace new-line symbol      */
11c606a6 (kx 2023-04-11 01:18:34 +0300  908)       skip_eol_spaces( ln );     /* remove spaces at end-of-line */
11c606a6 (kx 2023-04-11 01:18:34 +0300  909) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  910)       /*
11c606a6 (kx 2023-04-11 01:18:34 +0300  911)         skip non-significant spaces at beginning of line
11c606a6 (kx 2023-04-11 01:18:34 +0300  912)         and print lines started with 'pkgname:'
11c606a6 (kx 2023-04-11 01:18:34 +0300  913)        */
11c606a6 (kx 2023-04-11 01:18:34 +0300  914)       if( (match = strstr( ln, pattern )) && n < DESCRIPTION_NUMBER_OF_LINES )
11c606a6 (kx 2023-04-11 01:18:34 +0300  915)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  916)         int mlen   = strlen( match ), plen = strlen( pattern );
11c606a6 (kx 2023-04-11 01:18:34 +0300  917)         int length = ( mlen > plen )  ? (mlen - plen - 1) : 0 ;
11c606a6 (kx 2023-04-11 01:18:34 +0300  918) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  919)         if( length > DESCRIPTION_LENGTH_OF_LINE )
11c606a6 (kx 2023-04-11 01:18:34 +0300  920)         {
11c606a6 (kx 2023-04-11 01:18:34 +0300  921)           /* WARNING( "Package DESCRIPTION contains lines with length greater than %d characters", DESCRIPTION_LENGTH_OF_LINE ); */
11c606a6 (kx 2023-04-11 01:18:34 +0300  922)           match[plen + 1 + DESCRIPTION_LENGTH_OF_LINE] = '\0'; /* truncating description line  */
11c606a6 (kx 2023-04-11 01:18:34 +0300  923)           skip_eol_spaces( match );                            /* remove spaces at end-of-line */
11c606a6 (kx 2023-04-11 01:18:34 +0300  924)         }
11c606a6 (kx 2023-04-11 01:18:34 +0300  925)         fprintf( output, "%s\n", match );
11c606a6 (kx 2023-04-11 01:18:34 +0300  926)         ++n;
11c606a6 (kx 2023-04-11 01:18:34 +0300  927)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  928)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  929) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  930)     if( n < DESCRIPTION_NUMBER_OF_LINES )
11c606a6 (kx 2023-04-11 01:18:34 +0300  931)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  932)       /* WARNING( "Package DESCRIPTION contains less than %d lines", DESCRIPTION_NUMBER_OF_LINES ); */
11c606a6 (kx 2023-04-11 01:18:34 +0300  933)       while( n < DESCRIPTION_NUMBER_OF_LINES )
11c606a6 (kx 2023-04-11 01:18:34 +0300  934)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  935)         fprintf( output, "%s\n", pattern );
11c606a6 (kx 2023-04-11 01:18:34 +0300  936)         ++n;
11c606a6 (kx 2023-04-11 01:18:34 +0300  937)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300  938)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  939) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  940)     free( pattern );
11c606a6 (kx 2023-04-11 01:18:34 +0300  941)     free( line );
11c606a6 (kx 2023-04-11 01:18:34 +0300  942)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  943) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  944)   free( buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300  945) }
11c606a6 (kx 2023-04-11 01:18:34 +0300  946) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  947) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  948) void write_restore_links( void )
11c606a6 (kx 2023-04-11 01:18:34 +0300  949) {
11c606a6 (kx 2023-04-11 01:18:34 +0300  950)   struct stat sb;
11c606a6 (kx 2023-04-11 01:18:34 +0300  951)   char  *buf = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  952) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  953)   if( output == NULL && output_fname == NULL )
11c606a6 (kx 2023-04-11 01:18:34 +0300  954)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  955)     FATAL_ERROR( "Unable to access output file" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  956)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  957) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  958)   buf = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300  959)   if( !buf )
11c606a6 (kx 2023-04-11 01:18:34 +0300  960)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  961)     FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  962)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300  963) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  964)   fprintf( output, "RESTORE LINKS:\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  965) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  966)   bzero( (void *)buf, PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300  967)   (void)sprintf( (char *)&buf[0], "%s/.RESTORELINKS", srcdir );
11c606a6 (kx 2023-04-11 01:18:34 +0300  968) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  969)   /* check if path exists and is a regular file */
11c606a6 (kx 2023-04-11 01:18:34 +0300  970)   if( stat( (const char *)&buf[0], &sb ) == 0 && S_ISREG(sb.st_mode) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  971)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300  972)     char *ln   = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  973)     char *line = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300  974) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  975)     FILE *input;
11c606a6 (kx 2023-04-11 01:18:34 +0300  976) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  977)     input = fopen( (const char *)&buf[0], "r" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  978)     if( !input )
11c606a6 (kx 2023-04-11 01:18:34 +0300  979)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  980)       FATAL_ERROR( "Unable to access %s file", (char *)&buf[0] );
11c606a6 (kx 2023-04-11 01:18:34 +0300  981)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  982) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  983)     line = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300  984)     if( !line )
11c606a6 (kx 2023-04-11 01:18:34 +0300  985)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  986)       FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300  987)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300  988) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  989)     /* cat .REQUIRES >> PKGLOG */
11c606a6 (kx 2023-04-11 01:18:34 +0300  990)     while( (ln = fgets( line, PATH_MAX, input )) )
11c606a6 (kx 2023-04-11 01:18:34 +0300  991)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300  992)       ln[strlen(ln) - 1] = '\0'; /* replace new-line symbol      */
11c606a6 (kx 2023-04-11 01:18:34 +0300  993)       skip_eol_spaces( ln );     /* remove spaces at end-of-line */
11c606a6 (kx 2023-04-11 01:18:34 +0300  994) 
11c606a6 (kx 2023-04-11 01:18:34 +0300  995)       /* print non-empty lines */
11c606a6 (kx 2023-04-11 01:18:34 +0300  996)       if( *ln )
11c606a6 (kx 2023-04-11 01:18:34 +0300  997)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300  998)         fprintf( output, "%s\n", ln );
11c606a6 (kx 2023-04-11 01:18:34 +0300  999)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1000)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1001) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1002)     free( line );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1003)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1004) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1005)   free( buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1006) }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1007) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1008) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1009) void write_install_script( void )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1010) {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1011)   struct stat sb;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1012)   char  *buf = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1013) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1014)   if( output == NULL && output_fname == NULL )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1015)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1016)     FATAL_ERROR( "Unable to access output file" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1017)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1018) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1019)   buf = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1020)   if( !buf )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1021)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1022)     FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1023)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1024) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1025)   fprintf( output, "INSTALL SCRIPT:\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1026) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1027)   bzero( (void *)buf, PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1028)   (void)sprintf( (char *)&buf[0], "%s/.INSTALL", srcdir );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1029) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1030)   /* check if path exists and is a regular file */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1031)   if( stat( (const char *)&buf[0], &sb ) == 0 && S_ISREG(sb.st_mode) )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1032)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1033)     char *ln   = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1034)     char *line = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1035) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1036)     FILE *input;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1037) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1038)     input = fopen( (const char *)&buf[0], "r" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1039)     if( !input )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1040)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1041)       FATAL_ERROR( "Unable to access %s file", (char *)&buf[0] );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1042)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1043) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1044)     line = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1045)     if( !line )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1046)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1047)       FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1048)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1049) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1050)     /* cat .REQUIRES >> PKGLOG */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1051)     while( (ln = fgets( line, PATH_MAX, input )) )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1052)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1053)       ln[strlen(ln) - 1] = '\0'; /* replace new-line symbol      */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1054)       skip_eol_spaces( ln );     /* remove spaces at end-of-line */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1055) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1056)       /* print all lines */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1057)       fprintf( output, "%s\n", ln );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1058)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1059)     free( line );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1060)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1061)   else
11c606a6 (kx 2023-04-11 01:18:34 +0300 1062)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1063)     FATAL_ERROR( "Package doesn't contains the INSTALL script" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1064)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1065) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1066)   free( buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1067) }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1068) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1069) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1070) void write_file_list( void )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1071) {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1072)   struct stat sb;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1073)   char  *buf = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1074) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1075)   if( output == NULL && output_fname == NULL )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1076)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1077)     FATAL_ERROR( "Unable to access output file" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1078)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1079) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1080)   buf = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1081)   if( !buf )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1082)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1083)     FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1084)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1085) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1086)   fprintf( output, "FILE LIST:\n" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1087) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1088)   bzero( (void *)buf, PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1089)   (void)sprintf( (char *)&buf[0], "%s/.FILELIST", srcdir );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1090) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1091)   /* check if path exists and is a regular file */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1092)   if( stat( (const char *)&buf[0], &sb ) == 0 && S_ISREG(sb.st_mode) )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1093)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1094)     char *ln;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1095)     char *line = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1096) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1097)     FILE *input;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1098) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1099)     input = fopen( (const char *)&buf[0], "r" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1100)     if( !input )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1101)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1102)       FATAL_ERROR( "Unable to access %s file", (char *)&buf[0] );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1103)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1104) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1105)     line = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1106)     if( !line )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1107)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1108)       FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1109)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1110) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1111)     /* cat .REQUIRES >> PKGLOG */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1112)     while( (ln = fgets( line, PATH_MAX, input )) )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1113)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1114)       ln[strlen(ln) - 1] = '\0'; /* replace new-line symbol      */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1115)       skip_eol_spaces( ln );     /* remove spaces at end-of-line */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1116) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1117)       /* print non-empty lines */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1118)       if( *ln )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1119)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1120)         fprintf( output, "%s\n", ln );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1121)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1122)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1123) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1124)     free( line );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1125)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1126)   else
11c606a6 (kx 2023-04-11 01:18:34 +0300 1127)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1128)     FATAL_ERROR( "Package doesn't contains the FILE list" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1129)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1130) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1131)   free( buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1132) }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1133) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1134) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1135) /*********************************************
11c606a6 (kx 2023-04-11 01:18:34 +0300 1136)   Get directory where this program is placed:
11c606a6 (kx 2023-04-11 01:18:34 +0300 1137)  */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1138) char *get_selfdir( void )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1139) {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1140)   char    *buf = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1141)   ssize_t  len;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1142) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1143)   buf = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1144)   if( !buf )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1145)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1146)     FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1147)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1148) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1149)   bzero( (void *)buf, PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1150)   len = readlink( "/proc/self/exe", buf, (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1151)   if( len > 0 && len < PATH_MAX )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1152)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1153)     char *p = xstrdup( (const char *)dirname( buf ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1154)     free( buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1155)     return p;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1156)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1157)   FATAL_ERROR( "Cannot determine self directory. Please mount /proc filesystem" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1158) }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1159) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1160) void set_stack_size( void )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1161) {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1162)   const rlim_t   stack_size = 16 * 1024 * 1024; /* min stack size = 16 MB */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1163)   struct rlimit  rl;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1164)   int ret;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1165) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1166)   ret = getrlimit( RLIMIT_STACK, &rl );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1167)   if( ret == 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1168)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1169)     if( rl.rlim_cur < stack_size )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1170)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1171)       rl.rlim_cur = stack_size;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1172)       ret = setrlimit( RLIMIT_STACK, &rl );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1173)       if( ret != 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1174)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1175)         fprintf(stderr, "setrlimit returned result = %d\n", ret);
11c606a6 (kx 2023-04-11 01:18:34 +0300 1176)         FATAL_ERROR( "Cannot set stack size" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1177)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1178)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1179)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1180) }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1181) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1182) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1183) int main( int argc, char *argv[] )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1184) {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1185)   gid_t  gid;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1186) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1187)   set_signal_handlers();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1188)   gid = getgid();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1189)   setgroups( 1, &gid );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1190) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1191)   fatal_error_hook = fatal_error_actions;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1192) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1193)   selfdir = get_selfdir();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1194) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1195)   errlog = stderr;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1196) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1197)   program = basename( argv[0] );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1198)   get_args( argc, argv );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1199) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1200)   /* set_stack_size(); */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1201) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1202)   if( pkginfo_type != PKGINFO_TEXT )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1203)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1204)     /* Create tmpdir */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1205)     srcdir = _mk_tmpdir();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1206)     if( !srcdir )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1207)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1208)       FATAL_ERROR( "Cannot create temporary dir" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1209)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1210)     rm_srcdir_at_exit = 1;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1211) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1212)     /* Unpack SERVICE files */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1213)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1214)       pid_t p = (pid_t) -1;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1215)       int   rc;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1216) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1217)       int   len = 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1218)       char *cmd = NULL, *errmsg = NULL, *wmsg = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1219) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1220)       cmd = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1221)       if( !cmd )    { FATAL_ERROR( "Cannot allocate memory" ); }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1222) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1223)       errmsg = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1224)       if( !errmsg ) { FATAL_ERROR( "Cannot allocate memory" ); }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1225) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1226)       wmsg = (char *)malloc( (size_t)PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1227)       if( !wmsg )   { FATAL_ERROR( "Cannot allocate memory" ); }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1228) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1229)       bzero( (void *)cmd, PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1230)       bzero( (void *)errmsg, PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1231)       bzero( (void *)wmsg, PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1232) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1233)       (void)sprintf( &errmsg[0], "Cannot get SERVICE files from %s file", basename( pkginfo_fname ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1234) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1235)       len = snprintf( &cmd[0], PATH_MAX, "tar -C %s -x%sf %s %s > /dev/null 2>&1",
11c606a6 (kx 2023-04-11 01:18:34 +0300 1236)                                           srcdir, uncompress, pkginfo_fname,
11c606a6 (kx 2023-04-11 01:18:34 +0300 1237)                                          ".PKGINFO .REQUIRES .DESCRIPTION .RESTORELINKS .INSTALL .FILELIST" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1238)       if( len == 0 || len == PATH_MAX - 1 )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1239)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1240)         FATAL_ERROR( errmsg );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1241)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1242)       p = sys_exec_command( cmd );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1243)       rc = sys_wait_command( p, (char *)&wmsg[0], PATH_MAX );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1244)       if( rc != 0 )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1245)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1246)         if( ! DO_NOT_WARN_ABOUT_SERVICE_FILES )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1247)         {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1248)           /*****************************************
11c606a6 (kx 2023-04-11 01:18:34 +0300 1249)             if( rc > 0 ) { return TAR exit status }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1250)             else         { return EXIT_FAILURE    }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1251)            */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1252)           if( rc > 0 ) exit_status = rc - 1; /* ERROR() will add one */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1253)           ERROR( errmsg );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1254)           if( fatal_error_hook) fatal_error_hook();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1255)           exit( exit_status );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1256)         }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1257)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1258) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1259)       if( cmd )    free( cmd );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1260)       if( errmsg ) free( errmsg );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1261)       if( wmsg )   free( wmsg );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1262)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1263) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1264)     /* Change input pkginfo file name and type */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1265)     if( pkginfo_fname )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1266)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1267)       char *buf = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1268) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1269)       buf = (char *)malloc( strlen( pkginfo_fname ) + 10 );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1270)       if( !buf )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1271)       {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1272)         FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1273)       }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1274) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1275)       (void)sprintf( &buf[0], "%s/.PKGINFO", srcdir );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1276) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1277)       free( pkginfo_fname ); pkginfo_fname = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1278) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1279)       pkginfo_fname = xstrdup( (const char *)buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1280)       free( buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1281)       pkginfo_type  = PKGINFO_TEXT;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1282)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1283) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1284)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1285)   else /* TEXT: */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1286)   {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1287)     char *buf = NULL;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1288) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1289)     buf = (char *)malloc( (size_t)strlen( pkginfo_fname ) + 1 );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1290)     if( !buf )
11c606a6 (kx 2023-04-11 01:18:34 +0300 1291)     {
11c606a6 (kx 2023-04-11 01:18:34 +0300 1292)       FATAL_ERROR( "Cannot allocate memory" );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1293)     }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1294) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1295)     /* function dirname() spoils the source contents: */
11c606a6 (kx 2023-04-11 01:18:34 +0300 1296)     (void)sprintf( buf, "%s", pkginfo_fname );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1297) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1298)     srcdir = xstrdup( (const char *)dirname( (char *)&buf[0] ) );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1299)     free( buf );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1300)     rm_srcdir_at_exit = 0;
11c606a6 (kx 2023-04-11 01:18:34 +0300 1301)   }
11c606a6 (kx 2023-04-11 01:18:34 +0300 1302) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1303) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1304)   write_pkginfo();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1305)   write_requires();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1306)   write_description();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1307)   write_restore_links();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1308)   write_install_script();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1309)   write_file_list();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1310) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1311) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1312)   if( rm_srcdir_at_exit ) _rm_tmpdir( (const char *)srcdir );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1313)   free_resources();
11c606a6 (kx 2023-04-11 01:18:34 +0300 1314) 
11c606a6 (kx 2023-04-11 01:18:34 +0300 1315)   exit( exit_status );
11c606a6 (kx 2023-04-11 01:18:34 +0300 1316) }