Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hdfs-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 "util/hdfs-util.h"
16 
17 #include <sstream>
18 #include <string.h>
19 
20 #include "util/error-util.h"
21 
22 #include "common/names.h"
23 
24 namespace impala {
25 
26 string GetHdfsErrorMsg(const string& prefix, const string& file) {
27  string error_msg = GetStrErrMsg();
28  stringstream ss;
29  ss << prefix << file << "\n" << error_msg;
30  return ss.str();
31 }
32 
33 Status GetFileSize(const hdfsFS& connection, const char* filename, int64_t* filesize) {
34  hdfsFileInfo* info = hdfsGetPathInfo(connection, filename);
35  if (info == NULL) return Status(GetHdfsErrorMsg("Failed to get file info ", filename));
36  *filesize = info->mSize;
37  hdfsFreeFileInfo(info, 1);
38  return Status::OK;
39 }
40 
41 Status GetLastModificationTime(const hdfsFS& connection, const char* filename,
42  time_t* last_mod_time) {
43  hdfsFileInfo* info = hdfsGetPathInfo(connection, filename);
44  if (info == NULL) return Status(GetHdfsErrorMsg("Failed to get file info ", filename));
45  *last_mod_time = info->mLastMod;
46  hdfsFreeFileInfo(info, 1);
47  return Status::OK;
48 }
49 
50 bool IsHiddenFile(const string& filename) {
51  return !filename.empty() && (filename[0] == '.' || filename[0] == '_');
52 }
53 
54 Status CopyHdfsFile(const hdfsFS& src_conn, const string& src_path,
55  const hdfsFS& dst_conn, const string& dst_path) {
56  int error = hdfsCopy(src_conn, src_path.c_str(), dst_conn, dst_path.c_str());
57  if (error != 0) {
58  string error_msg = GetHdfsErrorMsg("");
59  stringstream ss;
60  ss << "Failed to copy " << src_path << " to " << dst_path << ": " << error_msg;
61  return Status(ss.str());
62  }
63  return Status::OK;
64 }
65 
66 bool IsDfsPath(const char* path) {
67  // TODO: currently, we require defaultFS to be HDFS, but when that is relaxed, we
68  // should fix this to not assume unqualified paths are DFS.
69  return strncmp(path, "hdfs://", 7) == 0 || strstr(path, ":/") == NULL;
70 }
71 
72 bool IsS3APath(const char* path) {
73  return strncmp(path, "s3a://", 6) == 0;
74 }
75 
76 }
bool IsS3APath(const char *path)
Returns true iff the path refers to a location on an S3A filesystem.
Definition: hdfs-util.cc:72
string path("/usr/lib/sasl2:/usr/lib64/sasl2:/usr/local/lib/sasl2:/usr/lib/x86_64-linux-gnu/sasl2")
string GetStrErrMsg()
Definition: error-util.cc:30
Status GetLastModificationTime(const hdfsFS &connection, const char *filename, time_t *last_mod_time)
Definition: hdfs-util.cc:41
Status CopyHdfsFile(const hdfsFS &src_conn, const string &src_path, const hdfsFS &dst_conn, const string &dst_path)
Definition: hdfs-util.cc:54
static const Status OK
Definition: status.h:87
bool IsHiddenFile(const string &filename)
Definition: hdfs-util.cc:50
string GetHdfsErrorMsg(const string &prefix, const string &file)
Definition: hdfs-util.cc:26
bool IsDfsPath(const char *path)
Returns true iff the path refers to a location on an HDFS filesystem.
Definition: hdfs-util.cc:66
Status GetFileSize(const hdfsFS &connection, const char *filename, int64_t *filesize)
Return the size, in bytes, of a file from the hdfs connection.
Definition: hdfs-util.cc:33