LCOV - code coverage report
Current view: top level - src - repos.c (source / functions) Hit Total Coverage
Test: deployctl-0.3.15.2.96a2d Code Coverage Lines: 190 501 37.9 %
Date: 2018-06-22 Functions: 3 17 17.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //
       2             : //  repos.c
       3             : //  deployctl
       4             : //
       5             : //  Created by Danny Goossen on 10/5/17.
       6             : //  Copyright (c) 2017 Danny Goossen. All rights reserved.
       7             : //
       8             : 
       9             : 
      10             : #include "deployd.h"
      11             : 
      12             : /*
      13             :  --- yaml repos
      14             :  projects:
      15             :  - projecturl1
      16             :  - projecturlb
      17             : 
      18             :  repos:
      19             :  - rpm:
      20             :  - centos7:
      21             :  - x86_64
      22             :  - armhfp
      23             :  - fedora25:
      24             :  - x64_86
      25             :  - coreos:
      26             :  - x86_64
      27             :  - apt:
      28             :  - general
      29             :  - x86_64
      30             :  - kodi:
      31             : 
      32             :  mkdir -pv repopath/(rpm/(centos7/(x86_64,armhfp),))
      33             :  */
      34          20 : void add_endpoint( cJSON** end_points, cJSON * trace_endpoints)//, char *value)
      35             : {
      36             :    char endpoint_str[1024];
      37          20 :    cJSON * value=NULL;
      38          20 :    cJSON * pos=NULL;
      39          20 :    value=cJSON_CreateArray();
      40             : 
      41          20 :    endpoint_str[0]=0;
      42          80 :    cJSON_ArrayForEach(pos,trace_endpoints)
      43             :    {
      44          60 :       if (cJSON_IsString(pos))
      45             :       {
      46          52 :          cJSON_AddItemToArray(value, cJSON_CreateString(pos->valuestring));
      47          52 :          sprintf(endpoint_str+strlen(endpoint_str), "/%s",pos->valuestring);
      48             :       }
      49             :       else
      50             :       { // if not string :
      51             :          // use key of child and add object as e.g. {"amd64": "x86_64"}
      52           8 :          sprintf(endpoint_str+strlen(endpoint_str), "/%s",pos->child->string);
      53           8 :          cJSON_AddItemToArray(value, cJSON_Duplicate(pos, 1));
      54             :       }
      55             :    }
      56          20 :    cJSON * tmp= cJSON_CreateObject();
      57          20 :    cJSON_AddItemToObject(tmp, endpoint_str,  value);
      58          20 :    if (!*end_points) *end_points=cJSON_CreateArray();
      59          20 :    cJSON_AddItemToArray(*end_points, tmp);
      60          20 :    tmp=NULL;
      61          20 : }
      62             : 
      63             : 
      64          14 : char * extract_dir_structure( cJSON* repo, cJSON** end_points, cJSON * trace_endpoints)
      65             : {
      66             :    struct MemoryStruct mem;
      67          14 :    init_dynamicbuf(&mem);
      68          14 :    if (!repo) return NULL;
      69             :    cJSON * pos;
      70          14 :    int item=0;
      71          14 :    char * temp=NULL;
      72          14 :    if (repo->type==cJSON_Array)
      73             :    {
      74          46 :       cJSON_ArrayForEach(pos,repo)
      75             :       {
      76          32 :          if (item) Writedynamicbuf(",", &mem);
      77          32 :          if (pos->type==cJSON_Object)
      78             :          {
      79             :                         /* if (strchr(pos->child->string,' '))
      80             :                          { // quote if name contains spaces
      81             :                                 Writedynamicbuf("\"", &mem);
      82             :                 Writedynamicbuf(pos->child->string, &mem);
      83             :                                 Writedynamicbuf("\"", &mem);
      84             :                          }
      85             :                         else */
      86          20 :                                 Writedynamicbuf(pos->child->string, &mem);
      87          20 :                         cJSON * this_trace= cJSON_Duplicate(trace_endpoints, 1);
      88          20 :             if (cJSON_GetArraySize(this_trace) <2)
      89             :             { // not at the end, call ourselfs to create next element in the trace.
      90          12 :                cJSON_AddItemToArray(this_trace, cJSON_CreateString(pos->child->string));
      91          12 :                temp= extract_dir_structure(pos->child,end_points,this_trace);
      92             :             }
      93             :             else
      94             :             { // end of the line, add object to trace and make endpoint.
      95           8 :                cJSON_AddItemToArray(this_trace, cJSON_Duplicate(pos, 1));
      96           8 :                add_endpoint(end_points, this_trace);
      97             :             }
      98          20 :             cJSON_Delete(this_trace);
      99          20 :             this_trace=NULL;
     100             :          }
     101          32 :          if (pos->type==cJSON_String) // end of the line, add as endpoint
     102             :          {
     103          12 :                          if (strchr(pos->valuestring,' '))
     104             :                          { // quote if name contains spaces
     105           0 :                                  Writedynamicbuf("\"", &mem);
     106           0 :                                  Writedynamicbuf(pos->valuestring, &mem);
     107           0 :                                  Writedynamicbuf("\"", &mem);
     108             :                          }
     109             :                          else
     110          12 :                              Writedynamicbuf(pos->valuestring, &mem);
     111          12 :             cJSON * this_trace= cJSON_Duplicate(trace_endpoints, 1);
     112          12 :             cJSON_AddItemToArray(this_trace, cJSON_CreateString(pos->valuestring));
     113          12 :             add_endpoint(end_points, this_trace);
     114             :          }
     115          32 :          if (temp){
     116          12 :             Writedynamicbuf("/", &mem);
     117          12 :             Writedynamicbuf(temp, &mem);
     118          12 :             free(temp);
     119          12 :             temp=NULL;
     120             :          }
     121          32 :          item++;
     122             :       }
     123             :    }
     124          14 :    if (mem.memory && strlen(mem.memory)==0)
     125             :    {
     126           0 :       free_dynamicbuf(&mem);
     127             :    }
     128          14 :    if (item>1 && mem.memory)
     129             :    {
     130          14 :       char * temp_result=calloc(1,strlen(mem.memory)+3);
     131          14 :       sprintf(temp_result,"{%s}",mem.memory);
     132          14 :       free_dynamicbuf(&mem);
     133          14 :       return temp_result;
     134             :    }
     135             :    else
     136           0 :       return mem.memory;
     137             : }
     138             : 
     139             : 
     140             : 
     141             : /*------------------------------------------------------------------------
     142             :  * converts a repo.json obj to a javascript var with project info
     143             :  * returns NULL on errors
     144             :  * return a null terminated string, caller is responsible for freeing it!
     145             :  *------------------------------------------------------------------------*/
     146           0 : char * create_repo_json_js (void * opaque, char * download_url, char * repo_json)
     147             : {
     148           0 :    debug("start  create_repo_json_js\n");
     149           0 :    cJSON * env_cjson=((data_exchange_t *)opaque)->env_json;
     150             : 
     151             :    struct MemoryStruct js_out;
     152           0 :    init_dynamicbuf(&js_out);
     153             : 
     154             :    /*
     155             :     var target = "Releases"
     156             :     var download_url= "https://downloads.deployctl.com";
     157             :     var deployctl_version="0.0.20";
     158             :     var project_info={
     159             :     "name":   "deployctl",
     160             :     "link":   "https://gitlab.gioxa.com/deployctl/deployctl",
     161             :     "path": "/deployctl/deployctl"
     162             :     };
     163             :     */
     164             : 
     165           0 :    Writedynamicbuf("var target = \"",  &js_out);
     166           0 :    Writedynamicbuf( "Repository",  &js_out);
     167           0 :    Writedynamicbuf("\";\n",  &js_out);
     168             : 
     169             : 
     170           0 :    if (download_url)
     171             :    {
     172           0 :       Writedynamicbuf("var download_url = \"",  &js_out);
     173           0 :       Writedynamicbuf( download_url,  &js_out);
     174           0 :       Writedynamicbuf("\";\n",  &js_out);
     175             :    }
     176           0 :    Writedynamicbuf("var deployctl_version = \"",  &js_out);
     177           0 :    Writedynamicbuf( PACKAGE_VERSION,  &js_out);
     178           0 :    Writedynamicbuf("\";\n",  &js_out);
     179             :    // project_info
     180           0 :    Writedynamicbuf("var project_info = {",  &js_out);
     181             :    // project name
     182           0 :    Writedynamicbuf( "\"name\" : \"",  &js_out);
     183           0 :    Writedynamicbuf( cJSON_get_key(env_cjson, "CI_PROJECT_NAME"),  &js_out);
     184           0 :    Writedynamicbuf("\",\n",  &js_out);
     185             :    // project path
     186           0 :    Writedynamicbuf( "\"path\" : \"",  &js_out);
     187           0 :    Writedynamicbuf( cJSON_get_key(env_cjson, "CI_PROJECT_PATH"),  &js_out);
     188           0 :    Writedynamicbuf("\",\n",  &js_out);
     189             :    // project path
     190           0 :    Writedynamicbuf( "\"link\" : \"",  &js_out);
     191           0 :    Writedynamicbuf( cJSON_get_key(env_cjson, "CI_PROJECT_URL"),  &js_out);
     192           0 :    Writedynamicbuf("\"",  &js_out);
     193             :    // close
     194           0 :    Writedynamicbuf("};\n",  &js_out);
     195           0 :    if (repo_json)
     196             :    {
     197           0 :       Writedynamicbuf("var RepoData = ",  &js_out);
     198           0 :       Writedynamicbuf(repo_json,  &js_out);
     199           0 :       Writedynamicbuf(";\n",  &js_out);
     200             :    }
     201           0 :    return (js_out.memory);
     202             : }
     203             : 
     204             : 
     205             : 
     206             : 
     207             : 
     208           0 : void get_rpm_info(cJSON * rpm_list,cJSON ** repo_info)
     209             : {
     210           0 :    cJSON * pos=NULL;
     211           0 :    cJSON* rpm_info_item=NULL;
     212           0 :    cJSON_ArrayForEach(pos,rpm_list)
     213             :    {
     214             :       char temp[1024];
     215           0 :       sprintf(temp, "%s/",cJSON_get_key(pos,"path")); // need to add slash
     216           0 :       debug("%s%s\n",temp,cJSON_get_key(pos,"filename"));
     217           0 :       get_rpm_fileinfo(NULL,temp, cJSON_get_key(pos,"filename"),&rpm_info_item);
     218           0 :       if (rpm_info_item)
     219             :       {
     220           0 :          if (!*repo_info ) *repo_info=cJSON_CreateArray();
     221           0 :          cJSON_AddItemToArray(*repo_info, rpm_info_item);
     222           0 :          rpm_info_item=NULL;
     223             :       }
     224             :    } // for each rpmfile
     225           0 :    return;
     226             : }
     227             : 
     228           0 : void get_deb_info(cJSON * deb_list,cJSON ** repo_info)
     229             : {
     230           0 :    if (!deb_list || !repo_info) return;
     231           0 :    cJSON * item=NULL;
     232           0 :    cJSON * pos=NULL;
     233           0 :    cJSON_ArrayForEach(pos,deb_list)
     234             :    {
     235           0 :       item=cJSON_CreateObject();
     236           0 :       char *buf=strdup(cJSON_get_key(pos, "subpath"));
     237           0 :       int i = 0;
     238           0 :       char *p = strtok (buf, "/");
     239             :       char *array[4];
     240           0 :       while (p != NULL&& i<4)
     241             :       {
     242           0 :          array[i++] = p;
     243           0 :          p = strtok (NULL, "/");
     244             :       }
     245           0 :       if (i>3 || i ==0)
     246             :       {
     247           0 :          cJSON_AddStringToObject(item, "error","wrong subpath");
     248           0 :          cJSON_AddStringToObject(item, "subpath",cJSON_get_key(pos, "subpath"));
     249             :       }
     250             :       else
     251           0 :          cJSON_AddStringToObject(item, "type",array[0]);
     252           0 :       if (i>1)
     253           0 :           cJSON_AddStringToObject(item, "distribution",array[1]);
     254           0 :       if (i>2)
     255           0 :          cJSON_AddStringToObject(item, "release",array[2]);
     256             : 
     257           0 :       cJSON_AddStringToObject(item, "filename", cJSON_get_key(pos, "filename"));
     258           0 :       cJSON_AddStringToObject(item, "fullpath",cJSON_get_key(pos, "filepath"));
     259           0 :       if (!*repo_info)* repo_info =cJSON_CreateArray();
     260           0 :       cJSON_AddItemToArray(*repo_info, item);
     261           0 :       free(buf);
     262           0 :       buf=NULL;
     263             :    }
     264             :    
     265             :    return;
     266             : }
     267             : 
     268           0 : void repo_rpm_list( const char *projectpath, cJSON** repo_info)
     269             : {
     270           0 :    if (!projectpath || strlen(projectpath)==0) return ;
     271           0 :    debug("getting directory listing: %s/rpm\n",projectpath);
     272           0 :    cJSON * filearray=JSON_dir_list_x( projectpath,"rpm",0, ".rpm");
     273           0 :    debug("DONE directory listing: %s/rpm\n",projectpath);
     274           0 :    get_rpm_info(filearray,repo_info);
     275           0 :    if (filearray)cJSON_Delete(filearray);
     276             :    return;
     277             : }
     278             : 
     279           0 : void repo_deb_list( const char *projectpath, cJSON** repo_info)
     280             : {
     281           0 :    if (!projectpath || strlen(projectpath)==0) return;
     282           0 :    debug("getting directory listing: %s/deb\n",projectpath);
     283           0 :    cJSON * filearray=JSON_dir_list_x( projectpath,"deb",1, ".deb");
     284           0 :    debug("DONE directory listing: %s/deb\n",projectpath);
     285           0 :    get_deb_info( filearray, repo_info);
     286           0 :    if (filearray)cJSON_Delete(filearray);
     287             :    return ;
     288             : }
     289             : 
     290           0 : int get_repo_list(void * opaque, const char *projectpath, cJSON** repo_info)
     291             : {
     292           0 :    repo_rpm_list(projectpath,repo_info);
     293           0 :    repo_deb_list(projectpath,repo_info);
     294           0 :    return 0;
     295             : }
     296             : 
     297           1 : int match_end_points(void * opaque, cJSON* repo_info, cJSON* end_points, cJSON ** matches, cJSON ** skips, cJSON** symlinks)
     298             : {
     299             : 
     300             :    // find endpoint for packages and/or wildcards and/or skipped packages.
     301             : 
     302           1 :    cJSON * pos=NULL;
     303          14 :    cJSON_ArrayForEach(pos,repo_info)
     304             :    {
     305          13 :       int found=0;
     306          13 :       const char * type=cJSON_get_key(pos, "type");
     307          13 :       const char * distribution=cJSON_get_key(pos, "distribution");
     308          13 :       const char * arch=cJSON_get_key(pos, "arch");
     309          13 :       const char * release=cJSON_get_key(pos, "release");
     310          13 :        const char * error=cJSON_get_key(pos, "error");
     311          13 :       const char * endpoint_type=NULL;
     312          13 :       const char * endpoint_distribution=NULL;
     313          13 :       const char * endpoint_arch=NULL;
     314          13 :       const char * endpoint_release=NULL;
     315          13 :       const char * endpoint=NULL;
     316          13 :       cJSON * tmp=NULL;
     317          13 :       int srpm=0;
     318          13 :       if ((tmp=cJSON_GetObjectItem(pos, "SRPM"))) srpm=tmp->valueint;
     319             : 
     320          13 :       cJSON * pos_endpoints=NULL;
     321          13 :       int wildcard_found_1=0;
     322          13 :       int wildcard_found_2=0;
     323          13 :       int wildcard_found_3=0;
     324          13 :       if (!error && !srpm)
     325             :       {
     326         108 :          cJSON_ArrayForEach(pos_endpoints,end_points)
     327             :          {
     328          99 :             endpoint_type =cJSON_GetArrayItem(pos_endpoints->child, 0)->valuestring;
     329          99 :             endpoint_distribution =cJSON_GetArrayItem(pos_endpoints->child, 1)->valuestring;
     330          99 :             endpoint_arch=NULL;
     331          99 :             cJSON * arch_item= cJSON_GetArrayItem(pos_endpoints->child, 2);
     332          99 :             if ( cJSON_IsString(arch_item)) endpoint_arch=arch_item->valuestring;
     333          99 :             if ( cJSON_IsObject(arch_item)) {
     334          40 :                if (cJSON_IsString(arch_item->child))
     335          20 :                endpoint_arch=arch_item->child->valuestring;
     336          20 :                else if (cJSON_IsArray(arch_item->child) )
     337             :                {
     338          20 :                   if (!arch || strcmp(arch,"noarch")==0 )
     339             :                   {
     340          14 :                      endpoint_arch=arch_item->child->string;
     341             :                   }
     342             :                   else
     343             :                   {
     344           6 :                      cJSON * rpm_arch_list=NULL;
     345          18 :                      cJSON_ArrayForEach(rpm_arch_list,arch_item->child)
     346             :                      {
     347          12 :                         if (strcmp(arch,rpm_arch_list->valuestring)==0)  {
     348           0 :                            endpoint_arch=rpm_arch_list->valuestring;
     349             :                         }
     350             :                      }
     351             :                      // if still NULL give it the repo arch
     352           6 :                      if (endpoint_arch==NULL) endpoint_arch=arch_item->child->string;
     353             :                   }
     354             :                }
     355             :             }
     356          99 :             endpoint_release =cJSON_GetArrayItem(pos_endpoints->child, 2)->valuestring;
     357          99 :             endpoint =pos_endpoints->child->string;
     358             : 
     359             :             // debug
     360          99 :             if (strcmp(type,"rpm")==0 && !distribution && arch && !endpoint_release && strcmp(arch,"i486")==0)
     361             :             {
     362             :                // this is the culprit
     363           0 :                debug("%s: %s :%s\n", cJSON_get_key(pos, "filename"),endpoint,endpoint_arch);
     364             :             }
     365             : 
     366          99 :             if ( \
     367         247 :                 (  type && endpoint_type && strcmp(type,endpoint_type)==0 ) && \
     368         118 :                 ( ( ! distribution && endpoint_distribution && strcmp("nodist",endpoint_distribution)==0  ) || \
     369          78 :                  (   distribution && endpoint_distribution && strcmp(distribution,endpoint_distribution)==0 ) \
     370           8 :                  ) && \
     371          13 :                  (( arch && endpoint_arch && strcmp(arch,endpoint_arch)==0 ) || ( release && endpoint_release && strcmp(release,endpoint_release)==0 ))
     372             :                 )
     373             :             { // found in Full
     374           2 :                if (!*matches) *matches=cJSON_CreateArray();
     375           2 :                cJSON * tmp=cJSON_CreateObject();
     376           2 :                cJSON_AddStringToObject(tmp, "fullpath", cJSON_get_key(pos, "fullpath"));
     377           2 :                cJSON_AddStringToObject(tmp, "filename", cJSON_get_key(pos, "filename"));
     378           2 :                cJSON_AddStringToObject(tmp, "endpoint", endpoint);
     379           2 :                cJSON_AddItemToArray(*matches, tmp);
     380           2 :                found=1;
     381             :                                 // no break, loop all basearm's on rpm
     382             : 
     383           2 :                if (strcmp(type,"rpm")!=0 )break;
     384             :             } // found in Full
     385          97 :             else if ( \
     386         144 :                 (  type && endpoint_type && strcmp(type,endpoint_type)==0 ) && \
     387         114 :                 ( ( ! distribution && endpoint_distribution && strcmp("nodist",endpoint_distribution)==0  ) || \
     388          80 :                  (   distribution && endpoint_distribution && strcmp(distribution,endpoint_distribution)==0 ) ) &&\
     389          10 :                  ( (!arch && !release) ||  (arch && strcmp(arch,"noarch")==0 ))
     390             :                 )
     391             :             { // wild card on arch or release (rpm or deb)
     392           4 :                if (!wildcard_found_1 && !wildcard_found_2 && !wildcard_found_3)
     393             :                {
     394           2 :                   if (!*matches) *matches=cJSON_CreateArray();
     395           2 :                   cJSON * tmp=cJSON_CreateObject();
     396           2 :                   cJSON_AddStringToObject(tmp, "fullpath", cJSON_get_key(pos, "fullpath"));
     397           2 :                   cJSON_AddStringToObject(tmp, "filename", cJSON_get_key(pos, "filename"));
     398             :                   char new_sub_endpoint[1024];
     399           2 :                   sprintf(new_sub_endpoint, "/%s/%s",endpoint_type,endpoint_distribution);
     400           2 :                   cJSON_AddStringToObject(tmp, "endpoint", new_sub_endpoint);
     401           2 :                   cJSON_AddItemToArray(*matches, tmp);
     402           2 :                   wildcard_found_1=1;
     403           2 :                   found=1;
     404             :                }
     405           4 :                if (wildcard_found_1)
     406             :                { //prepare symlinks
     407           4 :                   found=1;
     408           4 :                   if (!*symlinks) *symlinks=cJSON_CreateArray();
     409           4 :                   cJSON * tmp=cJSON_CreateObject();
     410             :                   // create
     411             :                   char sym_source[256];
     412           4 :                   sprintf(sym_source, "/%s/%s",endpoint_type,endpoint_distribution);
     413           4 :                   cJSON_AddStringToObject(tmp, "source", sym_source );
     414           4 :                   cJSON_AddStringToObject(tmp, "filename", cJSON_get_key(pos, "filename"));
     415           4 :                   cJSON_AddStringToObject(tmp, "endpoint", endpoint);
     416           4 :                   cJSON_AddItemToArray(*symlinks, tmp);
     417             :                }
     418             :             } // wild card on arch or release (rpm or deb)
     419          93 :             else if ( \
     420          93 :                      (  type && endpoint_type && strcmp(type,endpoint_type)==0 ) && \
     421          43 :                      ( !distribution ||  (distribution && strcmp(distribution,"nodist")==0 )) && \
     422          35 :                      ( ((arch && endpoint_arch && strcmp(arch,endpoint_arch)==0)|| (release && endpoint_release && strcmp(release,endpoint_release)==0 )) && endpoint_arch  && !(arch && strcmp(arch,"noarch")==0)) )
     423             :             { // wild card on distribution but not on arch rpm
     424           2 :                if (!wildcard_found_1 && !wildcard_found_2 && !wildcard_found_3)
     425             :                {
     426           1 :                   if (!*matches) *matches=cJSON_CreateArray();
     427           1 :                   cJSON * tmp=cJSON_CreateObject();
     428           1 :                   cJSON_AddStringToObject(tmp, "fullpath", cJSON_get_key(pos, "fullpath"));
     429           1 :                   cJSON_AddStringToObject(tmp, "filename", cJSON_get_key(pos, "filename"));
     430             :                   char new_sub_endpoint[1024];
     431           1 :                   sprintf(new_sub_endpoint, "/%s",endpoint_type);
     432           1 :                   cJSON_AddStringToObject(tmp, "endpoint", new_sub_endpoint);
     433           1 :                   cJSON_AddItemToArray(*matches, tmp);
     434           1 :                   wildcard_found_2=1;
     435           1 :                   found=1;
     436             :                }
     437           2 :                if (wildcard_found_2)
     438             :                { //prepare symlinks
     439           2 :                   found=1;
     440           2 :                   if (!*symlinks) *symlinks=cJSON_CreateArray();
     441           2 :                   cJSON * tmp=cJSON_CreateObject();
     442             :                   char sym_source[256];
     443           2 :                   sprintf(sym_source, "/%s",endpoint_type);
     444           2 :                   cJSON_AddStringToObject(tmp, "source", sym_source );
     445           2 :                   cJSON_AddStringToObject(tmp, "filename", cJSON_get_key(pos, "filename"));
     446           2 :                   cJSON_AddStringToObject(tmp, "endpoint", endpoint);
     447           2 :                   cJSON_AddItemToArray(*symlinks, tmp);
     448             :                }
     449             :             } // wild card on distribution but not on arch rpm
     450             : 
     451          91 :             else if ( type && endpoint_type && strcmp(type,endpoint_type)==0 && \
     452          41 :                      ( !distribution ||  (distribution && strcmp(distribution,"nodist")==0 )) && \
     453          31 :                      ( (!arch && !release) ||  (arch && strcmp(arch,"noarch")==0 )) \
     454             :                      )
     455             :             { // wild card on distro and arch / distro (rpm/deb)
     456          10 :                if (!wildcard_found_1 && !wildcard_found_2 && !wildcard_found_3)
     457             :                {
     458           2 :                   if (!*matches) *matches=cJSON_CreateArray();
     459           2 :                   cJSON * tmp=cJSON_CreateObject();
     460           2 :                   cJSON_AddStringToObject(tmp, "fullpath", cJSON_get_key(pos, "fullpath"));
     461           2 :                   cJSON_AddStringToObject(tmp, "filename", cJSON_get_key(pos, "filename"));
     462             :                   char new_sub_endpoint[1024];
     463           2 :                   sprintf(new_sub_endpoint, "/%s",endpoint_type);
     464           2 :                   cJSON_AddStringToObject(tmp, "endpoint", new_sub_endpoint);
     465           2 :                   cJSON_AddItemToArray(*matches, tmp);
     466           2 :                   wildcard_found_3=1;
     467           2 :                   found=1;
     468             :                }
     469             : 
     470          10 :                if (wildcard_found_3)
     471             :                { //prepare symlinks
     472          10 :                   found=1;
     473          10 :                   if (!*symlinks) *symlinks=cJSON_CreateArray();
     474          10 :                   cJSON * tmp=cJSON_CreateObject();
     475             :                   char sym_source[256];
     476          10 :                   sprintf(sym_source, "/%s",endpoint_type);
     477          10 :                   cJSON_AddStringToObject(tmp, "source", sym_source );
     478          10 :                   cJSON_AddStringToObject(tmp, "filename", cJSON_get_key(pos, "filename"));
     479          10 :                   cJSON_AddStringToObject(tmp, "endpoint", endpoint);
     480          10 :                   cJSON_AddItemToArray(*symlinks, tmp);
     481             :                }
     482             :             } // wild card on distro
     483             :          }
     484             :       }
     485          13 :       if (!found)
     486             :       {
     487           6 :          if (!*skips) *skips=cJSON_CreateArray();
     488           6 :          cJSON * tmp=cJSON_CreateObject();
     489           6 :          if (cJSON_get_key(pos, "filename"))cJSON_AddStringToObject(tmp, "filename", cJSON_get_key(pos, "filename"));
     490           6 :          if (type)cJSON_AddStringToObject(tmp, "type", type);
     491           6 :          if (distribution)cJSON_AddStringToObject(tmp, "distribution", distribution);
     492           6 :          if (release) cJSON_AddStringToObject(tmp, "release", release);
     493           6 :          if (arch) cJSON_AddStringToObject(tmp, "arch", arch);
     494           6 :          if (error ) cJSON_AddStringToObject(tmp, "error", error);
     495           6 :          if (srpm) cJSON_AddStringToObject(tmp, "SRPM", "True");
     496           6 :          if (cJSON_get_key(pos, "subpath"))cJSON_AddStringToObject(tmp, "subpath",cJSON_get_key(pos, "subpath") );
     497           6 :          cJSON_AddItemToArray(*skips, tmp);
     498             :       }
     499             :    }
     500           1 :    return 0;
     501             : }
     502             : 
     503           0 : void repo_print_skips(void * opaque,cJSON * skips)
     504             : {
     505           0 :    if (!skips) return;
     506           0 :    if (cJSON_GetArraySize(skips)==0) return;
     507           0 :    struct trace_Struct *trace=((data_exchange_t *)opaque)->trace;
     508             : 
     509           0 :    debug("\nSKIPS\n");
     510           0 :    cJSON * pos=NULL;
     511           0 :    if (cJSON_GetArraySize(skips)==1)
     512           0 :       Write_dyn_trace(trace, none,"\nSKIPPED File:\n");
     513             :    else
     514           0 :       Write_dyn_trace(trace, none,"\nSKIPPED Files\n");
     515             : 
     516           0 :       cJSON_ArrayForEach(pos,skips)
     517             :       {
     518           0 :          Write_dyn_trace(trace, none,"-%s: ",cJSON_get_key(pos, "filename"));
     519           0 :          if (cJSON_get_key(pos, "error"))Write_dyn_trace(trace, none,"%s ",cJSON_get_key(pos, "error"));
     520           0 :          if (cJSON_get_key(pos, "subpath"))Write_dyn_trace(trace, none,"(%s) ",cJSON_get_key(pos, "subpath"));
     521           0 :          if (cJSON_get_key(pos, "distribution")) Write_dyn_trace(trace, none,"[%s",cJSON_get_key(pos, "distribution"));
     522           0 :          if (!cJSON_get_key(pos, "arch") && !cJSON_get_key(pos, "release")) Write_dyn_trace(trace, none,"]");
     523           0 :          if (cJSON_get_key(pos, "arch")) Write_dyn_trace(trace, none,"/%s]",cJSON_get_key(pos, "arch"));
     524           0 :          if (cJSON_get_key(pos, "release"))Write_dyn_trace(trace, none,"/%s]",cJSON_get_key(pos, "release"));
     525             :       }
     526             : 
     527           0 :    Write_dyn_trace(trace, none,"\nFiles are skipped when the rpm data does not match the repo-config");
     528           0 :    return;
     529             : }
     530             : 
     531           0 : void repo_copy_matches(void * opaque,cJSON * matches,char* basepath)
     532             : {
     533           0 :    if (!matches || !basepath) return;
     534           0 :    if (cJSON_GetArraySize(matches)==0) return;
     535             : 
     536           0 :    debug("\nSymlinks\n");
     537             : 
     538           0 :    struct trace_Struct *trace=((data_exchange_t *)opaque)->trace;
     539             :    char * newarg[8];
     540             :    char var_arg1_string[1024]; // temporary string
     541             : 
     542           0 :    void * saved_args=((data_exchange_t *)opaque)->paramlist;
     543           0 :    ((data_exchange_t *)opaque)->paramlist=(char **)newarg;
     544           0 :    ((data_exchange_t *)opaque)->needenvp=0;
     545           0 :    int error=0;
     546           0 :    cJSON * pos=NULL;
     547           0 :    cJSON_ArrayForEach(pos,matches)
     548             :    {
     549           0 :       newarg[0]="/bin/cp";
     550           0 :       newarg[1]="-f";
     551           0 :       newarg[2]=cJSON_get_key(pos, "fullpath");
     552           0 :       newarg[3]=(char *)var_arg1_string;
     553           0 :       sprintf((char *)newarg[3],"%s/public%s/", basepath,cJSON_get_key(pos, "endpoint"));
     554           0 :       newarg[4]=NULL;
     555             : 
     556             : 
     557           0 :       Write_dyn_trace_pad(trace, none,39,"+ cp %s", cJSON_get_key(pos, "filename"));
     558           0 :       Write_dyn_trace_pad(trace, none,36," > %s ", cJSON_get_key(pos, "endpoint"));
     559             : 
     560           0 :       debug("cmd: %s %s %s %s\n",newarg[0],newarg[1],newarg[2],newarg[3]);
     561           0 :       error=cmd_exec(opaque);
     562           0 :       if (error)
     563             :       {
     564           0 :          debug("Failed copy %s \n",cJSON_get_key(pos, "filename"));
     565           0 :          Write_dyn_trace(trace, red,"\n ERR: Failed copy\n");
     566           0 :          break;
     567             :       }
     568             :       else
     569           0 :          Write_dyn_trace(trace, green,"[OK]\n");
     570             :    }
     571             :    //Restore arguments
     572           0 :    ((data_exchange_t *)opaque)->paramlist=saved_args;
     573             : }
     574             : 
     575             : 
     576             : 
     577           0 : void repo_create_symlinks(void * opaque,cJSON * symlinks,char* basepath)
     578             : {
     579           0 :    if (!symlinks || !basepath) return;
     580           0 :    if (cJSON_GetArraySize(symlinks)==0) return;
     581             : 
     582           0 :    debug("\nSymlinks\n");
     583             : 
     584           0 :    struct trace_Struct *trace=((data_exchange_t *)opaque)->trace;
     585             :    char * newarg[8];
     586             :    char var_arg1_string[1024]; // temporary string
     587             :    char var_arg2_string[1024]; // temporary string
     588           0 :    void * saved_args=((data_exchange_t *)opaque)->paramlist;
     589           0 :    ((data_exchange_t *)opaque)->paramlist=(char **)newarg;
     590           0 :    ((data_exchange_t *)opaque)->needenvp=0;
     591           0 :    int error=0;
     592           0 :    cJSON * pos=NULL;
     593           0 :    cJSON_ArrayForEach(pos,symlinks)
     594             :    {
     595           0 :       newarg[0]="/bin/ln";
     596           0 :       newarg[1]="-fs";
     597           0 :       newarg[2]=(char *)var_arg1_string;
     598           0 :       newarg[3]=(char *)var_arg2_string;
     599           0 :       sprintf((char *)newarg[2],"%s/public%s/%s", basepath,cJSON_get_key(pos, "source"),cJSON_get_key(pos, "filename"));
     600           0 :       sprintf((char *)newarg[3],"%s/public%s/%s", basepath,cJSON_get_key(pos, "endpoint"),cJSON_get_key(pos, "filename"));
     601           0 :       newarg[4]=NULL;
     602             : 
     603           0 :      Write_dyn_trace_pad(trace, none,39,"+ ln %s", cJSON_get_key(pos, "filename"));
     604           0 :      Write_dyn_trace_pad(trace, none,36," > %s ", cJSON_get_key(pos, "endpoint"));
     605             : 
     606           0 :       debug("cmd: %s %s %s %s\n",newarg[0],newarg[1],newarg[2],newarg[3]);
     607           0 :       error=cmd_exec(opaque);
     608           0 :       if (error)
     609             :       {
     610           0 :          debug("Failed symlink %s \n",cJSON_get_key(pos, "filename"));
     611           0 :          Write_dyn_trace(trace, red,"\n ERR: Failed Symlink\n");
     612           0 :          break;
     613             :       }
     614             :       else
     615           0 :          Write_dyn_trace(trace, green,"[OK]\n");
     616             :    }
     617             :    //Restore arguments
     618           0 :    ((data_exchange_t *)opaque)->paramlist=saved_args;
     619             : }
     620             : 
     621             : // escape space, caller responsible for freeing
     622             : 
     623           0 : char * escape_space(const char * in)
     624             : {
     625           0 :         if (!in) return NULL;
     626             : 
     627             :         struct MemoryStruct mem;
     628           0 :         init_dynamicbuf( &mem);
     629           0 :         long pos_s=0;
     630           0 :         char * pos_space=NULL;
     631           0 :         while ((pos_space=strchr(in+pos_s, ' '))) // change ' ' to "\ "
     632             :         {
     633           0 :                 Writedynamicbuf_n((void*)in+pos_s, (size_t)(pos_space-(in+pos_s)), &mem);
     634           0 :                 Writedynamicbuf("\\ ",&mem);
     635           0 :                 pos_s=pos_space-in+1;
     636             :         }
     637           0 :         Writedynamicbuf((void*)in+pos_s, &mem);
     638           0 :         return mem.memory;
     639             : }
     640             : 
     641           0 : cJSON * create_repo_dirs(void * opaque, char * repo_PATH , cJSON* repos)
     642             : {
     643           0 :    cJSON * endpoints=NULL;
     644           0 :    cJSON * trace_end= cJSON_CreateArray();
     645             : 
     646           0 :    char * temp=extract_dir_structure(repos,&endpoints,trace_end);
     647           0 :    cJSON_Delete(trace_end);
     648           0 :    if (!temp)
     649             :    {
     650           0 :            if (endpoints) cJSON_Delete(endpoints);
     651             :        return NULL;
     652             :    }
     653             : 
     654             :    // feedback buffer
     655           0 :    int exitcode=0;
     656           0 :    struct trace_Struct *trace=((data_exchange_t *)opaque)->trace;
     657             :    // env json
     658             :    char * newarg[4];
     659             :    // no need to set environment for individual commands
     660           0 :    ((data_exchange_t *)opaque)->needenvp=0;
     661           0 :    void * saved_args=((data_exchange_t *)opaque)->paramlist;
     662           0 :    ((data_exchange_t *)opaque)->paramlist=(char **)newarg;
     663             :    char var_arg1_string[1024];
     664           0 :    char * sub = escape_space(temp);
     665           0 :    sprintf(var_arg1_string, "mkdir -pv %s/public/%s",repo_PATH,sub);
     666           0 :         if (sub) free(sub);
     667           0 :         free(temp);
     668           0 :    newarg[0]="/bin/sh";
     669           0 :    newarg[1]="-c";
     670           0 :    newarg[2]=var_arg1_string;
     671           0 :    newarg[3]=NULL;
     672           0 :    debug("create repodir's\n%s\n\n",newarg[2]);
     673           0 :    Write_dyn_trace_pad(trace, none,75,"+ mkdir repos");
     674           0 :    exitcode=cmd_exec(opaque);
     675           0 :    if (exitcode)
     676           0 :      Write_dyn_trace(trace, red,"[FAILED]\n");
     677             :    else
     678           0 :      Write_dyn_trace(trace, green,"[OK]\n");
     679           0 :    if (exitcode)
     680           0 :       error("\nERR: creating dir structure\n");
     681             :    else
     682           0 :       debug ("created repo dir structure\n");
     683             : 
     684           0 :    ((data_exchange_t *)opaque)->paramlist=saved_args;
     685           0 :    return endpoints;
     686             : }
     687             : 
     688           0 : int init_repo_dirs(void * opaque, char * basepath, cJSON* endpoints)
     689             : {
     690             :         // feedback buffer
     691           0 :    int exitcode=0;
     692           0 :    struct trace_Struct *trace=((data_exchange_t *)opaque)->trace;
     693             :         // env json
     694             :         char * newarg[4];
     695             :         // no need to set environment for individual commands
     696           0 :         ((data_exchange_t *)opaque)->needenvp=0;
     697           0 :         void * saved_args=((data_exchange_t *)opaque)->paramlist;
     698           0 :         ((data_exchange_t *)opaque)->paramlist=(char **)newarg;
     699             :         char var_arg1_string[1024];
     700             : 
     701           0 :         newarg[0]="/bin/sh";
     702           0 :         newarg[1]="-c";
     703           0 :         newarg[2]=var_arg1_string;
     704           0 :         newarg[3]=NULL;
     705           0 :         int count=0;
     706           0 :         cJSON * pos=NULL;
     707           0 :         cJSON_ArrayForEach(pos,endpoints)
     708             :         {
     709           0 :     if ((count%5)==0) update_details(trace);
     710           0 :                 debug("create repo: %s\n",pos->child->string);
     711           0 :     int n_len=(int)strlen(pos->child->string);
     712             :     char padn[]="                                             ";
     713             :     if (n_len <= 32) padn[32-n_len]=0; else padn[0]=0;
     714             : 
     715           0 :     Write_dyn_trace_pad(trace, none,75,"+ create repo: %s",pos->child->string);
     716             : 
     717           0 :                 if (strcmp(pos->child->child->valuestring,"rpm")==0)
     718             :                 {
     719           0 :                         char * sub = escape_space(pos->child->string);
     720           0 :                         sprintf(var_arg1_string, "/bin/createrepo --quiet --checkts %s/public%s",basepath,sub);
     721           0 :                         if (sub) free(sub);
     722             :                 }
     723           0 :                 else if (strcmp(pos->child->child->valuestring,"deb")==0)
     724           0 :                         sprintf(var_arg1_string, "cd %s/public%s && /bin/dpkg-scanpackages -m . | gzip -c > Packages.gz ",basepath,pos->child->string);
     725             :                 else {
     726           0 :                         Write_dyn_trace(trace, red,"\t unknow repo type");
     727           0 :          exitcode=1;
     728             :                 }
     729           0 :                 if (!exitcode)exitcode=cmd_exec(opaque);
     730           0 :                 if (exitcode)
     731             :                 {
     732           0 :                         Write_dyn_trace(trace, red,"\t[Failed]\n");
     733             :                         break;
     734             :                 }
     735           0 :                 else if (strcmp(pos->child->child->valuestring,"rpm")==0) Write_dyn_trace(trace, green,"[OK]\n");
     736             :         }
     737             : 
     738           0 :         if (exitcode)
     739           0 :                 error("\nERR: creating repos\n");
     740             :         else
     741           0 :                 debug ("created repos\n");
     742             : 
     743           0 :         ((data_exchange_t *)opaque)->paramlist=saved_args;
     744             : 
     745           0 :         return exitcode;
     746             : }
     747             : 
     748             : 
     749             : 
     750           0 : int update_repo_dirs(void * opaque, char * basepath, cJSON* endpoints)
     751             : {
     752             :    // feedback buffer
     753           0 :    int exitcode=0;
     754           0 :    struct trace_Struct *trace=((data_exchange_t *)opaque)->trace;
     755             :    // env json
     756             :    char * newarg[8];
     757             :    // no need to set environment for individual commands
     758           0 :    ((data_exchange_t *)opaque)->needenvp=0;
     759           0 :    void * saved_args=((data_exchange_t *)opaque)->paramlist;
     760           0 :    ((data_exchange_t *)opaque)->paramlist=(char **)newarg;
     761             :    char var_arg1_string[1024];
     762             : 
     763           0 :    newarg[0]="/bin/sh";
     764           0 :    newarg[1]="-c";
     765           0 :    newarg[2]=var_arg1_string;
     766           0 :    newarg[3]=NULL;
     767           0 :    int count=0;
     768           0 :    cJSON * pos=NULL;
     769           0 :    cJSON_ArrayForEach(pos,endpoints)
     770             :    {
     771           0 :      if ((count%5)==0) update_details(trace);
     772           0 :       debug("update repo: %s\n",pos->child->string);
     773             : 
     774           0 :       Write_dyn_trace_pad(trace, none,75,"+ update repo: %s",pos->child->string);
     775           0 :       if (strcmp(pos->child->child->valuestring,"rpm")==0)
     776             :           {
     777             : 
     778           0 :                   char * sub = escape_space(pos->child->string);
     779           0 :                   sprintf(var_arg1_string, "/bin/createrepo --update --quiet --checkts %s/public%s",basepath,sub);
     780           0 :                   if (sub) free(sub);
     781             :           }
     782           0 :           else if (strcmp(pos->child->child->valuestring,"deb")==0)
     783           0 :          sprintf(var_arg1_string, "cd %s/public%s && /bin/dpkg-scanpackages -m . | gzip -c > Packages.gz ",basepath,pos->child->string);
     784             :       else {
     785           0 :         Write_dyn_trace(trace, magenta,"unknow repo type\n");
     786           0 :          exitcode=1;
     787             :       }
     788           0 :       if (!exitcode)exitcode=cmd_exec(opaque);
     789           0 :       if (exitcode)
     790             :       {
     791           0 :          Write_dyn_trace(trace, red,"[Failed]\n");
     792           0 :          break;
     793             :       }
     794           0 :       else if (strcmp(pos->child->child->valuestring,"rpm")==0) Write_dyn_trace(trace, green,"[OK]\n");
     795             :    }
     796             : 
     797           0 :    if (exitcode)
     798           0 :       error("\nERR: updating repos\n");
     799             :    else
     800           0 :       debug ("updated repos\n");
     801             : 
     802           0 :    ((data_exchange_t *)opaque)->paramlist=saved_args;
     803             : 
     804           0 :    return 0;
     805             : }
     806             : 
     807             : 
     808             : /*------------------------------------------------------------------------
     809             : * substitude script vars for repo setup
     810             : * returns 0 on success
     811             : *------------------------------------------------------------------------*/
     812           0 : int copy_script_sh(void * opaque, const char * source, const char * destination, char * repo_name, char * repo_url, int PGP_repo, int PGP_pack)
     813             : {
     814           0 :    if (!source || !destination) return 1;
     815             :    // feedback buffer
     816           0 :    int exitcode=0;
     817           0 :    struct trace_Struct *trace=((data_exchange_t *)opaque)->trace;
     818             :   // env json
     819             :    const char * newarg[8];
     820             :    // no need to set environment for individual commands
     821           0 :    ((data_exchange_t *)opaque)->needenvp=0;
     822           0 :    void * saved_args=((data_exchange_t *)opaque)->paramlist;
     823           0 :    ((data_exchange_t *)opaque)->paramlist=(char **)newarg;
     824             : 
     825           0 :    debug("Read script: %s\n",source);
     826           0 :    FILE *f = fopen(source, "r");
     827           0 :    if (f == NULL)
     828             :    {
     829           0 :       debug("process_read_file: %s\n",strerror(errno));
     830           0 :       return 1;
     831             :    }
     832             :    struct MemoryStruct mem;
     833           0 :    init_dynamicbuf(&mem);
     834             :    char buf[1024];
     835           0 :    bzero(buf, 1024);
     836           0 :    while( fgets( buf, 1024, f ) != NULL )
     837             :    {
     838           0 :       Writedynamicbuf(buf, &mem );
     839           0 :       bzero(buf, 1024);
     840             :    }
     841           0 :    fclose(f);
     842             :    //fprintf(stderr,"readfile: >\n%s\n",mem.memory);
     843             : 
     844             :    // process file
     845             :    // repo_url=@http://repo.url@
     846             :    // repo_name=@repo_name@
     847             : 
     848           0 :    char * tmp=substitute_repo_script(mem.memory, repo_name,repo_url,PGP_repo,0);
     849             : 
     850           0 :    if (tmp)
     851             :    {
     852             :        // writefile to destination
     853           0 :        newarg[0]="write file";
     854           0 :        newarg[1]=destination;
     855           0 :        newarg[2]=tmp;
     856           0 :        newarg[3]=NULL;
     857           0 :        exitcode=cmd_write(opaque);
     858           0 :        if (!exitcode)
     859             :        {
     860           0 :           debug("wrote  %s \n",newarg[1]);
     861           0 :           Write_dyn_trace(trace, green,"[OK]\n");
     862             :        }
     863           0 :        else Write_dyn_trace(trace, red,"[FAILED]\n");
     864             :    }
     865             :    else
     866             :   {
     867           0 :       Write_dyn_trace(trace, red,"[FAILED]\n");
     868           0 :       exitcode=1;
     869             :    }
     870           0 :    if (mem.memory) free(mem.memory);
     871           0 :    ((data_exchange_t *)opaque)->paramlist=saved_args;
     872           0 :    return exitcode;
     873             : }

Generated by: LCOV version 1.10