[579] | 1 | /* |
---|
[1] | 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
[917] | 4 | * Copyright (c) 2009-2018 GeoLabs SARL |
---|
[1] | 5 | * |
---|
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 7 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 8 | * in the Software without restriction, including without limitation the rights |
---|
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 10 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 11 | * furnished to do so, subject to the following conditions: |
---|
| 12 | * |
---|
| 13 | * The above copyright notice and this permission notice shall be included in |
---|
| 14 | * all copies or substantial portions of the Software. |
---|
| 15 | * |
---|
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 22 | * THE SOFTWARE. |
---|
| 23 | */ |
---|
| 24 | |
---|
[730] | 25 | #define _LARGEFILE64_SOURCE 1 |
---|
[649] | 26 | #ifdef USE_MS |
---|
| 27 | #include "service_internal_ms.h" |
---|
| 28 | #else |
---|
| 29 | #include "cpl_vsi.h" |
---|
| 30 | #endif |
---|
[703] | 31 | #include "service_internal.h" |
---|
[216] | 32 | |
---|
[917] | 33 | #ifdef WIN32 |
---|
| 34 | // ref. https://docs.microsoft.com/en-us/windows/desktop/fileio/locking-and-unlocking-byte-ranges-in-files |
---|
| 35 | __inline int fcntl(int fd, int cmd, ...) |
---|
| 36 | { |
---|
| 37 | va_list a; |
---|
| 38 | va_start(a, cmd); |
---|
| 39 | switch(cmd) |
---|
| 40 | { |
---|
| 41 | case F_SETLK: |
---|
| 42 | { |
---|
| 43 | HANDLE h = (HANDLE)_get_osfhandle(fd); |
---|
| 44 | struct flock* l= va_arg(a, struct flock*); |
---|
| 45 | OVERLAPPED sOverlapped; |
---|
| 46 | sOverlapped.Offset = 0; |
---|
| 47 | sOverlapped.OffsetHigh = 0; |
---|
| 48 | switch(l->l_type) |
---|
| 49 | { |
---|
| 50 | case F_RDLCK: |
---|
| 51 | { |
---|
| 52 | if (!LockFileEx(h, LOCKFILE_FAIL_IMMEDIATELY, 0, l->l_len, 0, &sOverlapped)) |
---|
| 53 | { |
---|
| 54 | _set_errno(GetLastError() == ERROR_LOCK_VIOLATION ? EAGAIN : EBADF); |
---|
| 55 | return -1; |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | break; |
---|
| 59 | case F_WRLCK: |
---|
| 60 | { |
---|
| 61 | if (!LockFileEx(h, LOCKFILE_FAIL_IMMEDIATELY|LOCKFILE_EXCLUSIVE_LOCK, 0, l->l_len, 0, &sOverlapped)) |
---|
| 62 | { |
---|
| 63 | _set_errno(GetLastError() == ERROR_LOCK_VIOLATION ? EAGAIN : EBADF); |
---|
| 64 | return -1; |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | break; |
---|
| 68 | case F_UNLCK: |
---|
| 69 | { |
---|
| 70 | UnlockFileEx(h, 0, l->l_len, 0, &sOverlapped); |
---|
| 71 | } |
---|
| 72 | break; |
---|
| 73 | default: |
---|
| 74 | _set_errno(ENOTSUP); |
---|
| 75 | return -1; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | break; |
---|
| 79 | default: |
---|
| 80 | _set_errno(ENOTSUP); |
---|
| 81 | return -1; |
---|
| 82 | } |
---|
| 83 | return 0; |
---|
| 84 | } |
---|
[365] | 85 | #endif |
---|
[917] | 86 | #define ERROR_MSG_MAX_LENGTH 1024 |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * Lock a file for read, write and upload. |
---|
| 90 | * @param conf the main configuration maps |
---|
| 91 | * @param filename the file to lock |
---|
| 92 | * @param mode define access: 'r' for read, 'w' for write |
---|
| 93 | * @return a new zooLock structure on sucess, NULL on failure |
---|
| 94 | */ |
---|
| 95 | struct zooLock* lockFile(maps* conf,const char* filename,const char mode){ |
---|
| 96 | struct stat f_status; |
---|
| 97 | int itn=0; |
---|
| 98 | int s; |
---|
| 99 | struct zooLock* myLock=(struct zooLock*)malloc(sizeof(struct flock)+sizeof(FILE*)+sizeof(char*)); |
---|
| 100 | int len=6; |
---|
| 101 | char *myTemplate="%s.lock"; |
---|
| 102 | int res=-1; |
---|
| 103 | retryLockFile: |
---|
| 104 | myLock->filename=(char*)malloc((strlen(filename)+len)*sizeof(char)); |
---|
| 105 | sprintf(myLock->filename,myTemplate,filename); |
---|
| 106 | s=stat(myLock->filename, &f_status); |
---|
| 107 | if(s==0 && mode!='r'){ |
---|
| 108 | if(itn<ZOO_LOCK_MAX_RETRY){ |
---|
| 109 | itn++; |
---|
| 110 | #ifdef DEBUG |
---|
| 111 | fprintf(stderr,"(%d) Wait for write lock on %s, tried %d times (sleep) ... \n",zGetpid(),myLock->filename,itn); |
---|
| 112 | fflush(stderr); |
---|
[365] | 113 | #endif |
---|
[917] | 114 | zSleep(5); |
---|
| 115 | free(myLock->filename); |
---|
| 116 | goto retryLockFile; |
---|
| 117 | }else{ |
---|
| 118 | free(myLock->filename); |
---|
| 119 | free(myLock); |
---|
| 120 | return NULL; |
---|
| 121 | } |
---|
| 122 | }else{ |
---|
| 123 | char local_mode[3]; |
---|
| 124 | memset(local_mode,0,3); |
---|
| 125 | if(mode=='w') |
---|
| 126 | sprintf(local_mode,"%c+",mode); |
---|
| 127 | else |
---|
| 128 | sprintf(local_mode,"%c",mode); |
---|
| 129 | myLock->lockfile=fopen(myLock->filename,local_mode); |
---|
| 130 | char tmp[512]; |
---|
| 131 | sprintf(tmp,"%d",zGetpid()); |
---|
| 132 | if(myLock->lockfile==NULL){ |
---|
| 133 | myLock->lockfile=fopen(myLock->filename,"w+"); |
---|
| 134 | fwrite(tmp,sizeof(char),strlen(tmp),myLock->lockfile); |
---|
| 135 | fflush(myLock->lockfile); |
---|
| 136 | fclose(myLock->lockfile); |
---|
| 137 | myLock->lockfile=fopen(myLock->filename,local_mode); |
---|
| 138 | }/*else |
---|
| 139 | fprintf(stderr,"%s %d %d\n",__FILE__,__LINE__,(myLock->lockfile==NULL));*/ |
---|
| 140 | if(mode!='r'){ |
---|
| 141 | fwrite(tmp,sizeof(char),strlen(tmp),myLock->lockfile); |
---|
| 142 | fflush(myLock->lockfile); |
---|
| 143 | } |
---|
| 144 | int cnt=0; |
---|
| 145 | if(mode=='r'){ |
---|
| 146 | myLock->lock.l_type = F_RDLCK; |
---|
| 147 | }else |
---|
| 148 | myLock->lock.l_type = F_WRLCK; |
---|
| 149 | myLock->lock.l_whence = 0; |
---|
| 150 | myLock->lock.l_start = 0; |
---|
| 151 | myLock->lock.l_len = strlen(tmp)*sizeof(char); |
---|
| 152 | while (true) { |
---|
| 153 | if((res=fcntl(fileno(myLock->lockfile), F_SETLK, &(myLock->lock)))==-1 && |
---|
| 154 | (errno==EAGAIN || errno==EACCES)){ |
---|
| 155 | if(cnt >= ZOO_LOCK_MAX_RETRY){ |
---|
| 156 | char message[51]; |
---|
| 157 | sprintf(message,"Unable to get the lock after %d attempts.\n",cnt); |
---|
| 158 | setMapInMaps(conf,"lenv","message",message); |
---|
| 159 | fclose(myLock->lockfile); |
---|
| 160 | free(myLock->filename); |
---|
| 161 | free(myLock); |
---|
| 162 | return NULL; |
---|
| 163 | } |
---|
| 164 | #ifdef DEBUG |
---|
| 165 | fprintf(stderr,"(%d) Wait for lock on %s, tried %d times ... \n",zGetpid(),myLock->filename,cnt); |
---|
| 166 | fflush(stderr); |
---|
| 167 | #endif |
---|
| 168 | zSleep(1); |
---|
| 169 | cnt++; |
---|
| 170 | }else |
---|
| 171 | break; |
---|
| 172 | } |
---|
| 173 | if(res<0){ |
---|
| 174 | char *tmp; |
---|
| 175 | if(errno==EBADF) |
---|
| 176 | tmp="Either: the filedes argument is invalid; you requested a read lock but the filedes is not open for read access; or, you requested a write lock but the filedes is not open for write access."; |
---|
| 177 | else |
---|
| 178 | if(errno==EINVAL) |
---|
| 179 | tmp="Either the lockp argument doesn’t specify valid lock information, or the file associated with filedes doesn’t support locks."; |
---|
| 180 | else |
---|
| 181 | tmp="The system has run out of file lock resources; there are already too many file locks in place."; |
---|
| 182 | #ifdef DEBUG |
---|
| 183 | fprintf(stderr,"Unable to get the lock on %s due to the following error: %s\n",myLock->filename,tmp); |
---|
| 184 | #endif |
---|
| 185 | return NULL; |
---|
| 186 | } |
---|
| 187 | return myLock; |
---|
| 188 | } |
---|
| 189 | } |
---|
[365] | 190 | |
---|
[917] | 191 | /** |
---|
| 192 | * Remove a lock. |
---|
| 193 | * @param conf the main configuration maps |
---|
| 194 | * @param s the zooLock structure |
---|
| 195 | * @return 0 on success, -1 on failure. |
---|
| 196 | */ |
---|
| 197 | int unlockFile(maps* conf,struct zooLock* s){ |
---|
| 198 | int res=-1; |
---|
| 199 | if(s!=NULL){ |
---|
| 200 | s->lock.l_type = F_UNLCK; |
---|
| 201 | res=fcntl(fileno(s->lockfile), F_SETLK, &s->lock); |
---|
| 202 | if(res==-1) |
---|
| 203 | return res; |
---|
| 204 | fclose(s->lockfile); |
---|
| 205 | #ifndef WIN32 |
---|
| 206 | // Check if there is any process locking a file and delete the lock if not. |
---|
| 207 | s->lock.l_type = F_WRLCK; |
---|
| 208 | if(fcntl(fileno(s->lockfile), F_GETLK, &s->lock)!=-1 && s->lock.l_type == F_UNLCK){ |
---|
| 209 | #endif |
---|
| 210 | zUnlink(s->filename); |
---|
| 211 | #ifndef WIN32 |
---|
| 212 | } |
---|
| 213 | #endif |
---|
| 214 | free(s->filename); |
---|
| 215 | free(s); |
---|
| 216 | } |
---|
| 217 | return res; |
---|
| 218 | } |
---|
| 219 | |
---|
[652] | 220 | #ifndef RELY_ON_DB |
---|
[917] | 221 | #include "dirent.h" |
---|
[578] | 222 | |
---|
[579] | 223 | /** |
---|
[652] | 224 | * Read the sid file attached of a service if any |
---|
[579] | 225 | * |
---|
| 226 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 227 | * @param pid the service identifier (usid key from the [lenv] section) |
---|
[652] | 228 | * @return the reported status char* (temporary/final result) |
---|
[579] | 229 | */ |
---|
[652] | 230 | char* getStatusId(maps* conf,char* pid){ |
---|
| 231 | map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); |
---|
| 232 | char* fbkpid = |
---|
| 233 | (char *) |
---|
| 234 | malloc ((strlen (r_inputs->value) + strlen (pid) + 7) * sizeof (char)); |
---|
| 235 | sprintf (fbkpid, "%s/%s.sid", r_inputs->value, pid); |
---|
| 236 | FILE* f0 = fopen (fbkpid, "r"); |
---|
| 237 | if(f0!=NULL){ |
---|
[653] | 238 | long flen; |
---|
| 239 | char *fcontent; |
---|
[652] | 240 | fseek (f0, 0, SEEK_END); |
---|
[653] | 241 | flen = ftell (f0); |
---|
[652] | 242 | fseek (f0, 0, SEEK_SET); |
---|
[653] | 243 | fcontent = (char *) malloc ((flen + 1) * sizeof (char)); |
---|
| 244 | fread(fcontent,flen,1,f0); |
---|
| 245 | fcontent[flen]=0; |
---|
[652] | 246 | fclose(f0); |
---|
[653] | 247 | return fcontent; |
---|
[652] | 248 | }else |
---|
| 249 | return NULL; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | /** |
---|
| 253 | * Acquire the global lock |
---|
| 254 | * |
---|
| 255 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 256 | * @return a semid |
---|
| 257 | */ |
---|
| 258 | semid acquireLock(maps* conf){ |
---|
| 259 | semid lockid; |
---|
| 260 | int itn=0; |
---|
| 261 | toRetry1: |
---|
| 262 | lockid=getShmLockId(conf,1); |
---|
[507] | 263 | if( |
---|
[216] | 264 | #ifdef WIN32 |
---|
[507] | 265 | lockid==NULL |
---|
| 266 | #else |
---|
| 267 | lockid<0 |
---|
| 268 | #endif |
---|
| 269 | ){ |
---|
[652] | 270 | #ifdef WIN32 |
---|
[680] | 271 | return NULL; |
---|
[652] | 272 | #else |
---|
| 273 | return -1; |
---|
| 274 | #endif |
---|
[507] | 275 | } |
---|
| 276 | if(lockShm(lockid)<0){ |
---|
[680] | 277 | #ifdef WIN32 |
---|
| 278 | return NULL; |
---|
| 279 | #else |
---|
[652] | 280 | if(itn<ZOO_LOCK_MAX_RETRY){ |
---|
| 281 | itn++; |
---|
| 282 | goto toRetry1; |
---|
| 283 | }else |
---|
[680] | 284 | return -1; |
---|
[652] | 285 | #endif |
---|
| 286 | }else |
---|
| 287 | return lockid; |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | /** |
---|
| 291 | * Read the cache file of a running service |
---|
| 292 | * |
---|
| 293 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 294 | * @param pid the service identifier (usid key from the [lenv] section) |
---|
| 295 | * @return the reported status char* (temporary/final result) |
---|
| 296 | */ |
---|
| 297 | char* _getStatusFile(maps* conf,char* pid){ |
---|
| 298 | map* tmpTmap = getMapFromMaps (conf, "main", "tmpPath"); |
---|
| 299 | |
---|
| 300 | struct dirent *dp; |
---|
| 301 | DIR *dirp = opendir(tmpTmap->value); |
---|
| 302 | char fileName[1024]; |
---|
| 303 | int hasFile=-1; |
---|
| 304 | if(dirp!=NULL){ |
---|
| 305 | char tmp[128]; |
---|
| 306 | sprintf(tmp,"_%s.xml",pid); |
---|
| 307 | while ((dp = readdir(dirp)) != NULL){ |
---|
| 308 | #ifdef DEBUG |
---|
| 309 | fprintf(stderr,"File : %s searched : %s\n",dp->d_name,tmp); |
---|
| 310 | #endif |
---|
| 311 | if(strstr(dp->d_name,"final_")==0 && strstr(dp->d_name,tmp)!=0){ |
---|
| 312 | sprintf(fileName,"%s/%s",tmpTmap->value,dp->d_name); |
---|
| 313 | hasFile=1; |
---|
| 314 | break; |
---|
| 315 | } |
---|
| 316 | } |
---|
[507] | 317 | } |
---|
[652] | 318 | if(hasFile>0){ |
---|
| 319 | semid lockid; |
---|
| 320 | char* stat=getStatusId(conf,pid); |
---|
| 321 | if(stat!=NULL){ |
---|
| 322 | setMapInMaps(conf,"lenv","lid",stat); |
---|
| 323 | lockid=acquireLock(conf); |
---|
| 324 | if(lockid<0) |
---|
| 325 | return NULL; |
---|
| 326 | } |
---|
| 327 | |
---|
[788] | 328 | //FILE* f0 = fopen (fileName, "r"); |
---|
[917] | 329 | // knut: open file in binary mode to avoid conversion of line endings (yielding extra bytes) on Windows platforms |
---|
| 330 | FILE* f0 = fopen(fileName, "rb"); |
---|
[652] | 331 | if(f0!=NULL){ |
---|
| 332 | fseek (f0, 0, SEEK_END); |
---|
| 333 | long flen = ftell (f0); |
---|
| 334 | fseek (f0, 0, SEEK_SET); |
---|
| 335 | char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char)); |
---|
| 336 | fread(tmps1,flen,1,f0); |
---|
| 337 | tmps1[flen]=0; |
---|
| 338 | fclose(f0); |
---|
| 339 | if(stat!=NULL){ |
---|
| 340 | unlockShm(lockid); |
---|
| 341 | free(stat); |
---|
| 342 | } |
---|
| 343 | return tmps1; |
---|
| 344 | } |
---|
| 345 | else{ |
---|
| 346 | if(stat!=NULL){ |
---|
| 347 | unlockShm(lockid); |
---|
| 348 | free(stat); |
---|
| 349 | } |
---|
| 350 | return NULL; |
---|
| 351 | } |
---|
| 352 | } |
---|
| 353 | else |
---|
| 354 | return NULL; |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | /** |
---|
| 358 | * Get the ongoing status of a running service |
---|
| 359 | * |
---|
| 360 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 361 | * @param pid the service identifier (usid key from the [lenv] section) |
---|
| 362 | * @return the reported status char* (MESSAGE|POURCENTAGE) |
---|
| 363 | */ |
---|
| 364 | char* _getStatus(maps* conf,char* lid){ |
---|
| 365 | map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); |
---|
| 366 | char* fbkpid = |
---|
| 367 | (char *) |
---|
| 368 | malloc ((strlen (r_inputs->value) + strlen (lid) + 9) * sizeof (char)); |
---|
| 369 | sprintf (fbkpid, "%s/%s.status", r_inputs->value, lid); |
---|
| 370 | FILE* f0 = fopen (fbkpid, "r"); |
---|
[738] | 371 | if(f0!=NULL){ |
---|
| 372 | semid lockid = NULL; |
---|
[653] | 373 | char* stat; |
---|
| 374 | long flen; |
---|
| 375 | stat=getStatusId(conf,lid); |
---|
[652] | 376 | if(stat!=NULL){ |
---|
| 377 | setMapInMaps(conf,"lenv","lid",stat); |
---|
[653] | 378 | lockid=acquireLock(conf); |
---|
[652] | 379 | if(lockid<0) |
---|
| 380 | return NULL; |
---|
| 381 | } |
---|
| 382 | fseek (f0, 0, SEEK_END); |
---|
[653] | 383 | flen = ftell (f0); |
---|
[652] | 384 | if(flen>0){ |
---|
[653] | 385 | char *fcontent; |
---|
[652] | 386 | fseek (f0, 0, SEEK_SET); |
---|
[653] | 387 | fcontent = (char *) malloc ((flen + 1) * sizeof (char)); |
---|
| 388 | fread(fcontent,flen,1,f0); |
---|
| 389 | fcontent[flen]=0; |
---|
[652] | 390 | fclose(f0); |
---|
| 391 | free(fbkpid); |
---|
| 392 | if(stat!=NULL){ |
---|
[680] | 393 | #ifndef WIN32 |
---|
[652] | 394 | removeShmLock(conf,1); |
---|
[680] | 395 | #else |
---|
| 396 | unlockShm(lockid); |
---|
| 397 | #endif |
---|
[652] | 398 | free(stat); |
---|
| 399 | } |
---|
[653] | 400 | return fcontent; |
---|
[652] | 401 | } |
---|
| 402 | fclose(f0); |
---|
| 403 | free(fbkpid); |
---|
| 404 | if(stat!=NULL){ |
---|
| 405 | removeShmLock(conf,1); |
---|
| 406 | free(stat); |
---|
| 407 | } |
---|
| 408 | return NULL; |
---|
| 409 | }else{ |
---|
| 410 | free(fbkpid); |
---|
| 411 | char* stat=getStatusId(conf,lid); |
---|
| 412 | setMapInMaps(conf,"lenv","lid",stat); |
---|
[507] | 413 | removeShmLock(conf,1); |
---|
[652] | 414 | return NULL; |
---|
[507] | 415 | } |
---|
| 416 | } |
---|
[216] | 417 | |
---|
[652] | 418 | /** |
---|
| 419 | * Stop handling status repport. |
---|
| 420 | * |
---|
| 421 | * @param conf the map containing the setting of the main.cfg file |
---|
| 422 | */ |
---|
[889] | 423 | void unhandleStatus(maps *conf){ |
---|
[652] | 424 | map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); |
---|
| 425 | map* usid = getMapFromMaps (conf, "lenv", "usid"); |
---|
| 426 | char* fbkpid = |
---|
| 427 | (char *) malloc ((strlen (r_inputs->value) + strlen (usid->value) + 9) |
---|
| 428 | * sizeof (char)); |
---|
| 429 | sprintf (fbkpid, "%s/%s.status", r_inputs->value, usid->value); |
---|
[917] | 430 | zUnlink(fbkpid); |
---|
[652] | 431 | free(fbkpid); |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | /** |
---|
| 435 | * Update the current status of the running service. |
---|
| 436 | * |
---|
| 437 | * @see acquireLock, lockShm |
---|
| 438 | * @param conf the map containing the setting of the main.cfg file |
---|
| 439 | * @return 0 on success, -2 if shmget failed, -1 if shmat failed |
---|
| 440 | */ |
---|
| 441 | int _updateStatus(maps *conf){ |
---|
[788] | 442 | |
---|
[652] | 443 | map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); |
---|
| 444 | map* sid = getMapFromMaps (conf, "lenv", "usid"); |
---|
[788] | 445 | |
---|
[652] | 446 | char* fbkpid = |
---|
| 447 | (char *) |
---|
| 448 | malloc ((strlen (r_inputs->value) + strlen (sid->value) + 9) * sizeof (char)); |
---|
| 449 | sprintf (fbkpid, "%s/%s.status", r_inputs->value, sid->value); |
---|
| 450 | map* status=getMapFromMaps(conf,"lenv","status"); |
---|
| 451 | map* msg=getMapFromMaps(conf,"lenv","message"); |
---|
| 452 | if(status!=NULL && msg!=NULL && |
---|
| 453 | status->value!=NULL && msg->value!=NULL && |
---|
[738] | 454 | strlen(status->value)>0 && strlen(msg->value)>1){ |
---|
| 455 | semid lockid = NULL; |
---|
[788] | 456 | |
---|
[652] | 457 | char* stat=getStatusId(conf,sid->value); |
---|
| 458 | if(stat!=NULL){ |
---|
| 459 | lockid=acquireLock(conf); |
---|
[680] | 460 | if(lockid<0){ |
---|
| 461 | dumpMap(status); |
---|
[652] | 462 | return ZOO_LOCK_ACQUIRE_FAILED; |
---|
[680] | 463 | } |
---|
[652] | 464 | } |
---|
| 465 | FILE* fstatus=fopen(fbkpid,"w"); |
---|
| 466 | if(fstatus!=NULL){ |
---|
[654] | 467 | fprintf(fstatus,"%s|%s",status->value,msg->value); |
---|
[652] | 468 | fflush(fstatus); |
---|
| 469 | fclose(fstatus); |
---|
| 470 | } |
---|
| 471 | if(stat!=NULL){ |
---|
| 472 | unlockShm(lockid); |
---|
| 473 | free(stat); |
---|
| 474 | } |
---|
| 475 | } |
---|
[788] | 476 | |
---|
[652] | 477 | return 0; |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | #endif |
---|
| 481 | |
---|
[507] | 482 | #ifdef WIN32 |
---|
| 483 | |
---|
[216] | 484 | #define SHMEMSIZE 4096 |
---|
| 485 | |
---|
[554] | 486 | size_t getKeyValue(maps* conf, char* key, size_t length){ |
---|
| 487 | if(conf==NULL) { |
---|
[579] | 488 | strncpy(key, "700666", length); |
---|
| 489 | return strlen(key); |
---|
[554] | 490 | } |
---|
| 491 | |
---|
[507] | 492 | map *tmpMap=getMapFromMaps(conf,"lenv","lid"); |
---|
| 493 | if(tmpMap==NULL) |
---|
[680] | 494 | tmpMap=getMapFromMaps(conf,"lenv","osid"); |
---|
[554] | 495 | |
---|
[507] | 496 | if(tmpMap!=NULL){ |
---|
[680] | 497 | snprintf(key, length, "zoo_sem_%s", tmpMap->value); |
---|
[507] | 498 | } |
---|
[554] | 499 | else { |
---|
[680] | 500 | strncpy(key, "-1", length); |
---|
[554] | 501 | } |
---|
| 502 | return strlen(key); |
---|
[579] | 503 | } |
---|
| 504 | |
---|
| 505 | |
---|
[507] | 506 | semid getShmLockId(maps* conf, int nsems){ |
---|
[680] | 507 | semid sem_id; |
---|
| 508 | char key[MAX_PATH]; |
---|
| 509 | getKeyValue(conf, key, MAX_PATH); |
---|
| 510 | |
---|
| 511 | sem_id = CreateSemaphore( NULL, nsems, nsems+1, key); |
---|
| 512 | if(sem_id==NULL){ |
---|
[507] | 513 | #ifdef DEBUG |
---|
[680] | 514 | fprintf(stderr,"Semaphore failed to create: %s\n", getLastErrorMessage()); |
---|
[507] | 515 | #endif |
---|
[680] | 516 | return NULL; |
---|
| 517 | } |
---|
[507] | 518 | #ifdef DEBUG |
---|
[680] | 519 | fprintf(stderr,"%s Accessed !\n",key); |
---|
[507] | 520 | #endif |
---|
[680] | 521 | return sem_id; |
---|
[507] | 522 | } |
---|
| 523 | |
---|
| 524 | int removeShmLock(maps* conf, int nsems){ |
---|
| 525 | semid sem_id=getShmLockId(conf,1); |
---|
| 526 | if (CloseHandle(sem_id) == 0) { |
---|
[652] | 527 | #ifdef DEBUG |
---|
[578] | 528 | fprintf(stderr,"Unable to remove semaphore: %s\n", getLastErrorMessage()); |
---|
[652] | 529 | #endif |
---|
[507] | 530 | return -1; |
---|
| 531 | } |
---|
| 532 | #ifdef DEBUG |
---|
| 533 | fprintf(stderr,"%d Removed !\n",sem_id); |
---|
| 534 | #endif |
---|
| 535 | return 0; |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | int lockShm(semid id){ |
---|
| 539 | DWORD dwWaitResult=WaitForSingleObject(id,INFINITE); |
---|
| 540 | switch (dwWaitResult){ |
---|
| 541 | case WAIT_OBJECT_0: |
---|
| 542 | return 0; |
---|
| 543 | break; |
---|
| 544 | case WAIT_TIMEOUT: |
---|
| 545 | return -1; |
---|
| 546 | break; |
---|
| 547 | default: |
---|
| 548 | return -2; |
---|
| 549 | break; |
---|
| 550 | } |
---|
| 551 | return 0; |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | int unlockShm(semid id){ |
---|
| 555 | if(!ReleaseSemaphore(id,1,NULL)){ |
---|
| 556 | return -1; |
---|
| 557 | } |
---|
| 558 | return 0; |
---|
| 559 | } |
---|
| 560 | |
---|
[216] | 561 | static LPVOID lpvMemG = NULL; // pointer to shared memory |
---|
| 562 | static HANDLE hMapObjectG = NULL; // handle to file mapping |
---|
| 563 | |
---|
| 564 | |
---|
| 565 | char* getStatus(int pid){ |
---|
[467] | 566 | char *lpszBuf=(char*) malloc(SHMEMSIZE*sizeof(char)); |
---|
[452] | 567 | int i=0; |
---|
[216] | 568 | LPWSTR lpszTmp=NULL; |
---|
| 569 | LPVOID lpvMem = NULL; |
---|
| 570 | HANDLE hMapObject = NULL; |
---|
| 571 | BOOL fIgnore,fInit; |
---|
[507] | 572 | char tmp[1024]; |
---|
| 573 | sprintf(tmp,"%d",pid); |
---|
[216] | 574 | if(hMapObject==NULL) |
---|
| 575 | hMapObject = CreateFileMapping( |
---|
| 576 | INVALID_HANDLE_VALUE, // use paging file |
---|
| 577 | NULL, // default security attributes |
---|
| 578 | PAGE_READWRITE, // read/write access |
---|
| 579 | 0, // size: high 32-bits |
---|
| 580 | 4096, // size: low 32-bits |
---|
| 581 | TEXT(tmp)); // name of map object |
---|
[507] | 582 | if (hMapObject == NULL){ |
---|
| 583 | #ifdef DEBUG |
---|
| 584 | fprintf(stderr,"ERROR on line %d\n",__LINE__); |
---|
| 585 | #endif |
---|
| 586 | return "-1"; |
---|
| 587 | } |
---|
[216] | 588 | if((GetLastError() != ERROR_ALREADY_EXISTS)){ |
---|
[507] | 589 | #ifdef DEBUG |
---|
| 590 | fprintf(stderr,"ERROR on line %d\n",__LINE__); |
---|
[578] | 591 | fprintf(stderr,"READING STRING S %s\n", getLastErrorMessage()); |
---|
[507] | 592 | #endif |
---|
[216] | 593 | fIgnore = UnmapViewOfFile(lpvMem); |
---|
| 594 | fIgnore = CloseHandle(hMapObject); |
---|
| 595 | return "-1"; |
---|
| 596 | } |
---|
| 597 | fInit=TRUE; |
---|
| 598 | if(lpvMem==NULL) |
---|
| 599 | lpvMem = MapViewOfFile( |
---|
| 600 | hMapObject, // object to map view of |
---|
| 601 | FILE_MAP_READ, // read/write access |
---|
| 602 | 0, // high offset: map from |
---|
| 603 | 0, // low offset: beginning |
---|
| 604 | 0); // default: map entire file |
---|
[507] | 605 | if (lpvMem == NULL){ |
---|
| 606 | #ifdef DEBUG |
---|
| 607 | fprintf(stderr,"READING STRING S %d\n",__LINE__); |
---|
[578] | 608 | fprintf(stderr,"READING STRING S %s\n", getLastErrorMessage()); |
---|
[507] | 609 | #endif |
---|
[216] | 610 | return "-1"; |
---|
[507] | 611 | } |
---|
[216] | 612 | lpszTmp = (LPWSTR) lpvMem; |
---|
[452] | 613 | while (*lpszTmp){ |
---|
| 614 | lpszBuf[i] = (char)*lpszTmp; |
---|
| 615 | *lpszTmp++; |
---|
| 616 | lpszBuf[i+1] = '\0'; |
---|
| 617 | i++; |
---|
| 618 | } |
---|
| 619 | return (char*)lpszBuf; |
---|
[216] | 620 | } |
---|
| 621 | |
---|
| 622 | #else |
---|
[579] | 623 | /** |
---|
| 624 | * Number of time to try to access a semaphores set |
---|
| 625 | * @see getShmLockId |
---|
| 626 | */ |
---|
[507] | 627 | #define MAX_RETRIES 10 |
---|
| 628 | |
---|
[509] | 629 | #ifndef __APPLE__ |
---|
[630] | 630 | /** |
---|
| 631 | * arg for semctl system calls. |
---|
| 632 | */ |
---|
[509] | 633 | union semun { |
---|
[630] | 634 | int val; //!< value for SETVAL |
---|
| 635 | struct semid_ds *buf; //!< buffer for IPC_STAT & IPC_SET |
---|
| 636 | ushort *array; //!< array for GETALL & SETALL |
---|
[509] | 637 | }; |
---|
| 638 | #endif |
---|
| 639 | |
---|
[579] | 640 | /** |
---|
[652] | 641 | * Set in the pre-allocated key the zoo_sem_[OSID] string |
---|
| 642 | * where [OSID] is the lid (if any) or osid value from the [lenv] section. |
---|
[579] | 643 | * |
---|
| 644 | * @param conf the map containing the setting of the main.cfg file |
---|
| 645 | */ |
---|
[507] | 646 | int getKeyValue(maps* conf){ |
---|
[514] | 647 | if(conf==NULL) |
---|
| 648 | return 700666; |
---|
[507] | 649 | map *tmpMap=getMapFromMaps(conf,"lenv","lid"); |
---|
| 650 | if(tmpMap==NULL) |
---|
[652] | 651 | tmpMap=getMapFromMaps(conf,"lenv","osid"); |
---|
[507] | 652 | int key=-1; |
---|
| 653 | if(tmpMap!=NULL) |
---|
| 654 | key=atoi(tmpMap->value); |
---|
| 655 | return key; |
---|
| 656 | } |
---|
| 657 | |
---|
[579] | 658 | /** |
---|
| 659 | * Try to create or access a semaphore set. |
---|
| 660 | * |
---|
| 661 | * @see getKeyValue |
---|
| 662 | * @param conf the map containing the setting of the main.cfg file |
---|
| 663 | * @param nsems number of semaphores |
---|
| 664 | * @return a semaphores set indentifier on success, -1 in other case |
---|
| 665 | */ |
---|
[507] | 666 | int getShmLockId(maps* conf, int nsems){ |
---|
| 667 | int i; |
---|
| 668 | union semun arg; |
---|
| 669 | struct semid_ds buf; |
---|
| 670 | struct sembuf sb; |
---|
| 671 | semid sem_id; |
---|
| 672 | int key=getKeyValue(conf); |
---|
| 673 | |
---|
| 674 | sem_id = semget(key, nsems, IPC_CREAT | IPC_EXCL | 0666); |
---|
| 675 | |
---|
| 676 | if (sem_id >= 0) { /* we got it first */ |
---|
| 677 | sb.sem_op = 1; |
---|
| 678 | sb.sem_flg = 0; |
---|
| 679 | arg.val=1; |
---|
| 680 | for(sb.sem_num = 0; sb.sem_num < nsems; sb.sem_num++) { |
---|
| 681 | /* do a semop() to "free" the semaphores. */ |
---|
| 682 | /* this sets the sem_otime field, as needed below. */ |
---|
| 683 | if (semop(sem_id, &sb, 1) == -1) { |
---|
| 684 | int e = errno; |
---|
| 685 | semctl(sem_id, 0, IPC_RMID); /* clean up */ |
---|
| 686 | errno = e; |
---|
| 687 | return -1; /* error, check errno */ |
---|
| 688 | } |
---|
| 689 | } |
---|
[917] | 690 | setMapInMaps(conf,"lenv","semaphore","Created"); |
---|
[507] | 691 | } else if (errno == EEXIST) { /* someone else got it first */ |
---|
| 692 | int ready = 0; |
---|
| 693 | |
---|
| 694 | sem_id = semget(key, nsems, 0); /* get the id */ |
---|
| 695 | if (sem_id < 0) return sem_id; /* error, check errno */ |
---|
| 696 | |
---|
| 697 | /* wait for other process to initialize the semaphore: */ |
---|
| 698 | arg.buf = &buf; |
---|
| 699 | for(i = 0; i < MAX_RETRIES && !ready; i++) { |
---|
| 700 | semctl(sem_id, nsems-1, IPC_STAT, arg); |
---|
| 701 | if (arg.buf->sem_otime != 0) { |
---|
| 702 | #ifdef DEBUG |
---|
| 703 | fprintf(stderr,"Semaphore acquired ...\n"); |
---|
| 704 | #endif |
---|
| 705 | ready = 1; |
---|
| 706 | } else { |
---|
| 707 | #ifdef DEBUG |
---|
| 708 | fprintf(stderr,"Retry to access the semaphore later ...\n"); |
---|
| 709 | #endif |
---|
[917] | 710 | zSleep(1000); |
---|
[507] | 711 | } |
---|
| 712 | } |
---|
[576] | 713 | errno = ZOO_LOCK_ACQUIRE_FAILED; |
---|
[507] | 714 | if (!ready) { |
---|
| 715 | #ifdef DEBUG |
---|
| 716 | fprintf(stderr,"Unable to access the semaphore ...\n"); |
---|
| 717 | #endif |
---|
| 718 | errno = ETIME; |
---|
| 719 | return -1; |
---|
| 720 | } |
---|
[917] | 721 | setMapInMaps(conf,"lenv","semaphore","Acquired"); |
---|
[507] | 722 | } else { |
---|
| 723 | return sem_id; /* error, check errno */ |
---|
| 724 | } |
---|
| 725 | #ifdef DEBUG |
---|
| 726 | fprintf(stderr,"%d Created !\n",sem_id); |
---|
| 727 | #endif |
---|
| 728 | return sem_id; |
---|
| 729 | } |
---|
| 730 | |
---|
[579] | 731 | /** |
---|
| 732 | * Try to remove a semaphore set. |
---|
| 733 | * |
---|
| 734 | * @param conf the map containing the setting of the main.cfg file |
---|
| 735 | * @param nsems number of semaphores |
---|
| 736 | * @return 0 if the semaphore can be removed, -1 in other case. |
---|
| 737 | */ |
---|
[507] | 738 | int removeShmLock(maps* conf, int nsems){ |
---|
| 739 | union semun arg; |
---|
| 740 | int sem_id=getShmLockId(conf,nsems); |
---|
| 741 | if (semctl(sem_id, 0, IPC_RMID, arg) == -1) { |
---|
[652] | 742 | #ifdef DEBUG |
---|
| 743 | perror("semctl remove"); |
---|
| 744 | #endif |
---|
[507] | 745 | return -1; |
---|
| 746 | } |
---|
[652] | 747 | #ifdef DEBUG |
---|
| 748 | fprintf(stderr,"Semaphore removed!\n"); |
---|
| 749 | #endif |
---|
[507] | 750 | return 0; |
---|
| 751 | } |
---|
| 752 | |
---|
[579] | 753 | /** |
---|
| 754 | * Lock a semaphore set. |
---|
| 755 | * |
---|
| 756 | * @param id the semaphores set indetifier |
---|
| 757 | * @return 0 if the semaphore can be locked, -1 in other case. |
---|
| 758 | */ |
---|
[507] | 759 | int lockShm(int id){ |
---|
| 760 | struct sembuf sb; |
---|
| 761 | sb.sem_num = 0; |
---|
| 762 | sb.sem_op = -1; /* set to allocate resource */ |
---|
| 763 | sb.sem_flg = SEM_UNDO; |
---|
| 764 | if (semop(id, &sb, 1) == -1){ |
---|
[652] | 765 | #ifdef DEBUG |
---|
| 766 | perror("semop lock"); |
---|
| 767 | #endif |
---|
[507] | 768 | return -1; |
---|
| 769 | } |
---|
| 770 | return 0; |
---|
| 771 | } |
---|
| 772 | |
---|
[579] | 773 | /** |
---|
| 774 | * unLock a semaphore set. |
---|
| 775 | * |
---|
| 776 | * @param id the semaphores set indetifier |
---|
| 777 | * @return 0 if the semaphore can be locked, -1 in other case. |
---|
| 778 | */ |
---|
[507] | 779 | int unlockShm(int id){ |
---|
| 780 | struct sembuf sb; |
---|
| 781 | sb.sem_num = 0; |
---|
| 782 | sb.sem_op = 1; /* free resource */ |
---|
| 783 | sb.sem_flg = SEM_UNDO; |
---|
| 784 | if (semop(id, &sb, 1) == -1) { |
---|
[26] | 785 | #ifdef DEBUG |
---|
[652] | 786 | perror("semop unlock"); |
---|
[26] | 787 | #endif |
---|
[652] | 788 | return -1; |
---|
[26] | 789 | } |
---|
[471] | 790 | return 0; |
---|
[26] | 791 | } |
---|
| 792 | |
---|
[579] | 793 | /** |
---|
[652] | 794 | * Get the current status of the running service. |
---|
[579] | 795 | * |
---|
| 796 | * @see getKeyValue, getShmLockId, lockShm |
---|
| 797 | * @param pid the semaphores |
---|
| 798 | * @return 0 on success, -2 if shmget failed, -1 if shmat failed |
---|
| 799 | */ |
---|
[26] | 800 | char* getStatus(int pid){ |
---|
[490] | 801 | int shmid; |
---|
[26] | 802 | key_t key; |
---|
| 803 | void *shm; |
---|
| 804 | key=pid; |
---|
| 805 | if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { |
---|
| 806 | #ifdef DEBUG |
---|
| 807 | fprintf(stderr,"shmget failed in getStatus\n"); |
---|
| 808 | #endif |
---|
| 809 | }else{ |
---|
| 810 | if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { |
---|
| 811 | #ifdef DEBUG |
---|
| 812 | fprintf(stderr,"shmat failed in getStatus\n"); |
---|
| 813 | #endif |
---|
| 814 | }else{ |
---|
[514] | 815 | char *ret=strdup((char*)shm); |
---|
| 816 | shmdt((void *)shm); |
---|
| 817 | return ret; |
---|
[26] | 818 | } |
---|
| 819 | } |
---|
[490] | 820 | return (char*)"-1"; |
---|
[26] | 821 | } |
---|
| 822 | |
---|
[216] | 823 | #endif |
---|
[26] | 824 | |
---|
[579] | 825 | /** |
---|
| 826 | * Update the status of an ongoing service |
---|
| 827 | * |
---|
| 828 | * @param conf the maps containing the settings of the main.cfg file |
---|
| 829 | * @param percentCompleted percentage of completude of execution of the service |
---|
| 830 | * @param message information about the current step executed |
---|
| 831 | * @return the value of _updateStatus |
---|
| 832 | * @see _updateStatus |
---|
| 833 | */ |
---|
[471] | 834 | int updateStatus( maps* conf, const int percentCompleted, const char* message ){ |
---|
| 835 | char tmp[4]; |
---|
| 836 | snprintf(tmp,4,"%d",percentCompleted); |
---|
| 837 | setMapInMaps( conf, "lenv", "status", tmp ); |
---|
| 838 | setMapInMaps( conf, "lenv", "message", message); |
---|
| 839 | return _updateStatus( conf ); |
---|
| 840 | } |
---|
| 841 | |
---|
[579] | 842 | /** |
---|
| 843 | * Access an input value |
---|
| 844 | * |
---|
| 845 | * @param inputs the maps to search for the input value |
---|
| 846 | * @param parameterName the input name to fetch the value |
---|
| 847 | * @param numberOfBytes the resulting size of the value to add (for binary |
---|
| 848 | * values), -1 for basic char* data |
---|
| 849 | * @return a pointer to the input value if found, NULL in other case. |
---|
| 850 | */ |
---|
[471] | 851 | char* getInputValue( maps* inputs, const char* parameterName, size_t* numberOfBytes){ |
---|
| 852 | map* res=getMapFromMaps(inputs,parameterName,"value"); |
---|
| 853 | if(res!=NULL){ |
---|
| 854 | map* size=getMapFromMaps(inputs,parameterName,"size"); |
---|
| 855 | if(size!=NULL){ |
---|
| 856 | *numberOfBytes=(size_t)atoi(size->value); |
---|
| 857 | return res->value; |
---|
| 858 | }else{ |
---|
| 859 | *numberOfBytes=strlen(res->value); |
---|
| 860 | return res->value; |
---|
| 861 | } |
---|
| 862 | } |
---|
| 863 | return NULL; |
---|
| 864 | } |
---|
| 865 | |
---|
[579] | 866 | /** |
---|
[649] | 867 | * Read a file using the GDAL VSI API |
---|
| 868 | * |
---|
| 869 | * @param conf the maps containing the settings of the main.cfg file |
---|
| 870 | * @param dataSource the datasource name to read |
---|
[781] | 871 | * @warning make sure to free resources returned by this function |
---|
[649] | 872 | */ |
---|
| 873 | char *readVSIFile(maps* conf,const char* dataSource){ |
---|
| 874 | VSILFILE * fichier=VSIFOpenL(dataSource,"rb"); |
---|
[730] | 875 | VSIStatBufL file_status; |
---|
[921] | 876 | int res=VSIStatL(dataSource, &file_status); |
---|
| 877 | if(fichier==NULL || res<0){ |
---|
[649] | 878 | char tmp[1024]; |
---|
[921] | 879 | sprintf(tmp,"Failed to open file %s for reading purpose. File seems empty %ld.", |
---|
[649] | 880 | dataSource,file_status.st_size); |
---|
| 881 | setMapInMaps(conf,"lenv","message",tmp); |
---|
| 882 | return NULL; |
---|
| 883 | } |
---|
| 884 | char *res1=(char *)malloc(file_status.st_size*sizeof(char)); |
---|
| 885 | VSIFReadL(res1,1,file_status.st_size*sizeof(char),fichier); |
---|
| 886 | res1[file_status.st_size-1]=0; |
---|
| 887 | VSIFCloseL(fichier); |
---|
| 888 | VSIUnlink(dataSource); |
---|
| 889 | return res1; |
---|
| 890 | } |
---|
| 891 | |
---|
| 892 | /** |
---|
[579] | 893 | * Set an output value |
---|
| 894 | * |
---|
| 895 | * @param outputs the maps to define the output value |
---|
| 896 | * @param parameterName the output name to set the value |
---|
| 897 | * @param data the value to set |
---|
| 898 | * @param numberOfBytes size of the value to add (for binary values), -1 for |
---|
| 899 | * basic char* data |
---|
| 900 | * @return 0 |
---|
| 901 | */ |
---|
[471] | 902 | int setOutputValue( maps* outputs, const char* parameterName, char* data, size_t numberOfBytes ){ |
---|
| 903 | if(numberOfBytes==-1){ |
---|
| 904 | setMapInMaps(outputs,parameterName,"value",data); |
---|
| 905 | }else{ |
---|
| 906 | char size[1024]; |
---|
| 907 | map* tmp=getMapFromMaps(outputs,parameterName,"value"); |
---|
[539] | 908 | if(tmp==NULL){ |
---|
| 909 | setMapInMaps(outputs,parameterName,"value",""); |
---|
| 910 | tmp=getMapFromMaps(outputs,parameterName,"value"); |
---|
| 911 | } |
---|
[471] | 912 | free(tmp->value); |
---|
| 913 | tmp->value=(char*) malloc((numberOfBytes+1)*sizeof(char)); |
---|
| 914 | memcpy(tmp->value,data,numberOfBytes); |
---|
[490] | 915 | sprintf(size,"%lu",numberOfBytes); |
---|
[471] | 916 | setMapInMaps(outputs,parameterName,"size",size); |
---|
| 917 | } |
---|
| 918 | return 0; |
---|
| 919 | } |
---|
[576] | 920 | |
---|
[917] | 921 | /** |
---|
| 922 | * Check if file exists in specified folder |
---|
| 923 | * |
---|
| 924 | * @param dir the folder in which to search for file |
---|
| 925 | * @param name the name of the file (not full path) |
---|
| 926 | * @return a character string with the full path [dir/name], or NULL if the file does not exist |
---|
| 927 | * |
---|
| 928 | * @attention Caller is responsible for applying free() to the returned pointer |
---|
| 929 | */ |
---|
| 930 | char* file_exists(const char* dir, const char* name) { |
---|
| 931 | const char* d = (dir != NULL ? dir : "."); |
---|
| 932 | if (name != NULL) { |
---|
| 933 | size_t length = strlen(d) + strlen(name) + 2; // including file separator and \0 character |
---|
| 934 | char* path = (char*)calloc(length, sizeof(char)); |
---|
| 935 | snprintf(path, length, "%s/%s", d, name); |
---|
| 936 | |
---|
| 937 | struct stat buffer; |
---|
| 938 | if (stat(path, &buffer) != 0) { |
---|
| 939 | free(path); |
---|
| 940 | path = NULL; |
---|
| 941 | } |
---|
| 942 | return path; |
---|
| 943 | } |
---|
| 944 | else { |
---|
| 945 | return NULL; |
---|
| 946 | } |
---|
| 947 | } |
---|