MED fichier: Fichier source de MEDfileExist.c

MED fichier

MEDfileExist.c
Aller à la documentation de ce fichier.
1 /* This file is part of MED.
2 *
3 * COPYRIGHT (C) 1999 - 2023 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_err _ret = -1;
57 int _mode = R_OK;
58 char *_filename = NULL;
59
60 #ifdef PPRO_NT_CALL
61 char _dirname[_MAX_PATH];
62 char _drive[_MAX_DRIVE];
63 char _direc[_MAX_DIR];
64 #else
65 char *_dirname = NULL;
66 #endif
67
68 /*
69 * On inhibe le gestionnaire d'erreur HDF
70 */
71 _MEDmodeErreurVerrouiller();
72
73 /*
74 * On ouvre le fichier MED sous HDF
75 */
76 if ( access(filename,F_OK) ) {
77 /*Le fichier n'existe pas*/
78 *fileexist = MED_FALSE;
79 *accessok = MED_FALSE;
80 if (accessmode==MED_ACC_RDONLY) goto SORTIE;
81 /*Si le fichier n'existe pas, il est necessaire d'avoir
82 les droits d'accès et d'écriture au répertoire*/
83
84 #ifndef PPRO_NT_CALL
85 _mode = _mode|W_OK|X_OK;
86 if (!(_filename = strndup (filename,2*MED_PATHNAME_SIZE))) {
87 MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"strndup");
88 SSCRUTE(filename);goto ERROR;
89 }
90 #else
91 /* access windows ne support pas |X_OK; */
92 /* - Cette fct ne vérifie pas les paramètres de sécurité du système de fichiers
93 (besoin d'un token pour çà)
94 - Dans les systèmes d’exploitation Windows 2000 et versions ultérieures,
95 tous les répertoires disposent d’un accès en lecture et en écriture.
96 */
97 _mode = _mode|W_OK;
98 if (!(_filename = _strdup (filename))) {
99 MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"strdup");
100 SSCRUTE(filename);goto ERROR;
101 }
102 #endif
103
104 #ifndef PPRO_NT_CALL
105 if (!(_dirname = dirname(_filename))) {
106 MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"dirname");
107 SSCRUTE(_filename);goto ERROR;
108 }
109 #else
110 _splitpath(_filename, _drive, _direc, NULL, NULL);
111 /*Le répertoire peut être vide */
112 /* if (!strlen(_direc)) { */
113 /* MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"_splitpath"); */
114 /* SSCRUTE(_filename);SSCRUTE(_direc);SSCRUTE(_drive);goto ERROR; */
115 /* } */
116 // Join drive letter and directory name:
117 _makepath(_dirname, _drive, _direc, NULL, NULL);
118 #endif
119 if ( _MEDaccess(_dirname,_mode) ) *accessok = MED_FALSE; else *accessok = MED_TRUE;
120
121 } else {
122
123 /*Le fichier existe*/
124 *fileexist = MED_TRUE;
125 /*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*/
126 (accessmode!=MED_ACC_RDONLY) && (_mode=_mode|W_OK);
127 if ( _MEDaccess(filename,_mode) ) *accessok = MED_FALSE; else *accessok = MED_TRUE;
128
129 }
130
131
132 SORTIE:
133 _ret=0;
134
135 ERROR:
136
137 if (_filename) free(_filename);
138
139 return _ret;
140 }
141
MED_ACC_RDONLY
Definition: med.h:122
MED_ERR_API
#define MED_ERR_API
Definition: med_err.h:111
MED_TRUE
Definition: med.h:262
filename
#define filename
Definition: 4.0.1/test10.c:75
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_FALSE
Definition: med.h:262
MED_ERR_
#define MED_ERR_(rt, r1, r2, r3)
Definition: med_utils.h:160
med_err
herr_t med_err
Definition: med.h:334
med_outils.h
_MEDaccess
MEDC_EXPORT int _MEDaccess(const char *const file, int mode)
med_config.h
med_bool
med_bool
Definition: med.h:262
MED_ERR_CALL
#define MED_ERR_CALL
Definition: med_err.h:48
med_access_mode
med_access_mode
Definition: med.h:122
MED_PATHNAME_SIZE
#define MED_PATHNAME_SIZE
Definition: med.h:91
SSCRUTE
#define SSCRUTE(chaine)
Definition: med_utils.h:323
med.h
_MEDmodeErreurVerrouiller
MEDC_EXPORT void _MEDmodeErreurVerrouiller(void)