FOSSology  4.4.0
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  SPDX-FileCopyrightText: © 2007-2011 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: LGPL-2.1-only
6 */
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <ctype.h>
20 #include "libfossrepo.h"
21 
22 #ifdef COMMIT_HASH
23 char BuildVersion[]="Build version: " COMMIT_HASH ".\n";
24 #endif
25 
26 /*** GLOBALS (for stats) ***/
27 long TotalImported = 0;
28 long TotalDuplicate = 0;
29 long TotalError = 0;
30 
31 
39 void CopyFile(char* Source, char* Type, char* Name)
40 {
41  int rc;
42  rc = fo_RepExist(Type, Name);
43  if (rc == 1)
44  {
45  TotalDuplicate++;
46  return;
47  }
48  else if (rc == -1)
49  {
50  TotalError++;
51  return;
52  }
53  if (fo_RepImport(Source, Type, Name, 1) == 0) TotalImported++;
54  else TotalError++;
55 } /* CopyFile() */
56 
64 int ReadLine(FILE* Fin, char* Line, int MaxLine)
65 {
66  int C = '@';
67  int i = 0; /* index */
68  memset(Line, 0, MaxLine);
69  if (feof(Fin)) return (-1);
70  while (!feof(Fin) && (i < MaxLine - 1) && (C != '\n') && (C > 0))
71  {
72  C = fgetc(Fin);
73  if ((C > 0) && (C != '\n'))
74  {
75  Line[i] = C;
76  i++;
77  }
78  }
79  return (i);
80 } /* ReadLine() */
81 
87 int Hex2Dec(char C)
88 {
89  if (!isxdigit(C)) return (0);
90  if (isdigit(C)) return (C - '0');
91  if (isupper(C)) return (C - 'A' + 10);
92  return (C - 'a' + 10);
93 } /* Hex2Dec() */
94 
100 int UnUnicodeHex(char* S)
101 {
102  int v;
103  if ((S[0] == '&') && (S[1] == '#') && (S[2] == 'x')
104  && isxdigit(S[3]) && isxdigit(S[4]) && (S[5] == ';'))
105  {
106  v = Hex2Dec(S[3]) * 16 + Hex2Dec(S[4]);
107  return (v);
108  }
109  return (-1);
110 } /* UnUnicodeHex() */
111 
117 void ProcessPairs(FILE* Fin, char* Type)
118 {
119  char Buf[10240];
120  char Dst[FILENAME_MAX];
121  char Src[FILENAME_MAX];
122  int Space;
123  int i, s, c;
124 
125  while (ReadLine(Fin, Buf, sizeof(Buf)) > 0)
126  {
127  Space = 0;
128  /* save the dst name */
129  while ((Buf[Space] != '\0') && (Buf[Space] != ' ')) Space++;
130  strncpy(Dst, Buf, Space);
131  /* skip the space */
132  /* save the src name and remove unicode stuff */
133  memset(Dst, '\0', sizeof(Dst));
134  memset(Src, '\0', sizeof(Src));
135  strncpy(Dst, Buf, Space);
136  s = 0;
137  for (i = Space + 1; Buf[i] != '\0'; i++)
138  {
139  if (Buf[i] != '&') Src[s++] = Buf[i];
140  else
141  {
142  c = UnUnicodeHex(Buf + i);
143  if (c >= 0)
144  {
145  Src[s++] = c;
146  i += 5;
147  }
148  else Src[s++] = Buf[i];
149  }
150  }
151 #if 0
152  printf("Dst='%s' Src='%s'\n",Dst,Src);
153 #endif
154  CopyFile(Src, Type, Dst);
155  }
156 } /* ProcessPairs() */
157 
168 void ProcessXML(FILE* Fin, char* TypeSource, char* Type)
169 {
170  char Buf[10240];
171  char Dst[FILENAME_MAX];
172  char Src[FILENAME_MAX];
173  int i, j, c;
174  char* fuid;
175  char* source;
176  char* field;
177  char* FileType;
178 
179  /* From ununpack: each line is either an item or a /item. */
180  while (ReadLine(Fin, Buf, sizeof(Buf)) > 0)
181  {
182  /* only process known item types */
183  if (!strncmp(Buf, "<item ", 6)) FileType = Type;
184  else if (!strncmp(Buf, "<source ", 8)) FileType = TypeSource;
185  else continue; /* skip this type */
186 
187  /* skip items without FUID values */
188  fuid = strstr(Buf, " fuid=\"");
189  if (!fuid) continue;
190  fuid += 7; /* jump to start of string; string ends with quote */
191 
192  /* find the source */
193  source = strstr(Buf, " source=\"");
194  if (!source) continue;
195  source += 9; /* jump to start of string; string ends with quote */
196 
197  /* skip artifacts and directories */
198  field = strstr(Buf, " artifact=\"1\"");
199  if (field) continue;
200  field = strstr(Buf, " mime=\"directory\"");
201  if (field) continue;
202 
203  /* save the src name and remove unicode stuff */
204  memset(Dst, '\0', sizeof(Dst));
205  memset(Src, '\0', sizeof(Src));
206  j = 0;
207  for (i = 0; source[i] != '\"'; i++)
208  {
209  if (source[i] != '&') Src[j++] = source[i];
210  else
211  {
212  c = UnUnicodeHex(source + i);
213  if (c >= 0)
214  {
215  Src[j++] = c;
216  i += 5;
217  }
218  else Src[j++] = source[i];
219  }
220  }
221  j = 0;
222  for (i = 0; fuid[i] != '\"'; i++)
223  {
224  if (fuid[i] != '&') Dst[j++] = fuid[i];
225  else
226  {
227  c = UnUnicodeHex(fuid + i);
228  if (c >= 0)
229  {
230  Dst[j++] = c;
231  i += 5;
232  }
233  else Dst[j++] = fuid[i];
234  }
235  }
236 #if 0
237  printf("Dst='%s' Src='%s'\n",Dst,Src);
238 #endif
239  CopyFile(Src, FileType, Dst);
240  /* always make sure a copy is put in the regular file repository */
241  if (FileType != Type) CopyFile(Src, Type, Dst);
242  }
243 } /* ProcessXML() */
244 
245 /*******************************************************************/
246 /*******************************************************************/
247 /*******************************************************************/
248 int main(int argc, char* argv[])
249 {
250  if ((argc < 2) || (argc > 4))
251  {
252  fprintf(stderr, "Usage: (depends on the parameters)\n");
253  fprintf(stderr, " %s type filename sha1.md5.len\n", argv[0]);
254  fprintf(stderr, " echo 'sha1.md5.len filename' | %s type\n", argv[0]);
255  fprintf(stderr, " echo '<xml from ununpack>...</xml>' | %s typesource type\n", argv[0]);
256  fprintf(stderr, " type = repository for storing files\n");
257  fprintf(stderr, " typesource = repository for storing source files (XML only)\n");
258  exit(-1);
259  }
260 
261  switch (argc)
262  {
263  case 2: /* pairs from stdin */
264  ProcessPairs(stdin, argv[1]);
265  break;
266  case 3: /* pairs from XML */
267  ProcessXML(stdin, argv[1], argv[2]);
268  break;
269  case 4: /* pairs from command-line */
270  CopyFile(argv[2], argv[1], argv[3]);
271  break;
272  }
273 
274  printf("Total Imported: %ld\n", TotalImported);
275  printf("Total Duplicates: %ld\n", TotalDuplicate);
276  printf("Total Errors: %ld\n", TotalError);
277  if (TotalError > 0) return (1);
278  return (0);
279 } /* main() */
280 
char BuildVersion[]
Definition: buckets.c:68
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:37
int fo_RepImport(char *Source, char *Type, char *Filename, int Link)
Import a file into the repository.
Definition: libfossrepo.c:812
int fo_RepExist(char *Type, char *Filename)
Determine if a file exists.
Definition: libfossrepo.c:486
void CopyFile(char *Source, char *Type, char *Name)
Definition: repcopyin.c:39
void ProcessXML(FILE *Fin, char *TypeSource, char *Type)
Process Ununpack XML.
Definition: repcopyin.c:168
int UnUnicodeHex(char *S)
Definition: repcopyin.c:100
int Hex2Dec(char C)
Definition: repcopyin.c:87
void ProcessPairs(FILE *Fin, char *Type)
Definition: repcopyin.c:117
int ReadLine(FILE *Fin, char *Line, int MaxLine)
Definition: repcopyin.c:64
static char * Dst
Destination location.
Definition: test_CopyFile.c:14
static char * Src
Souce location.
Definition: test_CopyFile.c:13