0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Filesystem.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
32 #include <Common/Compat.h>
33 
34 #include "Filesystem.h"
35 
36 #include <Common/Error.h>
37 #include <Common/Serialization.h>
38 
39 #include <boost/algorithm/string.hpp>
40 
41 #include <strings.h>
42 
43 using namespace Hypertable;
44 using namespace Serialization;
45 
47  return 1;
48 }
49 
51  return 13 + encoded_length_vstr(name);
52 }
53 
54 void Filesystem::Dirent::encode_internal(uint8_t **bufp) const {
55  encode_vstr(bufp, name);
56  encode_i64(bufp, length);
57  encode_i32(bufp, last_modification_time);
58  encode_bool(bufp, is_dir);
59 }
60 
61 void Filesystem::Dirent::decode_internal(uint8_t version, const uint8_t **bufp,
62  size_t *remainp) {
63  name = decode_vstr(bufp, remainp);
64  length = decode_i64(bufp, remainp);
65  last_modification_time = decode_i32(bufp, remainp);
66  is_dir = decode_bool(bufp, remainp);
67 }
68 
69 
70 void
72  std::vector<Dirent> &listing) {
73  const uint8_t *decode_ptr = event_ptr->payload;
74  size_t decode_remain = event_ptr->payload_len;
75 
76  int error = decode_i32(&decode_ptr, &decode_remain);
77 
78  if (error != Error::OK)
79  HT_THROW(error, "");
80 
81  uint32_t len = decode_i32(&decode_ptr, &decode_remain);
82 
83  listing.clear();
84  listing.reserve(len);
85  Dirent entry;
86  for (uint32_t i = 0; i < len; i++) {
87  entry.decode(&decode_ptr, &decode_remain);
88  listing.push_back(entry);
89  }
90 }
91 
92 
93 bool
95  const uint8_t *decode_ptr = event_ptr->payload;
96  size_t decode_remain = event_ptr->payload_len;
97 
98  int error = decode_i32(&decode_ptr, &decode_remain);
99 
100  if (error != Error::OK)
101  HT_THROW(error, "");
102 
103  return decode_bool(&decode_ptr, &decode_remain);
104 }
105 
106 
107 int
109  const uint8_t *decode_ptr = event_ptr->payload;
110  size_t decode_remain = event_ptr->payload_len;
111 
112  return decode_i32(&decode_ptr, &decode_remain);
113 }
114 
115 static String &
116 remove_trailing_duplicates(String &s, char separator) {
117  while (s.size() > 1) {
118  if (s[s.size()-1] == separator)
119  s.resize(s.size() - 1);
120  else
121  break;
122  }
123 
124  return s;
125 }
126 
127 String Filesystem::dirname(String name, char separator) {
128  if (name.size() == 1 && name[0] == separator)
129  return name;
130 
131  // Remove trailing separators
132  std::string::size_type cur = name.length();
133  while (cur > 0 && name[cur - 1] == separator)
134  --cur;
135  name.resize(cur);
136 
137  // Find last separator
138  std::string::size_type pos = name.rfind(separator);
139 
140  if (pos == std::string::npos)
141  name = "."; // No separators
142  else if (pos == 0 && name.length() == 1 && name[0] == separator)
143  name = separator; // Only separators
144  else if (pos == 0 && name.length() > 1 && name[0] == separator)
145  name = "/";
146  else
147  name.resize(pos);
148 
149  // Remove any duplicate adjacent path separators
150  return remove_trailing_duplicates(name, separator);
151 }
152 
153 
154 String Filesystem::basename(String name, char separator) {
155  if (name.size() == 1 && name[0] == separator)
156  return name;
157 
158  // Remove trailing separators
159  std::string::size_type cur = name.length();
160  while (cur > 0 && name[cur - 1] == separator)
161  --cur;
162  name.resize(cur);
163 
164  // Find last separator
165  std::string::size_type pos = name.rfind(separator);
166 
167  std::string ret;
168  if (pos == std::string::npos)
169  ; // No separators
170  else if (pos == 0 && name.length() == 1 && name[0] == separator)
171  name = separator; // Only separators
172  else
173  name = name.substr(pos + 1); // Basename only
174 
175  // Remove any duplicate adjacent path separators
176  return remove_trailing_duplicates(name, separator);
177 }
178 
179 
181  boost::trim_if(str, boost::is_any_of("\"'"));
182  if (!strcasecmp(str.c_str(), "FLUSH"))
184  else if (!strcasecmp(str.c_str(), "SYNC"))
186  else if (!strcasecmp(str.c_str(), "NONE"))
188  HT_THROWF(Error::INVALID_ARGUMENT, "Unrecognized argument: %s", str.c_str());
189 }
char * decode_vstr(const uint8_t **bufp, size_t *remainp)
Decode a vstr (vint64, data, null).
static String & remove_trailing_duplicates(String &s, char separator)
Definition: Filesystem.cc:116
virtual bool decode_response_exists(EventPtr &event)=0
Decodes the response from an exists request.
Definition: Filesystem.cc:94
Abstract base class for a filesystem.
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Filesystem::Flags convert(std::string str)
Converts string mnemonic to corresponding Filesystem::Flags value.
Definition: Filesystem.cc:180
Flags
Enumeration type for append flags.
Definition: Filesystem.h:76
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
static int decode_response(EventPtr &event)
Decodes the response from an request that only returns an error code.
Definition: Filesystem.cc:108
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
size_t encoded_length_vstr(size_t len)
Computes the encoded length of vstr (vint64, data, null)
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
uint64_t decode_i64(const uint8_t **bufp, size_t *remainp)
Decode a 64-bit integer in little-endian order.
virtual void decode_response_readdir(EventPtr &event, std::vector< Dirent > &listing)=0
Decodes the response from a readdir request.
Definition: Filesystem.cc:71
bool decode_bool(const uint8_t **bufp, size_t *remainp)
Decodes a boolean value from the given buffer.
Definition: Serialization.h:96
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Compatibility Macros for C/C++.
void encode_i64(uint8_t **bufp, uint64_t val)
Encode a 64-bit integer in little-endian order.
Functions to serialize/deserialize primitives to/from a memory buffer.
static String basename(String name, char separator= '/')
A posix-compliant basename() which strips directory names from a filename.
Definition: Filesystem.cc:154
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
void encode_vstr(uint8_t **bufp, const void *buf, size_t len)
Encode a buffer as variable length string (vint64, data, null)
Hypertable definitions
void encode_bool(uint8_t **bufp, bool bval)
Encodes a boolean into the given buffer.
Definition: Serialization.h:84
void encode_internal(uint8_t **bufp) const override
Writes serialized representation of object to a buffer.
Definition: Filesystem.cc:54
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
size_t encoded_length_internal() const override
Returns internal serialized length.
Definition: Filesystem.cc:50
static String dirname(String name, char separator= '/')
A posix-compliant dirname() which strips the last component from a file name.
Definition: Filesystem.cc:127
uint8_t encoding_version() const override
Returns encoding version.
Definition: Filesystem.cc:46
Error codes, Exception handling, error logging.
#define HT_THROW(_code_, _msg_)
Definition: Error.h:478
void decode_internal(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Reads serialized representation of object from a buffer.
Definition: Filesystem.cc:61