~ubuntu-branches/ubuntu/utopic/esorex/utopic

« back to all changes in this revision

Viewing changes to src/er_fileutils.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2012-05-11 12:00:00 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120511120000-4uz6r3lrod1dkly5
Tags: 3.9.6-1
* New upstream version
* Add Multi-Arch default plugin directory
* Set DM-Upload-Allowed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: er_fileutils.c,v 1.33 2011/01/10 15:36:27 kbanse Exp $
 
1
/* $Id: er_fileutils.c,v 1.36 2012/03/14 09:26:56 kbanse Exp $
2
2
 *
3
3
 *   This file is part of the ESO Common Pipeline Library
4
 
 *   Copyright (C) 2001-2006 European Southern Observatory
 
4
 *   Copyright (C) 2001-2012 European Southern Observatory
5
5
 *
6
6
 *   This program is free software; you can redistribute it and/or modify
7
7
 *   it under the terms of the GNU General Public License as published by
20
20
 
21
21
/*
22
22
 * $Author: kbanse $
23
 
 * $Date: 2011/01/10 15:36:27 $
24
 
 * $Revision: 1.33 $
25
 
 * $Name: esorex-3_9_0 $
 
23
 * $Date: 2012/03/14 09:26:56 $
 
24
 * $Revision: 1.36 $
 
25
 * $Name: esorex-3_9_6 $
26
26
 */
27
27
 
28
28
#ifdef HAVE_CONFIG_H
58
58
#include "er_plugin.h"
59
59
#include "er_fileutils.h"
60
60
 
61
 
#define PATHSET_MAX     1024
62
61
#define DEV_BLOCKSIZE   4096
63
62
 
64
63
#define Xspace(x)  (x == ' ') || (x == '\t') || (x == '\n')
85
84
 *
86
85
 * @param  path      Input path
87
86
 *
88
 
 * @return A string with the replaced varaibles.
 
87
 * @return A string with the replaced variables.
89
88
 *
90
89
 * This function performs the replacement of any environment 
91
90
 * variables. It only works if the WORDEXP package is available 
163
162
 *
164
163
 * This function also performs the replacement of any environment 
165
164
 * variables.
 
165
 *
 
166
 * OJO: this routine returns a pointer to a static string,
 
167
 *      if you need the returned filename later on, you must copy it
 
168
 *      to a string of PATHSET_MAX length
 
169
 *      otherwise the returned pointer may point later on to something
 
