Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_FileInfo.C

raw
1 // Copyright(C) 1999-2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "Ioss_CodeTypes.h"
8 #include "Ioss_FileInfo.h"
9 #include "Ioss_ParallelUtils.h"
10 #include "Ioss_Utils.h"
11 #include <cstring>
12 #include <ostream>
13 #include <stdlib.h>
14 #include <string>
15 #include <tokenize.h>
16 #include <vector>
17
18 #if defined(__IOSS_WINDOWS__)
19 #include <direct.h>
20 #include <io.h>
21
22 #define access _access
23 #define R_OK 4 /* Test for read permission. */
24 #define W_OK 2 /* Test for write permission. */
25 #define X_OK 1 /* execute permission - unsupported in windows*/
26 #define F_OK 0 /* Test for existence. */
27 #ifndef S_ISREG
28 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
29 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
30 #endif
31 #else
32 #include <unistd.h>
33 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
34 #include <sys/mount.h>
35 #elif defined(__OpenBSD__)
36 #include <sys/mount.h>
37 #include <sys/types.h>
38 #else
39 #include <sys/statfs.h>
40 #endif
41 #endif
42
43 #ifdef SEACAS_HAVE_MPI
44 #include <numeric>
45 #endif
46
47 #include <cstdio>
48 #include <sys/stat.h>
49
50 namespace {
51 bool internal_access(const std::string &name, int mode);
52 bool do_stat(const std::string &filename, struct stat *s);
53 } // namespace
54
55 namespace Ioss {
56
57 FileInfo::FileInfo() = default;
58
59 FileInfo::FileInfo(std::string my_filename) : filename_(std::move(my_filename))
60 {
61 readable_ = internal_access(filename_, R_OK);
62 exists_ = readable_ || internal_access(filename_, F_OK);
63 }
64
65 FileInfo::FileInfo(const char *my_filename) : filename_(std::string(my_filename))
66 {
67 readable_ = internal_access(filename_, R_OK);
68 exists_ = readable_ || internal_access(filename_, F_OK);
69 }
70
71 FileInfo::FileInfo(const FileInfo &copy_from) = default;
72
73 FileInfo::FileInfo(const std::string &dirpath, const std::string &my_filename)
74 {
75
76 if (!dirpath.empty()) {
77 filename_ = dirpath;
78 if (filename_.at(filename_.size() - 1) != '/') {
79 static std::string SLASH("/");
80 filename_ += SLASH;
81 }
82 }
83 filename_ += my_filename;
84 readable_ = internal_access(filename_, R_OK);
85 exists_ = readable_ || internal_access(filename_, F_OK);
86 }
87
88 //: Returns TRUE if the file exists (is readable)
89 bool FileInfo::exists() const { return exists_; }
90
91 int FileInfo::parallel_exists(IOSS_MAYBE_UNUSED Ioss_MPI_Comm communicator,
92 IOSS_MAYBE_UNUSED std::string &where) const
93 {
94 IOSS_PAR_UNUSED(communicator);
95 IOSS_PAR_UNUSED(where);
96 int sum = exists_ ? 1 : 0;
97
98 #ifdef SEACAS_HAVE_MPI
99 Ioss::ParallelUtils pu(communicator);
100 int my_rank = pu.parallel_rank();
101 int my_size = pu.parallel_size();
102 if (my_size > 1) {
103 // Handle the parallel case
104 std::vector<int> result;
105 pu.all_gather(sum, result);
106 sum = std::accumulate(result.begin(), result.end(), 0);
107 if (my_rank == 0 && sum < my_size) {
108 std::vector<size_t> procs;
109 for (int i = 0; i < my_size; i++) {
110 if (result[i] == 0) {
111 procs.push_back(i);
112 }
113 }
114 where = Ioss::Utils::format_id_list(procs, "--");
115 }
116 }
117 #endif
118 return sum;
119 }
120
121 //: Returns TRUE if the file is readable
122 bool FileInfo::is_readable() const { return readable_; }
123
124 //: Returns TRUE if the file is writable
125 bool FileInfo::is_writable() const { return internal_access(filename_, W_OK); }
126
127 //: Returns TRUE if the file is executable
128 bool FileInfo::is_executable() const { return internal_access(filename_, X_OK); }
129
130 //: Returns TRUE if we are pointing to a file or a symbolic link to
131 //: a file.
132 bool FileInfo::is_file() const
133 {
134 struct stat s
135 {
136 };
137 if (do_stat(filename_, &s)) {
138 return S_ISREG(s.st_mode);
139 }
140
141 return false;
142 }
143
144 //: Returns TRUE if we are pointing to a directory or a symbolic link to
145 //: a directory.
146 bool FileInfo::is_dir() const
147 {
148 struct stat s
149 {
150 };
151 if (do_stat(filename_, &s)) {
152 return S_ISDIR(s.st_mode);
153 }
154
155 return false;
156 }
157
158 //: Returns TRUE if we are pointing to a symbolic link
159 bool FileInfo::is_symlink() const
160 {
161 #if !defined(__IOSS_WINDOWS__)
162 struct stat s
163 {
164 };
165 if (lstat(filename_.c_str(), &s) == 0) {
166 return S_ISLNK(s.st_mode);
167 }
168 #endif
169 return false;
170 }
171
172 //: Return TRUE if file is on an NFS filesystem...
173 bool FileInfo::is_nfs() const
174 {
175 #if !defined(__IOSS_WINDOWS__)
176 auto tmp_path = pathname();
177 if (tmp_path.empty()) {
178 char *current_cwd = getcwd(nullptr, 0);
179 tmp_path = std::string(current_cwd);
180 free(current_cwd);
181 }
182 char *path = ::realpath(tmp_path.c_str(), nullptr);
183 if (path != nullptr) {
184
185 struct statfs stat_fs;
186 // We want to run `statfs` on the path; not the filename since it might not exist.
187 if (statfs(path, &stat_fs) == -1) {
188 free(path);
189 std::ostringstream errmsg;
190 errmsg << "ERROR: Could not run statfs on '" << filename_ << "'.\n";
191 IOSS_ERROR(errmsg);
192 }
193 free(path);
194
195 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
196 return (0 == ::strcmp("nfs", stat_fs.f_fstypename));
197 #else
198 /* linux statfs defines that 0x6969 is NFS filesystem */
199 return (stat_fs.f_type == 0x6969);
200 #endif
201 }
202 #endif
203 return false;
204 }
205
206 //: Time of last data modification. See 'man stat(2)'
207 time_t FileInfo::modified() const
208 {
209 struct stat s
210 {
211 };
212 if (do_stat(filename_, &s)) {
213 return s.st_mtime;
214 }
215
216 return 0;
217 }
218
219 //: Time of last access
220 time_t FileInfo::accessed() const
221 {
222 struct stat s
223 {
224 };
225 if (do_stat(filename_, &s)) {
226 return s.st_atime;
227 }
228
229 return 0;
230 }
231
232 //: Time of last status change. (creation, chmod, ...)
233 time_t FileInfo::created() const
234 {
235 struct stat s
236 {
237 };
238 if (do_stat(filename_, &s)) {
239 return s.st_ctime;
240 }
241
242 return 0;
243 }
244
245 //: File size in bytes. Only if is_file() == true
246 off_t FileInfo::size() const
247 {
248 struct stat s
249 {
250 };
251 if (do_stat(filename_, &s)) {
252 return s.st_size;
253 }
254
255 return 0;
256 }
257
258 //: Returns the filename
259 std::string FileInfo::filename() const { return filename_; }
260
261 //: Sets the filename
262 void FileInfo::set_filename(const std::string &name)
263 {
264 filename_ = name;
265 readable_ = internal_access(filename_, R_OK);
266 exists_ = readable_ || internal_access(filename_, F_OK);
267 }
268
269 //: Sets the filename
270 void FileInfo::set_filename(const char *name)
271 {
272 filename_ = std::string(name);
273 readable_ = internal_access(filename_, R_OK);
274 exists_ = readable_ || internal_access(filename_, F_OK);
275 }
276
277 //: Returns the filename extension or the empty string if there is
278 //: no extension. Assumes extension is all characters following the
279 //: last period.
280 std::string FileInfo::extension() const
281 {
282 size_t ind = filename_.find_last_of('.', std::string::npos);
283 size_t inds = filename_.find_last_of('/', std::string::npos);
284
285 // Protect against './filename' returning /filename as extension
286 if (ind != std::string::npos && (inds == std::string::npos || inds < ind)) {
287 return filename_.substr(ind + 1, filename_.size());
288 }
289
290 return {};
291 }
292
293 std::string FileInfo::pathname() const
294 {
295 size_t ind = filename_.find_last_of('/', filename_.size());
296 if (ind != std::string::npos) {
297 return filename_.substr(0, ind);
298 }
299
300 return {};
301 }
302
303 std::string FileInfo::tailname() const
304 {
305 size_t ind = filename_.find_last_of('/', filename_.size());
306 if (ind != std::string::npos) {
307 return filename_.substr(ind + 1, filename_.size());
308 }
309
310 return filename_; // No path, just return the filename
311 }
312
313 std::string FileInfo::basename() const
314 {
315 std::string tail = tailname();
316
317 // Strip off the extension
318 size_t ind = tail.find_last_of('.', tail.size());
319 if (ind != std::string::npos) {
320 return tail.substr(0, ind);
321 }
322
323 return tail;
324 }
325
326 std::string FileInfo::realpath() const
327 {
328 #if defined(__IOSS_WINDOWS__)
329 char *path = _fullpath(nullptr, filename_.c_str(), _MAX_PATH);
330 #else
331 char *path = ::realpath(filename_.c_str(), nullptr);
332 #endif
333 if (path != nullptr) {
334 std::string temp(path);
335 free(path);
336 return temp;
337 }
338 return filename_;
339 }
340
341 bool FileInfo::remove_file()
342 {
343 int success = std::remove(filename_.c_str());
344 return success == 0;
345 }
346
347 void FileInfo::create_path(const std::string &filename)
348 {
349 bool error_found = false;
350 std::ostringstream errmsg;
351
352 Ioss::FileInfo file = Ioss::FileInfo(filename);
353 std::string path = file.pathname();
354 std::string path_root = path[0] == '/' ? "/" : "";
355
356 auto comps = tokenize(path, "/");
357 for (const auto &comp : comps) {
358 path_root += comp;
359
360 struct stat st;
361 if (stat(path_root.c_str(), &st) != 0) {
362 #if defined(__IOSS_WINDOWS__)
363 if (mkdir(path_root.c_str()) != 0 && errno != EEXIST) {
364 #else
365 const int mode = 0777; // Users umask will be applied to this.
366 if (mkdir(path_root.c_str(), mode) != 0 && errno != EEXIST) {
367 #endif
368 errmsg << "ERROR: Cannot create directory '" << path_root << "': " << std::strerror(errno)
369 << "\n";
370 error_found = true;
371 break;
372 }
373 }
374 else if (!S_ISDIR(st.st_mode)) {
375 errno = ENOTDIR;
376 errmsg << "ERROR: Path '" << path_root << "' is not a directory.\n";
377 error_found = true;
378 break;
379 }
380 path_root += "/";
381 }
382
383 if (error_found) {
384 IOSS_ERROR(errmsg);
385 }
386 }
387
388 void FileInfo::create_path(const std::string &filename,
389 IOSS_MAYBE_UNUSED Ioss_MPI_Comm communicator)
390 {
391 IOSS_PAR_UNUSED(communicator);
392 #ifdef SEACAS_HAVE_MPI
393 int error_found = 0;
394 std::ostringstream errmsg;
395
396 Ioss::ParallelUtils util(communicator);
397
398 if (util.parallel_rank() == 0) {
399 try {
400 create_path(filename);
401 }
402 catch (const std::exception &x) {
403 errmsg << x.what();
404 error_found = 1;
405 }
406 }
407 else {
408 errmsg << "ERROR: Could not create path '" << filename << "'.\n";
409 }
410
411 util.broadcast(error_found);
412 if (error_found) {
413 IOSS_ERROR(errmsg);
414 }
415 #else
416 create_path(filename);
417 #endif
418 }
419
420 } // namespace Ioss
421
422 namespace {
423 bool internal_access(const std::string &name, int mode)
424 {
425 if (name.empty()) {
426 return false;
427 }
428 if (::access(name.c_str(), mode) != 0) {
429 return false;
430 }
431 return true;
432 }
433
434 bool do_stat(const std::string &filename, struct stat *s)
435 {
436 return (stat(filename.c_str(), s) == 0);
437 }
438 } // namespace