MED fichier
MEDfileExist.c
Aller à la documentation de ce fichier.
1 /* This file is part of MED.
2  *
3  * COPYRIGHT (C) 1999 - 2021 EDF R&D, CEA/DEN
4  * MED is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * MED is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with MED. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 #include <med.h>
20 #include <med_config.h>
21 #include <med_outils.h>
22 
23 #ifdef PPRO_NT_CALL
24 #define R_OK 4 /* Test for read permission. */
25 #define W_OK 2 /* Test for write permission. */
26 #define X_OK 1 /* Test for execute permission. */
27 #define F_OK 0 /* Test for existence. */
28 #else
29 #include <unistd.h>
30 #endif
31 
32 #ifndef PPRO_NT_CALL
33 #include <libgen.h>
34 #endif
35 
36 #include <string.h>
37 
38 
50 med_err
51 MEDfileExist(const char* const filename,
52  const med_access_mode accessmode,
53  med_bool * const fileexist,
54  med_bool * const accessok )
55 {
56  med_idt _fid = 0;
57  med_err _ret = -1;
58  int _mode = R_OK;
59  char *_filename = NULL;
60 
61 #ifdef PPRO_NT_CALL
62  char _dirname[_MAX_PATH];
63  char _drive[_MAX_DRIVE];
64  char _direc[_MAX_DIR];
65 #else
66  char *_dirname = NULL;
67 #endif
68 
69  /*
70  * On inhibe le gestionnaire d'erreur HDF
71  */
73 
74  /*
75  * On ouvre le fichier MED sous HDF
76  */
77  if ( access(filename,F_OK) ) {
78  /*Le fichier n'existe pas*/
79  *fileexist = MED_FALSE;
80  *accessok = MED_FALSE;
81  if (accessmode==MED_ACC_RDONLY) goto SORTIE;
82  /*Si le fichier n'existe pas, il est necessaire d'avoir
83  les droits d'accès et d'écriture au répertoire*/
84 
85 #ifndef PPRO_NT_CALL
86  _mode = _mode|W_OK|X_OK;
87  if (!(_filename = strndup (filename,2*MED_PATHNAME_SIZE))) {
88  MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"strndup");
89  SSCRUTE(filename);SSCRUTE(_filename);goto ERROR;
90  }
91 #else
92  /* access windows ne support pas |X_OK; */
93  /* - Cette fct ne vérifie pas les paramètres de sécurité du système de fichiers
94  (besoin d'un token pour çà)
95  - Dans les systèmes d’exploitation Windows 2000 et versions ultérieures,
96  tous les répertoires disposent d’un accès en lecture et en écriture.
97  */
98  _mode = _mode|W_OK;
99  if (!(_filename = _strdup (filename))) {
100  MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"strdup");
101  SSCRUTE(filename);SSCRUTE(_filename);goto ERROR;
102  }
103 #endif
104 
105 #ifndef PPRO_NT_CALL
106  if (!(_dirname = dirname(_filename))) {
107  MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"dirname");
108  SSCRUTE(_filename);SSCRUTE(_dirname);goto ERROR;
109  }
110 #else
111  _splitpath(_filename, _drive, _direc, NULL, NULL);
112  /*Le répertoire peut être vide */
113  /* if (!strlen(_direc)) { */
114  /* MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"_splitpath"); */
115  /* SSCRUTE(_filename);SSCRUTE(_direc);SSCRUTE(_drive);goto ERROR; */
116  /* } */
117  // Join drive letter and directory name:
118  _makepath(_dirname, _drive, _direc, NULL, NULL);
119 #endif
120  if ( _MEDaccess(_dirname,_mode) ) *accessok = MED_FALSE; else *accessok = MED_TRUE;
121 
122  } else {
123 
124  /*Le fichier existe*/
125  *fileexist = MED_TRUE;
126  /*Si le mode d'accès au fichier est MED_ACC_RDWR our MED_ACC_CREATE, il faut vérifier l'accès en écriture*/
127  (accessmode!=MED_ACC_RDONLY) && (_mode=_mode|W_OK);
128  if ( _MEDaccess(filename,_mode) ) *accessok = MED_FALSE; else *accessok = MED_TRUE;
129 
130  }
131 
132 
133  SORTIE:
134  _ret=0;
135 
136  ERROR:
137 
138  if (_filename) free(_filename);
139 
140  return _ret;
141 }
142 
MED_ERR_API
#define MED_ERR_API
Definition: med_err.h:111
med_idt
hid_t med_idt
Definition: med.h:331
MED_ERR_
#define MED_ERR_(rt, r1, r2, r3)
Definition: med_utils.h:160
MED_ACC_RDONLY
Definition: med.h:120
med_err
herr_t med_err
Definition: med.h:332
MED_TRUE
Definition: med.h:260
_MEDaccess
MEDC_EXPORT int _MEDaccess(const char *const file, int mode)
MED_FALSE
Definition: med.h:260
MED_ERR_CALL
#define MED_ERR_CALL
Definition: med_err.h:48
med.h
med_bool
med_bool
Definition: med.h:260
MEDfileExist
med_err MEDfileExist(const char *const filename, const med_access_mode accessmode, med_bool *const fileexist, med_bool *const accessok)
Interroge l'existence d'un fichier de nom filename et la possibilité de l'ouvrir selon le mode d'accè...
Definition: MEDfileExist.c:51
med_access_mode
med_access_mode
Definition: med.h:120
filename
#define filename
Definition: test10.c:72
MED_PATHNAME_SIZE
#define MED_PATHNAME_SIZE
Definition: med.h:89
SSCRUTE
#define SSCRUTE(chaine)
Definition: med_utils.h:323
med_outils.h
med_config.h
_MEDmodeErreurVerrouiller
MEDC_EXPORT void _MEDmodeErreurVerrouiller(void)