FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
native_file.h
1 #ifndef __NATIVE_FILE_H__
2 #define __NATIVE_FILE_H__
3 
4 /*
5  * Copyright 2014 Open Connectome Project (http://openconnecto.me)
6  * Written by Da Zheng (zhengda1936@gmail.com)
7  *
8  * This file is part of SAFSlib.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <dirent.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <libgen.h>
31 
32 #include <string>
33 #include <vector>
34 
35 namespace safs
36 {
37 
38 class native_file
39 {
40  std::string file_name;
41 public:
42  native_file(const std::string &file_name) {
43  this->file_name = file_name;
44  }
45 
49  ssize_t get_size() const {
50  struct stat stats;
51  if (stat(file_name.c_str(), &stats) < 0) {
52  perror("stat");
53  return -1;
54  }
55  return stats.st_size;
56  }
57 
58  bool exist() const {
59  struct stat stats;
60  return stat(file_name.c_str(), &stats) == 0;
61  }
62 
63  bool is_dir() const {
64  struct stat stats;
65  if (stat(file_name.c_str(), &stats) < 0) {
66  perror("stat");
67  return false;
68  }
69  return S_ISDIR(stats.st_mode);
70  }
71 
72  const std::string get_file_name() const {
73  char *basec = strdup(file_name.c_str());
74  std::string ret = basename(basec);
75  free(basec);
76  return ret;
77  }
78 
79  const std::string get_dir_name() const {
80  char *dirc = strdup(file_name.c_str());
81  std::string ret = dirname(dirc);
82  free(dirc);
83  return ret;
84  }
85 
86  /*
87  * This returns the path to the file
88  */
89  const std::string &get_name() const {
90  return file_name;
91  }
92 
93  virtual bool rename(const std::string &new_name) {
94  if (::rename(file_name.c_str(), new_name.c_str()) == 0) {
95  file_name = new_name;
96  return true;
97  }
98  else
99  return false;
100  }
101 
106  bool create_file(size_t size) {
107  int fd = open(file_name.c_str(), O_WRONLY | O_CREAT,
108  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
109  if (fd < 0) {
110  fprintf(stderr, "can't create %s: %s\n", file_name.c_str(),
111  strerror(errno));
112  return false;
113  }
114  bool bret = true;
115  if (size > 0) {
116  int ret = posix_fallocate(fd, 0, size);
117  if (ret != 0) {
118  fprintf(stderr, "can't allocate %ld bytes for %s, error: %s\n",
119  size, file_name.c_str(), strerror(ret));
120  bret = false;
121  }
122  }
123  close(fd);
124  return bret;
125  }
126 
127  bool delete_file() {
128  int ret = unlink(file_name.c_str());
129  if (ret < 0) {
130  fprintf(stderr, "can't delete %s\n", file_name.c_str());
131  return false;
132  }
133  return true;
134  }
135 };
136 
137 class native_dir
138 {
139  std::string name;
140 public:
141  native_dir(const std::string &name) {
142  this->name = name;
143  }
144 
145  bool is_dir() const {
146  native_file f(name);
147  return f.is_dir();
148  }
149 
150  bool exist() const {
151  native_file f(name);
152  return f.exist();
153  }
154 
155  const std::string &get_name() const {
156  return name;
157  }
158 
159  ssize_t read_all_files(std::vector<std::string> &files) const;
160 
161  bool create_dir(bool recursive);
162  bool delete_dir(bool recursive);
163 };
164 
165 static inline bool file_exist(const std::string &file)
166 {
167  native_file f(file);
168  return f.exist();
169 }
170 
171 }
172 
173 #endif