170
 *      very different   :-(
166
171
 */
167
172
/**********************************************************************/
168
173
 
172
177
static char str[PATHSET_MAX];
173
178
char *marker, *cpp;
174
179
 
175
 
int  len;
 
180
int  len, lname;
176
181
 
177
182
 
178
183
 
181
186
 
182
187
if (name == NULL) return NULL; 
183
188
 
 
189
lname = (int) strlen(name);
184
190
 
185
191
/* Only do this, if we start with a tilde (~) */
186
192
 
187
193
if (*name != '~')
188
194
   {
189
 
   len = (int) strlen(name);
190
 
   if (len > (PATHSET_MAX-1))
 
195
   if (lname > (PATHSET_MAX-1))
191
196
      {
192
 
      cpl_msg_error (er_func, "Buffer overflow in filename '%s' - fatal error", name);
 
197
      cpl_msg_error(er_func, 
 
198
                    "Buffer overflow in filename '%s' - fatal error",
 
199
                    name);
193
200
      exit (EXIT_FAILURE);
194
201
      }
195
 
   (void) strcpy (str, name);
 
202
   return (name);
196
203
   }
197
204
 
198
205
else
200
207
   cpp = getenv ("HOME");
201
208
   if (cpp == NULL)
202
209
      {
203
 
      cpl_msg_error (er_func, "Env. variable HOME not set, could not replace `~'");
 
210
      cpl_msg_error(er_func, 
 
211
                    "Env. variable HOME not set, could not replace `~'");
204
212
      exit (EXIT_FAILURE);
205
213
      }
206
214
       
 
215
   len = (int) strlen(cpp);
 
216
   if (len > (PATHSET_MAX-1))
 
217
      {
 
218
      cpl_msg_error (er_func, 
 
219
                     "Buffer overflow in filename '%s' - fatal error",
 
220
                      name);
 
221
      exit (EXIT_FAILURE);
 
222
      }
207
223
   (void) strcpy (str,cpp);
208
 
   len = (int) strlen(str) + (int) strlen(name);        /* +1 for \0 */
209
 
   if (len > PATHSET_MAX)                               /* -1 for (name+1) below */
 
224
 
 
225
   len += lname;                                /* +1 for \0 */
 
226
   if (len > PATHSET_MAX)                       /* -1 for (name+1) below */
210
227
      {
211
 
      cpl_msg_error (er_func, "Buffer overflow in filename '%s' - fatal error", name);
 
228
      cpl_msg_error(er_func, 
 
229
                    "Buffer overflow in filename '%s' - fatal error",
 
230
                    name);
212
231
      exit (EXIT_FAILURE);
213
232
      }
214
233
 
257
276
 * dot replaced by the current working directory (as an absolute path)
258
277
 * using the $PWD environment variable. If not, then the string is 
259
278
 * returned unmodified.
 
279
 *
 
280
 * OJO: this routine returns a pointer to a static string,
 
281
 *      if you need the returned filename later on, you must copy it
 
282
 *      to a string of PATHSET_MAX length
 
283
 *      otherwise the returned pointer may point later on to something
 
284
 *      very different   :-(
260
285
 */
261
286
/**********************************************************************/
262
287
 
266
291
static char str[PATHSET_MAX];
267
292
char  *cpp;
268
293
 
269
 
int  len;
 
294
int  len, lname;
270
295
 
271
296
 
272
297
 
275
300
 
276
301
if (name == NULL) return NULL;
277
302
 
 
303
lname = (int) strlen(name);
278
304
 
279
305
/* if we don't start with a dot (.) just pass the name on */
280
306
 
281
307
if (*name != '.') 
282
308
   {
283
 
   len = (int) strlen(name);
284
 
   if (len > (PATHSET_MAX-1))
 
309
   if (lname > (PATHSET_MAX-1))
285
310
      {
286
 
      cpl_msg_error (er_func, "Buffer overflow in filename '%s' - fatal error", name);
 
311
      cpl_msg_error (er_func,
 
312
                    "Buffer overflow in filename '%s' - fatal error",
 
313
                     name);
287
314
      exit (EXIT_FAILURE);
288
315
      }
289
316
 
290
 
   (void) strcpy (str, name);
291
 
   return (str);
 
317
   return (name);
292
318
   }
293
319
 
294
320
 
300
326
   len = (int) strlen(cpp);
301
327
   if (len > (PATHSET_MAX-1))
302
328
      {
303
 
      cpl_msg_error (er_func, "Buffer overflow in filename '%s' - fatal error", name);
 
329
      cpl_msg_error (er_func, 
 
330
                     "Buffer overflow in filename '%s' - fatal error",
 
331
                      name);
304
332
      exit (EXIT_FAILURE);
305
333
      }
306
334
 
308
336
   }
309
337
else
310
338
   {
311
 
   cpl_msg_error (er_func, "Env. variable PWD not set - fatal errorn");
 
339
   cpl_msg_error (er_func, "Env. variable PWD not set - fatal error");
312
340
   exit (EXIT_FAILURE);
313
341
   }
314
342
 
319
347
   {
320
348
   if ((len+2) > (PATHSET_MAX-1))
321
349
      {
322
 
      cpl_msg_error (er_func, "Buffer overflow in filename '%s' - fatal error", name);
 
350
      cpl_msg_error (er_func, 
 
351
                     "Buffer overflow in filename '%s' - fatal error", 
 
352
                     name);
323
353
      exit (EXIT_FAILURE);
324
354
      }
325
355
   (void) strcat (str, "/.");
326
356
   len += 2;
327
357
   }
328
358
 
329
 
len = (int) strlen(str) + (int) strlen(name);           /* +1 for \0 */
330
 
if (len > PATHSET_MAX)                                  /* -1 for (name+1) below */
 
359
len += lname;                                   /* +1 for \0 */
 
360
if (len > PATHSET_MAX)                          /* -1 for (name+1) below */
331
361
   {
332
362
   cpl_msg_error (er_func, "Buffer overflow in filename '%s'", name);
333
363
   cpl_msg_error (er_func, "Fatal error replacing current working "
334
364
                  "directory symbol due to buffer overflow");
335
365
   exit (EXIT_FAILURE);
336
366
   }
 
367
 
337
368
(void) strcat(str, name + 1);           /* Add the rest of the file name */
338
 
 
339
369
return (str);
340
370
}                            
341
371
/*
363
393
ER_TRACE(__func__)
364
394
 
365
395
 
366
 
if (directory_name == NULL) return 0;
367
 
 
368
 
dp = opendir (er_fileutils_tilde_replace (directory_name));
369
 
if (dp != NULL)
370
 
   {
371
 
   (void) closedir (dp);
372
 
   return 1;                    /* that's TRUE */
 
