1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright 2017 GeoLabs SARL. All rights reserved. |
---|
5 | * |
---|
6 | * This work was supported by public funds received in the framework of GEOSUD, |
---|
7 | * a project (ANR-10-EQPX-20) of the program "Investissements d'Avenir" managed |
---|
8 | * by the French National Research Agency |
---|
9 | * |
---|
10 | * This work was supported by public funds received in the framework of GEOSUD, |
---|
11 | * a project (ANR-10-EQPX-20) of the program "Investissements d'Avenir" managed |
---|
12 | * by the French National Research Agency |
---|
13 | * |
---|
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
15 | * of this software and associated documentation files (the "Software"), to deal |
---|
16 | * in the Software without restriction, including without limitation the rights |
---|
17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
18 | * copies of the Software, and to permit persons to whom the Software is |
---|
19 | * furnished to do so, subject to the following conditions: |
---|
20 | * |
---|
21 | * The above copyright notice and this permission notice shall be included in |
---|
22 | * all copies or substantial portions of the Software. |
---|
23 | * |
---|
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
30 | * THE SOFTWARE. |
---|
31 | */ |
---|
32 | |
---|
33 | |
---|
34 | #include "service.h" |
---|
35 | #include "service_internal.h" |
---|
36 | |
---|
37 | #include <libxml/tree.h> |
---|
38 | #include <libxml/parser.h> |
---|
39 | #include <libxml/xpath.h> |
---|
40 | #include <libxml/xpathInternals.h> |
---|
41 | |
---|
42 | #include <libxslt/xslt.h> |
---|
43 | #include <libxslt/xsltInternals.h> |
---|
44 | #include <libxslt/transform.h> |
---|
45 | #include <libxslt/xsltutils.h> |
---|
46 | |
---|
47 | #include <dirent.h> |
---|
48 | #include <string.h> |
---|
49 | #include <cerrno> |
---|
50 | |
---|
51 | extern "C" { |
---|
52 | |
---|
53 | /** |
---|
54 | * Retrieve the error message which occured when trying to remove a file |
---|
55 | * (return values can be EACCES, EBUSY, EFAULT, EIO, EISDIR, ELOOP, |
---|
56 | * ENAMETOOLONG, ENOENT, ENOMEM, ENOTDIR, EPERM, EROFS, UNKNOWN). |
---|
57 | * |
---|
58 | * @param errn |
---|
59 | * @result a string corresponding to the error number |
---|
60 | */ |
---|
61 | char* fetchErrno(int errn){ |
---|
62 | switch(errn){ |
---|
63 | case EACCES: |
---|
64 | return "EACCES"; |
---|
65 | case EBUSY: |
---|
66 | return "EBUSY"; |
---|
67 | case EFAULT: |
---|
68 | return "EFAULT"; |
---|
69 | case EIO: |
---|
70 | return "EIO"; |
---|
71 | case EISDIR: |
---|
72 | return "EISDIR"; |
---|
73 | case ELOOP: |
---|
74 | return "ELOOP"; |
---|
75 | case ENAMETOOLONG: |
---|
76 | return "ENAMETOOLONG"; |
---|
77 | case ENOENT: |
---|
78 | return "ENOENT"; |
---|
79 | case ENOMEM: |
---|
80 | return "ENOMEM"; |
---|
81 | case ENOTDIR: |
---|
82 | return "ENOTDIR"; |
---|
83 | case EPERM: |
---|
84 | return "EPERM"; |
---|
85 | case EROFS: |
---|
86 | return "EROFS"; |
---|
87 | default: |
---|
88 | return "UNKNOWN"; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Try to delete a cache file. |
---|
94 | * |
---|
95 | * @param storage the full path to store the file |
---|
96 | * @param filename the filename to store |
---|
97 | * @param ext the extention of the file |
---|
98 | * @return 0 in case of success |
---|
99 | */ |
---|
100 | int tryDeleteCacheFile(const char* storage,const char* filename,const char* ext){ |
---|
101 | char *filename_noext=(char*)malloc((strlen(filename)-3)*sizeof(char)); |
---|
102 | snprintf(filename_noext,strlen(filename)-4,"%s",filename); |
---|
103 | char* filename_full=(char*)malloc((strlen(filename)+1)*sizeof(char)); |
---|
104 | sprintf(filename_full,"%s.%s",filename_noext,ext); |
---|
105 | char* fullpath=(char*)malloc((strlen(storage)+strlen(filename_full)+2)*sizeof(char)); |
---|
106 | sprintf(fullpath,"%s/%s",storage,filename_full); |
---|
107 | if(unlink(fullpath)==0){ |
---|
108 | // TODO store the filename_full in the deletedfiles |
---|
109 | fprintf(stderr,"#### DeleteData #### %s %d %s has been successfully deleted\n",__FILE__,__LINE__,filename_full); |
---|
110 | }else{ |
---|
111 | fprintf(stderr,"#### DeleteData #### unable to delete %s \n",fullpath); |
---|
112 | } |
---|
113 | free(fullpath); |
---|
114 | free(filename_full); |
---|
115 | return 0; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Try to delete a data file. |
---|
120 | * |
---|
121 | * @param storage the full path to store the file |
---|
122 | * @param filename the filename to store |
---|
123 | * @return 0 in case of success |
---|
124 | */ |
---|
125 | int tryDeleteDataFile(const char* storage,const char* filename){ |
---|
126 | char* fullpath=NULL; |
---|
127 | if(filename!=NULL){ |
---|
128 | fullpath=(char*)malloc((strlen(storage)+strlen(filename)+2)*sizeof(char)); |
---|
129 | sprintf(fullpath,"%s/%s",storage,filename); |
---|
130 | } |
---|
131 | else |
---|
132 | fullpath=zStrdup(storage); |
---|
133 | if(unlink(fullpath)==0){ |
---|
134 | // TODO store the filename_full in the deletedfiles |
---|
135 | fprintf(stderr,"#### DeleteData #### %s %d %s has been successfully deleted\n",__FILE__,__LINE__,(filename!=NULL && strlen(filename)>0?filename:fullpath)); |
---|
136 | }else{ |
---|
137 | fprintf(stderr,"#### DeleteData #### unable to delete %s %s \n",fullpath,fetchErrno(errno)); |
---|
138 | } |
---|
139 | free(fullpath); |
---|
140 | return 0; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /** |
---|
145 | * DeleteData ZOO-Service : |
---|
146 | * This service is used in the ZOO-Project to delete the data file |
---|
147 | * associated with input or output. |
---|
148 | */ |
---|
149 | ZOO_DLL_EXPORT int DeleteData(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
150 | map* dataPath=getMapFromMaps(conf,"main","dataPath"); |
---|
151 | map* tmpPath=getMapFromMaps(conf,"main","tmpPath"); |
---|
152 | map* cacheDir=getMapFromMaps(conf,"main","cacheDir"); |
---|
153 | map* jobid=getMapFromMaps(inputs,"jobid","value"); |
---|
154 | map* filename=getMapFromMaps(inputs,"filename","value"); |
---|
155 | map* ioname=getMapFromMaps(inputs,"ioname","value"); |
---|
156 | char tmp0[4]; |
---|
157 | sprintf(tmp0,"%c%c%c",filename->value[strlen(filename->value)-3],filename->value[strlen(filename->value)-2],filename->value[strlen(filename->value)-1]); |
---|
158 | char *cfilename=NULL; |
---|
159 | if(strcasecmp(tmp0,"zca")==0){ |
---|
160 | cfilename=(char*) malloc((strlen(filename->value)+strlen(cacheDir->value)+2)*sizeof(char)); |
---|
161 | sprintf(cfilename,"%s/%s",cacheDir->value,filename->value); |
---|
162 | } |
---|
163 | else{ |
---|
164 | cfilename=(char*) malloc((strlen(filename->value)+strlen(tmpPath->value)+2)*sizeof(char)); |
---|
165 | sprintf(cfilename,"%s/%s",tmpPath->value,filename->value); |
---|
166 | } |
---|
167 | zooLock* lck=lockFile(conf,cfilename,'w'); |
---|
168 | char** deletedfiles; |
---|
169 | if(lck!=NULL){ |
---|
170 | if(strcasecmp(tmp0,"zca")==0){ |
---|
171 | // Read the zcp file to verify if it comes from a shared source |
---|
172 | char *filename_noext=(char*)malloc((strlen(filename->value)-3)*sizeof(char)); |
---|
173 | snprintf(filename_noext,strlen(filename->value)-4,"%s",filename->value); |
---|
174 | char* filename_full=(char*)malloc((strlen(filename->value)+1)*sizeof(char)); |
---|
175 | sprintf(filename_full,"%s.zcp",filename_noext); |
---|
176 | char* fullpath=(char*)malloc((strlen(cacheDir->value)+strlen(filename_full)+2)*sizeof(char)); |
---|
177 | sprintf(fullpath,"%s/%s",cacheDir->value,filename_full); |
---|
178 | FILE* f0=fopen(fullpath,"rb"); |
---|
179 | char *fcontent=NULL; |
---|
180 | if(f0!=NULL){ |
---|
181 | long flen; |
---|
182 | fseek (f0, 0, SEEK_END); |
---|
183 | flen = ftell (f0); |
---|
184 | fseek (f0, 0, SEEK_SET); |
---|
185 | fcontent = (char *) malloc ((flen + 1) * sizeof (char)); |
---|
186 | fread(fcontent,flen,1,f0); |
---|
187 | fcontent[flen]=0; |
---|
188 | fclose(f0); |
---|
189 | } |
---|
190 | if(fcontent!=NULL && strcasecmp(fcontent,"SHARED")!=0){ |
---|
191 | // Delete associated zcm, zcp and maps files |
---|
192 | tryDeleteCacheFile(cacheDir->value,filename->value,"zca"); |
---|
193 | tryDeleteCacheFile(cacheDir->value,filename->value,"zcm"); |
---|
194 | tryDeleteCacheFile(cacheDir->value,filename->value,"zcp"); |
---|
195 | tryDeleteCacheFile(tmpPath->value,filename->value,"maps"); |
---|
196 | // Delete <input>_<sid>.map |
---|
197 | char* datafile=(char*)malloc((strlen(jobid->value)+strlen(ioname->value)+19)*sizeof(char)); |
---|
198 | sprintf(datafile,"%s_%s.map",ioname->value,jobid->value); |
---|
199 | tryDeleteDataFile(dataPath->value,datafile); |
---|
200 | free(datafile); |
---|
201 | }else{ |
---|
202 | setMapInMaps(conf,"lenv","message",_ss("The file you try to delete is a shared ressource and cannot be deleted by nature.")); |
---|
203 | unlockFile(conf,lck); |
---|
204 | return SERVICE_FAILED; |
---|
205 | } |
---|
206 | setMapInMaps(outputs,"Result","value",_ss("The input data has been correclty removed")); |
---|
207 | }else{ |
---|
208 | char tmp1[8]; |
---|
209 | snprintf(tmp1,7,"%s",filename->value); |
---|
210 | if(strcasecmp(tmp1,"output")==0 || strstr(filename->value,".zca")!=NULL || strcasecmp(tmp1,"input_")==0){ |
---|
211 | tryDeleteDataFile(tmpPath->value,filename->value); |
---|
212 | char *tmp=zStrdup(filename->value); |
---|
213 | tmp[strlen(tmp)-strlen(strrchr(tmp,'.'))]=0; |
---|
214 | char *mapsfile=(char*)malloc((strlen(tmp)+6)*sizeof(char)); |
---|
215 | sprintf(mapsfile,"%s.maps",tmp); |
---|
216 | char* mapPath=(char*)malloc((strlen(tmpPath->value)+strlen(mapsfile)+2)*sizeof(char)); |
---|
217 | sprintf(mapPath,"%s/%s",tmpPath->value,mapsfile); |
---|
218 | FILE* myMapsfile=fopen(mapPath,"r"); |
---|
219 | if(myMapsfile!=NULL){ |
---|
220 | char *buffer=(char*)malloc(1024*sizeof(char)); |
---|
221 | while(fgets(buffer, 1024, myMapsfile) != NULL){ |
---|
222 | buffer[strlen(buffer)-1]=0; |
---|
223 | tryDeleteDataFile(buffer,NULL); |
---|
224 | } |
---|
225 | free(buffer); |
---|
226 | fclose(myMapsfile); |
---|
227 | } |
---|
228 | tryDeleteDataFile(mapPath,NULL); |
---|
229 | free(mapPath); |
---|
230 | free(mapsfile); |
---|
231 | // Delete ZOO_DATA_<output>_<sid>.data and <output>_<sid>.map |
---|
232 | char* datafile=(char*)malloc((strlen(jobid->value)+strlen(ioname->value)+19)*sizeof(char)); |
---|
233 | sprintf(datafile,"ZOO_DATA_%s_%s.data",ioname->value,jobid->value); |
---|
234 | tryDeleteDataFile(dataPath->value,datafile); |
---|
235 | free(datafile); |
---|
236 | datafile=(char*)malloc((strlen(jobid->value)+strlen(ioname->value)+19)*sizeof(char)); |
---|
237 | sprintf(datafile,"%s_%s.map",ioname->value,jobid->value); |
---|
238 | tryDeleteDataFile(dataPath->value,datafile); |
---|
239 | free(datafile); |
---|
240 | char* webServices[3]={"wms","wfs","wcs"}; |
---|
241 | int i=0; |
---|
242 | // Delete ZOO_DATA_<ws>_link_<sid>.data and <ws>_link_<sid>.map |
---|
243 | // with <ws> is the corresponding OGC web service (wms,wfs,wcs) |
---|
244 | for(i=0;i<3;i++){ |
---|
245 | datafile=(char*)malloc((strlen(jobid->value)+24)*sizeof(char)); |
---|
246 | sprintf(datafile,"ZOO_DATA_%s_link_%s.data",webServices[i],jobid->value); |
---|
247 | tryDeleteDataFile(dataPath->value,datafile); |
---|
248 | free(datafile); |
---|
249 | datafile=(char*)malloc((strlen(jobid->value)+14)*sizeof(char)); |
---|
250 | sprintf(datafile,"%s_link_%s.map",webServices[i],jobid->value); |
---|
251 | tryDeleteDataFile(dataPath->value,datafile); |
---|
252 | free(datafile); |
---|
253 | } |
---|
254 | setMapInMaps(outputs,"Result","value",_ss("The output data has been correclty removed")); |
---|
255 | }else{ |
---|
256 | setMapInMaps(conf,"lenv","message",_ss("The file you try to delete is nor an input, nor an output.")); |
---|
257 | unlockFile(conf,lck); |
---|
258 | return SERVICE_FAILED; |
---|
259 | } |
---|
260 | } |
---|
261 | unlockFile(conf,lck); |
---|
262 | } |
---|
263 | else{ |
---|
264 | setMapInMaps(conf,"lenv","message",_ss("Failed to acquire lock for deletion, please try again later.")); |
---|
265 | return SERVICE_FAILED; |
---|
266 | } |
---|
267 | return SERVICE_SUCCEEDED; |
---|
268 | } |
---|
269 | |
---|
270 | } |
---|