1 /***************************************************************
2  Copyright (C) 2010-2014 Hewlett-Packard Development Company, L.P.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 
17 ***************************************************************/
18 #ifndef _BUCKETS_H
19 #define _BUCKETS_H 1
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <strings.h>
25 #include <ctype.h>
26 #include <regex.h>
27 #include <libgen.h>
28 #include <getopt.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/wait.h>
34 
35 #include <libfossology.h>
36 #include "liccache.h"
37 #define FUNCTION
38 
39 #define myBUFSIZ       2048
40 #define MAXSQL         1024
41 
42 #define IsContainer(mode)  ((mode & 1<<29) != 0)
43 #define IsArtifact(mode)   ((mode & 1<<28) != 0)
44 
45 
46 /**
47  * struct regex_file_struct
48  * \brief REGEX-FILE bucket type
49  *
50  * The ftypes tell you what data to apply the regex to.
51  */
52 struct regex_file_struct
53 {
54   int      ftype1;          /**< 1=filename, 2=license */
55   char    *regex1;          /**< regex1 string */
56   regex_t  compRegex1;
57   int      op;              /**< 0=end of expression, 1=and, 2=or, 3=not  */
58   int      ftype2;          /**< 1=filename, 2=license */
59   char    *regex2;          /**< regex2 string */
60   regex_t  compRegex2;
61 };
62 typedef struct regex_file_struct regex_file_t, *pregex_file_t;
63 
64 /**
65  * struct bucketdef_struct
66  * Bucket definition
67  */
68 struct bucketdef_struct
69 {
70   int      bucket_pk;       /**< bucket id */
71   char    *bucket_name;     /**< bucker name */
72   int      bucket_type;     /**< 1=MATCH_EVERY, 2=MATCH_ONLY, 3=REGEX, 4=EXEC, 5=REGEX-FILE, 99=Not in any other bucket. */
73   char    *regex;           /**< regex string */
74   regex_t  compRegex;       /**< compiled regex if type=3 */
75   char    *dataFilename;    /**< File in PROJECTSTATEDIR */
76   int     *match_only;      /**< array of rf_pk's if type=2 */
77   int    **match_every;     /**< list of arrays of rf_pk's if type=1 */
78   regex_file_t *regex_row;  /**< array of regex_file_structs if type=5 */
79   char     stopon;          /**< Y to stop procecessing if this bucket matches */
80   char     applies_to;      /**< 'f'=every file, 'p'=packages only  */
81   int      nomos_agent_pk;  /**< nomos agent_pk whose results this bucket scan is using */
82   int      bucket_agent_pk; /**< bucket agent_pk */
83   int      bucketpool_pk;
84   char    *uploadtree_tablename;
85 };
86 typedef struct bucketdef_struct bucketdef_t, *pbucketdef_t;
87 
88 /**
89  * struct bucketpool_struct
90  * Bucket pool
91  *
92  * NOTE: This struct is not currently used.  When it is, move
93  * nomos_agent_pk, bucket_agent_pk to here and remove from bucketdef
94 */
95 struct bucketpool_struct
96 {
97   int  bucketpool_pk;
98   int  bucketpool_name;
99   int  bucketpool_version;
100   int  nomos_agent_pk;
101   int  bucket_agent_pk;
102   pbucketdef_t pbucketdef;  /**< array of bucketdef's which define all the buckets for this pool  */
103 };
104 typedef struct bucketpool_struct bucketpool_t, *pbucketpool_t;
105 
106 /**
107  * struct uploadtree_struct
108  * uploadtree record values
109  */
110 struct uploadtree_struct
111 {
112   int      uploadtree_pk;  /**< Upload tree id */
113   char    *ufile_name;     /**< Upload name */
114   int      upload_fk;      /**< Upload id */
115   int      ufile_mode;     /**< Upload mode */
116   int      pfile_fk;       /**< Pfile id */
117   int      lft;            /**< Left child range */
118   int      rgt;            /**< Right child range */
119 };
120 typedef struct uploadtree_struct uploadtree_t, *puploadtree_t;
121 
122 /**
123  * struct package_struct
124  * package record values
125  */
126 struct package_struct
127 {
128   char pkgname[256];       /**< Package name */
129   char pkgvers[256];       /**< Package version */
130   char vendor[256];        /**< Vendor name */
131   char srcpkgname[256];    /**< Source name */
132 };
133 typedef struct package_struct package_t, *ppackage_t;
134 
135 /* walk.c */
136 int walkTree(PGconn *pgConn, pbucketdef_t bucketDefArray, int agent_pk,
137              int uploadtree_pk, int skipProcessedCheck,
138              int hasPrules);
139 
140 int processFile(PGconn *pgConn, pbucketdef_t bucketDefArray,
141                       puploadtree_t puploadtree, int agent_pk, int hasPrules);
142 
143 /* leaf.c */
144 int processLeaf(PGconn *pgConn, pbucketdef_t bucketDefArray,
145                 puploadtree_t puploadtree, ppackage_t ppackage,
146                 int agent_pk, int hasPrules);
147 
148 int *getLeafBuckets(PGconn *pgConn, pbucketdef_t bucketDefArray,
149                     puploadtree_t puploadtree, ppackage_t ppackage,
150                     int hasPrules);
151 
152 /* container.c */
153 int *getContainerBuckets(PGconn *pgConn, pbucketdef_t bucketDefArray, int uploadtree_pk);
154 
155 /* child.c */
156 int childInBucket(PGconn *pgConn, pbucketdef_t in_bucketDef, puploadtree_t puploadtree);
157 
158 /* write.c */
159 int writeBuckets(PGconn *pgConn, int pfile_pk, int uploadtree_pk,
160                  int *bucketList, int agent_pk, int nomosagent_pk, int bucketpool_pk);
161 
162 /* match.c */
163 int matchAnyLic(PGresult *result, int numLics, regex_t *compRegex);
164 
165 
166 /* validate.c */
167 int arrayAinB        (int *arrayA, int *arrayB);
168 int intAinB          (int intA, int *arrayB);
169 int validate_pk      (PGconn *pgConn, char *sql);
170 void Usage           (char *Name);
171 int processed        (PGconn *pgConn, int agent_pk, int nomos_agent_pk, int pfile_pk, int uploadtree_pk, int bucketpool_pk, int bucket_pk);
172 int UploadProcessed  (PGconn *pgConn, int bucketagent_pk, int nomosagent_pk, int pfile_pk, int uploadtree_pk, int upload_pk, int bucketpool_pk);
173 
174 /* inits.c */
175 pbucketdef_t initBuckets   (PGconn *pgConn, int bucketpool_pk, cacheroot_t *pcroot);
176 int *getMatchOnly    (PGconn *pgConn, int bucketpool_pk, char *filename, cacheroot_t *pcroot);
177 int **getMatchEvery  (PGconn *pgConn, int bucketpool_pk, char *filename, cacheroot_t *pcroot);
178 regex_file_t *getRegexFile  (PGconn *pgConn, int bucketpool_pk, char *filename, cacheroot_t *pcroot);
179 int getRegexFiletype (char *token, char *filepath);
180 int getBucketpool_pk (PGconn *pgConn, char * bucketpool_name);
181 int LatestNomosAgent(PGconn *pgConn, int upload_pk);
182 int *getLicsInStr    (PGconn *pgConn, char *nameStr, cacheroot_t *pcroot);
183 int childParent      (PGconn *pgConn, int uploadtree_pk);
184 
185 #endif /* _BUCKETS_H */
186