396
if (directory_name != NULL) 
 
397
   {                            /* no need to save the pointer, here! */
 
398
   dp = opendir (er_fileutils_tilde_replace (directory_name));
 
399
   if (dp != NULL)
 
400
      {
 
401
      (void) closedir (dp);
 
402
      return 1;                 /* that's TRUE */
 
403
      }
373
404
   }
374
405
 
375
406
return 0;                       /* that's FALSE */
389
420
 */
390
421
/**********************************************************************/
391
422
 
392
 
int fileutils_file_is_there (const char * file_name)
393
 
 
394
 
{
395
 
int file_descriptor;
396
 
 
397
 
 
398
 
 
399
 
ER_TRACE(__func__)
400
 
 
401
 
if (file_name != NULL) 
402
 
   {
403
 
   file_descriptor = open (file_name, O_RDONLY);
404
 
   if (file_descriptor > -1)
405
 
      {
406
 
      (void) close (file_descriptor);
407
 
      return 1;
408
 
      }
409
 
   }
410
 
 
411
 
return 0;
412
 
 
413
 
}                               /* End of fileutils_file_is_there() */
414
 
/*
415
 
^L
416
 
*/
417
 
 
418
 
/**********************************************************************/
419
 
/**
420
 
 * @brief
421
 
 *   Checks whether the given file actually exists
422
 
 *
423
 
 * @param file_name Filename
424
 
 *
425
 
 * @returns 1 if file exists, 0 otherwise
426
 
 */
427
 
/**********************************************************************/
428
 
 
429
423
int fileutils_file_exists (const char * file_name)
430
424
 
431
425
{
439
433
ER_TRACE(__func__)
440
434
 
441
435
if (file_name != NULL) 
442
 
   {
443
 
   resname = er_fileutils_tilde_replace (file_name);
 
436
   {                            /* no need to save the pointer, here! */
 
437
   resname = er_fileutils_dot_replace (er_fileutils_tilde_replace (file_name));
 
438
 
444
439
   file_descriptor = open (resname, O_RDONLY);
445
440
   if (file_descriptor > -1)
446
441
      {
449
444
      }
450
445
   }
451
446
 
452
 
return 0;
453
 
 
454
 
}                               /* End of fileutils_file_exists() */
 
447
return 0;                       /* that's FALSE */
 
448
455
449
/*
456
450
^L
457
451
*/
734
728
 * returns and the source file is left untouched.
735
729
 */
736
730
 
 
731
 
 
732
 
737
733
int fileutils_move (const char *srcpath, const char *dstpath)
738
734
 
739
735
{
767
763
   (void) unlink (srcpath);
768
764
return 0;
769
765
 
770
 
}
 
766
 
767
 
 
768
 
771
769
/*
772
770
^L
773
771
*/
847
845
 
848
846
/* check if link already there... */
849
847
 
850
 
if (fileutils_file_is_there (linkname) == 1) 
 
848
/* if (fileutils_file_is_there (linkname) == 1)  */
 
849
if (fileutils_file_exists (linkname) == 1) 
851
850
   {
852
851
   cpl_msg_warning (er_func, "the link %s already exists - will be overwritten!",linkname);
853
852
   }
1005
1004
 
1006
1005
 
1007
1006
 
1008
 
/*            orig code of Enrique
 
1007
/*            orig code of Enrique using CFITSIO functions
1009
1008
 
1010
1009
int er_fileutils_file_is_fits(const char * self)
1011
1010
{