FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
repcopyin.c
Go to the documentation of this file.
1 /****************************************************************
2 repcopyin: Copy a file into the repository.
3 
4 Copyright (C) 2007-2011 Hewlett-Packard Development Company, L.P.
5 
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License version 2.1 as published by the Free Software Foundation.
9 
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License
16 along with this library; if not, write to the Free Software Foundation, Inc.0
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 ****************************************************************/
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include "libfossrepo.h"
34 
35 #ifdef COMMIT_HASH
36 char BuildVersion[]="Build version: " COMMIT_HASH ".\n";
37 #endif
38 
39 /*** GLOBALS (for stats) ***/
40 long TotalImported = 0;
41 long TotalDuplicate = 0;
42 long TotalError = 0;
43 
44 
52 void CopyFile(char* Source, char* Type, char* Name)
53 {
54  int rc;
55  rc = fo_RepExist(Type, Name);
56  if (rc == 1)
57  {
58  TotalDuplicate++;
59  return;
60  }
61  else if (rc == -1)
62  {
63  TotalError++;
64  return;
65  }
66  if (fo_RepImport(Source, Type, Name, 1) == 0) TotalImported++;
67  else TotalError++;
68 } /* CopyFile() */
69 
77 int ReadLine(FILE* Fin, char* Line, int MaxLine)
78 {
79  int C = '@';
80  int i = 0; /* index */
81  memset(Line, 0, MaxLine);
82  if (feof(Fin)) return (-1);
83  while (!feof(Fin) && (i < MaxLine - 1) && (C != '\n') && (C > 0))
84  {
85  C = fgetc(Fin);
86  if ((C > 0) && (C != '\n'))
87  {
88  Line[i] = C;
89  i++;
90  }
91  }
92  return (i);
93 } /* ReadLine() */
94 
100 int Hex2Dec(char C)
101 {
102  if (!isxdigit(C)) return (0);
103  if (isdigit(C)) return (C - '0');
104  if (isupper(C)) return (C - 'A' + 10);
105  return (C - 'a' + 10);
106 } /* Hex2Dec() */
107 
113 int UnUnicodeHex(char* S)
114 {
115  int v;
116  if ((S[0] == '&') && (S[1] == '#') && (S[2] == 'x')
117  && isxdigit(S[3]) && isxdigit(S[4]) && (S[5] == ';'))
118  {
119  v = Hex2Dec(S[3]) * 16 + Hex2Dec(S[4]);
120  return (v);
121  }
122  return (-1);
123 } /* UnUnicodeHex() */
124 
130 void ProcessPairs(FILE* Fin, char* Type)
131 {
132  char Buf[10240];
133  char Dst[FILENAME_MAX];
134  char Src[FILENAME_MAX];
135  int Space;
136  int i, s, c;
137 
138  while (ReadLine(Fin, Buf, sizeof(Buf)) > 0)
139  {
140  Space = 0;
141  /* save the dst name */
142  while ((Buf[Space] != '\0') && (Buf[Space] != ' ')) Space++;
143  strncpy(Dst, Buf, Space);
144  /* skip the space */
145  /* save the src name and remove unicode stuff */
146  memset(Dst, '\0', sizeof(Dst));
147  memset(Src, '\0', sizeof(Src));
148  strncpy(Dst, Buf, Space);
149  s = 0;
150  for (i = Space + 1; Buf[i] != '\0'; i++)
151  {
152  if (Buf[i] != '&') Src[s++] = Buf[i];
153  else
154  {
155  c = UnUnicodeHex(Buf + i);
156  if (c >= 0)
157  {
158  Src[s++] = c;
159  i += 5;
160  }
161  else Src[s++] = Buf[i];
162  }
163  }
164 #if 0
165  printf("Dst='%s' Src='%s'\n",Dst,Src);
166 #endif
167  CopyFile(Src, Type, Dst);
168  }
169 } /* ProcessPairs() */
170 
181 void ProcessXML(FILE* Fin, char* TypeSource, char* Type)
182 {
183  char Buf[10240];
184  char Dst[FILENAME_MAX];
185  char Src[FILENAME_MAX];
186  int i, j, c;
187  char* fuid;
188  char* source;
189  char* field;
190  char* FileType;
191 
192  /* From ununpack: each line is either an item or a /item. */
193  while (ReadLine(Fin, Buf, sizeof(Buf)) > 0)
194  {
195  /* only process known item types */
196  if (!strncmp(Buf, "<item ", 6)) FileType = Type;
197  else if (!strncmp(Buf, "<source ", 8)) FileType = TypeSource;
198  else continue; /* skip this type */
199 
200  /* skip items without FUID values */
201  fuid = strstr(Buf, " fuid=\"");
202  if (!fuid) continue;
203  fuid += 7; /* jump to start of string; string ends with quote */
204 
205  /* find the source */
206  source = strstr(Buf, " source=\"");
207  if (!source) continue;
208  source += 9; /* jump to start of string; string ends with quote */
209 
210  /* skip artifacts and directories */
211  field = strstr(Buf, " artifact=\"1\"");
212  if (field) continue;
213  field = strstr(Buf, " mime=\"directory\"");
214  if (field) continue;
215 
216  /* save the src name and remove unicode stuff */
217  memset(Dst, '\0', sizeof(Dst));
218  memset(Src, '\0', sizeof(Src));
219  j = 0;
220  for (i = 0; source[i] != '\"'; i++)
221  {
222  if (source[i] != '&') Src[j++] = source[i];
223  else
224  {
225  c = UnUnicodeHex(source + i);
226  if (c >= 0)
227  {
228  Src[j++] = c;
229  i += 5;
230  }
231  else Src[j++] = source[i];
232  }
233  }
234  j = 0;
235  for (i = 0; fuid[i] != '\"'; i++)
236  {
237  if (fuid[i] != '&') Dst[j++] = fuid[i];
238  else
239  {
240  c = UnUnicodeHex(fuid + i);
241  if (c >= 0)
242  {
243  Dst[j++] = c;
244  i += 5;
245  }
246  else Dst[j++] = fuid[i];
247  }
248  }
249 #if 0
250  printf("Dst='%s' Src='%s'\n",Dst,Src);
251 #endif
252  CopyFile(Src, FileType, Dst);
253  /* always make sure a copy is put in the regular file repository */
254  if (FileType != Type) CopyFile(Src, Type, Dst);
255  }
256 } /* ProcessXML() */
257 
258 /*******************************************************************/
259 /*******************************************************************/
260 /*******************************************************************/
261 int main(int argc, char* argv[])
262 {
263  if ((argc < 2) || (argc > 4))
264  {
265  fprintf(stderr, "Usage: (depends on the parameters)\n");
266  fprintf(stderr, " %s type filename sha1.md5.len\n", argv[0]);
267  fprintf(stderr, " echo 'sha1.md5.len filename' | %s type\n", argv[0]);
268  fprintf(stderr, " echo '<xml from ununpack>...</xml>' | %s typesource type\n", argv[0]);
269  fprintf(stderr, " type = repository for storing files\n");
270  fprintf(stderr, " typesource = repository for storing source files (XML only)\n");
271  exit(-1);
272  }
273 
274  switch (argc)
275  {
276  case 2: /* pairs from stdin */
277  ProcessPairs(stdin, argv[1]);
278  break;
279  case 3: /* pairs from XML */
280  ProcessXML(stdin, argv[1], argv[2]);
281  break;
282  case 4: /* pairs from command-line */
283  CopyFile(argv[2], argv[1], argv[3]);
284  break;
285  }
286 
287  printf("Total Imported: %ld\n", TotalImported);
288  printf("Total Duplicates: %ld\n", TotalDuplicate);
289  printf("Total Errors: %ld\n", TotalError);
290  if (TotalError > 0) return (1);
291  return (0);
292 } /* main() */
293 
int UnUnicodeHex(char *S)
Definition: repcopyin.c:113
char BuildVersion[]
Definition: buckets.c:79
int Hex2Dec(char C)
Definition: repcopyin.c:100
void ProcessPairs(FILE *Fin, char *Type)
Definition: repcopyin.c:130
void CopyFile(char *Source, char *Type, char *Name)
Definition: repcopyin.c:52
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:48
static char * Src
Souce location.
Definition: test_CopyFile.c:24
static char * Dst
Destination location.
Definition: test_CopyFile.c:25
void ProcessXML(FILE *Fin, char *TypeSource, char *Type)
Process Ununpack XML.
Definition: repcopyin.c:181
int ReadLine(FILE *Fin, char *Line, int MaxLine)
Definition: repcopyin.c:77
int fo_RepImport(char *Source, char *Type, char *Filename, int Link)
Import a file into the repository.
Definition: libfossrepo.c:824
int fo_RepExist(char *Type, char *Filename)
Determine if a file exists.
Definition: libfossrepo.c:498