Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
filesystem-util.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <fcntl.h>
16 #include <sys/stat.h>
17 #include <boost/filesystem.hpp>
18 #include <gutil/strings/substitute.h>
19 
20 #include "util/filesystem-util.h"
21 #include "util/error-util.h"
22 
23 #include "common/names.h"
24 
25 using std::exception;
26 using namespace strings;
27 
28 // boost::filesystem functions, which may throw exceptions, are used with their
29 // fully-qualified names to explicitly distinguish them from unix system
30 // functions that do not throw exceptions.
31 namespace impala {
32 
33 Status FileSystemUtil::CreateDirectories(const vector<string>& directories) {
34  for (int i = 0; i < directories.size(); ++i) {
35  // Remove the directory and it's contents if it exists. Ignore the error
36  // that occurs if the directory doesn't exist. Only report the error from
37  // create directory.
38  try {
39  boost::filesystem::remove_all(directories[i]);
40  } catch (exception& e) {
41  }
42  try {
43  boost::filesystem::create_directory(directories[i]);
44  } catch (exception& e) {
45  return Status(ErrorMsg(TErrorCode::RUNTIME_ERROR, Substitute(
46  "Encountered error creating directory $0: $1", directories[i], e.what())));
47  }
48  }
49 
50  return Status::OK;
51 }
52 
53 Status FileSystemUtil::RemovePaths(const vector<string>& directories) {
54  for (int i = 0; i < directories.size(); ++i) {
55  try {
56  boost::filesystem::remove_all(directories[i]);
57  } catch (exception& e) {
58  return Status(ErrorMsg(TErrorCode::RUNTIME_ERROR, Substitute(
59  "Encountered error removing directory $0: $1", directories[i], e.what())));
60  }
61  }
62 
63  return Status::OK;
64 }
65 
66 Status FileSystemUtil::CreateFile(const string& file_path) {
67  int fd = creat(file_path.c_str(), S_IRUSR | S_IWUSR);
68 
69  if (fd < 0) {
70  return Status(ErrorMsg(TErrorCode::RUNTIME_ERROR,
71  Substitute("Create file $0 failed with errno=$1 description=$2",
72  file_path.c_str(), errno, GetStrErrMsg())));
73  }
74 
75  int success = close(fd);
76  if (success < 0) {
77  return Status(ErrorMsg(TErrorCode::RUNTIME_ERROR,
78  Substitute("Close file $0 failed with errno=$1 description=$2",
79  file_path.c_str(), errno, GetStrErrMsg())));
80  }
81 
82  return Status::OK;
83 }
84 
85 Status FileSystemUtil::ResizeFile(const string& file_path, int64_t trunc_len) {
86  int success = truncate(file_path.c_str(), trunc_len);
87  if (success != 0) {
88  return Status(ErrorMsg(TErrorCode::RUNTIME_ERROR, Substitute(
89  "Truncate file $0 to length $1 failed with errno $2 ($3)",
90  file_path, trunc_len, errno, GetStrErrMsg())));
91  }
92 
93  return Status::OK;
94 }
95 
96 Status FileSystemUtil::VerifyIsDirectory(const string& directory_path) {
97  try {
98  if (!boost::filesystem::exists(directory_path)) {
99  return Status(ErrorMsg(TErrorCode::RUNTIME_ERROR, Substitute(
100  "Directory path $0 does not exist", directory_path)));
101  }
102  } catch (exception& e) {
103  return Status(ErrorMsg(TErrorCode::RUNTIME_ERROR, Substitute(
104  "Encountered exception while verifying existence of directory path $0: $1",
105  directory_path, e.what())));
106  }
107  if (!boost::filesystem::is_directory(directory_path)) {
108  return Status(ErrorMsg(TErrorCode::RUNTIME_ERROR, Substitute(
109  "Path $0 is not a directory", directory_path)));
110  }
111  return Status::OK;
112 }
113 
114 Status FileSystemUtil::GetSpaceAvailable(const string& directory_path,
115  uint64_t* available_bytes) {
116  try {
117  boost::filesystem::space_info info = boost::filesystem::space(directory_path);
118  *available_bytes = info.available;
119  } catch (exception& e) {
120  return Status(ErrorMsg(TErrorCode::RUNTIME_ERROR, Substitute(
121  "Encountered exception while checking available space for path $0: $1",
122  directory_path, e.what())));
123  }
124 
125  return Status::OK;
126 }
127 
128 } // namespace impala
string space[]
string GetStrErrMsg()
Definition: error-util.cc:30