DABC (Data Acquisition Backbone Core)  2.9.9
BinaryFile.cxx
Go to the documentation of this file.
1 // $Id: BinaryFile.cxx 4724 2021-03-13 17:50:59Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #include "dabc/BinaryFile.h"
17 
18 #include <unistd.h>
19 #include <cstdlib>
20 #include <dirent.h>
21 #include <fnmatch.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <cstring>
25 
26 #include "dabc/Object.h"
27 #include "dabc/logging.h"
28 
29 bool dabc::FileInterface::mkdir(const char* path)
30 {
31  if ((path==0) || (*path==0)) return false;
32 
33  const char* part = path;
34 
35  // we should ensure that part is exists
36  while (part != 0) {
37  part = strchr(part+1, '/');
38 
39  std::string subname;
40  if (part == 0)
41  subname = path;
42  else
43  subname.append(path, part - path);
44 
45  struct stat buf;
46  if (stat(subname.c_str(), &buf) < 0) {
47  if (::mkdir(subname.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) return false;
48  } else {
49  if (!S_ISDIR(buf.st_mode)) {
50  EOUT("Existing path %s is not a directory", subname.c_str());
51  return false;
52  }
53  }
54  }
55 
56  return true;
57 }
58 
59 
60 dabc::Object* dabc::FileInterface::fmatch(const char* fmask, bool select_files)
61 {
62  if (!fmask || (*fmask==0)) return nullptr;
63 
64  std::string pathname;
65  const char* fname = nullptr;
66 
67  const char* slash = strrchr(fmask, '/');
68 
69  if (slash==0) {
70  pathname = ".";
71  fname = fmask;
72  } else {
73  pathname.assign(fmask, slash - fmask + 1);
74  fname = slash + 1;
75  }
76 
77  struct dirent **namelist;
78  int len = scandir(pathname.c_str(), &namelist, 0, alphasort);
79  if (len < 0) return nullptr;
80 
81  Object* res = nullptr;
82  struct stat buf;
83 
84  for (int n=0;n<len;n++) {
85  const char* item = namelist[n]->d_name;
86  // exclude all files/directory names starting with .
87  // we exclude all hidden files or directory likes '.' and '..'
88  if ((item==0) || (*item == '.')) continue;
89 
90  if ((fname==0) || (fnmatch(fname, item, FNM_NOESCAPE)==0)) {
91  std::string fullitemname;
92  if (slash) fullitemname += pathname;
93  fullitemname += item;
94  if (stat(fullitemname.c_str(), &buf)!=0) continue;
95 
96  if ((select_files && !S_ISDIR(buf.st_mode) && (access(fullitemname.c_str(), R_OK)==0)) ||
97  (!select_files && S_ISDIR(buf.st_mode) && (access(fullitemname.c_str(), R_OK | X_OK)==0))) {
98  if (!res) res = new dabc::Object(0, "FilesList");
99  new dabc::Object(res, fullitemname);
100  }
101  }
102 
103  free(namelist[n]);
104  }
105 
106  free(namelist);
107 
108  return res;
109 }
virtual bool mkdir(const char *path)
Definition: BinaryFile.cxx:29
virtual Object * fmatch(const char *fmask, bool select_files=true)
Produce list of files, object must be explicitly destroyed with ref.Destroy call One could decide if ...
Definition: BinaryFile.cxx:60
Base class for most of the DABC classes.
Definition: Object.h:116
#define EOUT(args ...)
Definition: logging.h:150