0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DirEntryAttr.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; either version 3
9  * of the 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 
22 #include <Common/Compat.h>
23 
24 #include "DirEntryAttr.h"
25 
26 #include <Common/Serialization.h>
27 #include <Common/Logger.h>
28 #include <Common/DynamicBuffer.h>
29 
30 using namespace Hypertable;
31 using namespace Serialization;
32 
33 namespace Hyperspace {
34 
36  size_t len = 2 + encoded_length_vstr(entry.name) + 4 + entry.attr.size + 4;
37  for (const auto &sub_entry : entry.sub_entries)
38  len += encoded_length_dir_entry_attr(sub_entry);
39  return len;
40  }
41 
42  void encode_dir_entry_attr(uint8_t **bufp, const DirEntryAttr &entry) {
43  encode_bool(bufp, entry.has_attr);
44  encode_bool(bufp, entry.is_dir);
45  encode_vstr(bufp, entry.name);
46  encode_bytes32(bufp, (void *)entry.attr.base, entry.attr.size);
47  encode_i32(bufp, entry.sub_entries.size());
48  for (const auto &sub_entry : entry.sub_entries)
49  encode_dir_entry_attr(bufp, sub_entry);
50  }
51 
52  DirEntryAttr &
53  decode_dir_entry_attr(const uint8_t **bufp, size_t *remainp, DirEntryAttr &entry) {
54  try {
55  entry.has_attr = decode_bool(bufp, remainp);
56  entry.is_dir = decode_bool(bufp, remainp);
57  entry.name = decode_vstr(bufp, remainp);
58  uint32_t attr_val_len;
59  void *attr_val= decode_bytes32(bufp, remainp, &attr_val_len);
60 
61  DynamicBuffer buffer(attr_val_len+1);
62  buffer.add_unchecked(attr_val, attr_val_len);
63  // nul-terminate to make caller's lives easier
64  *buffer.ptr = 0;
65  buffer.size = attr_val_len+1;
66  entry.attr = buffer;
67 
68  DirEntryAttr sub_entry;
69  uint32_t sub_entries_count = decode_i32(bufp, remainp);
70  entry.sub_entries.clear();
71  entry.sub_entries.reserve(sub_entries_count);
72  while (sub_entries_count--) {
73  entry.sub_entries.push_back(decode_dir_entry_attr(bufp, remainp, sub_entry));
74  }
75  }
76  catch (Exception &e) {
77  HT_THROW2(e.code(), e, "Error deserializing DirEntryAttr");
78  }
79  return entry;
80  }
81 }
char * decode_vstr(const uint8_t **bufp, size_t *remainp)
Decode a vstr (vint64, data, null).
bool is_dir
Definition: DirEntryAttr.h:66
StaticBuffer attr
Definition: DirEntryAttr.h:63
size_t encoded_length_vstr(size_t len)
Computes the encoded length of vstr (vint64, data, null)
uint8_t * ptr
Pointer to the end of the used part of the buffer.
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
Hyperspace definitions
Definition: DirEntryAttr.h:40
A dynamic, resizable memory buffer.
std::string name
Definition: DirEntryAttr.h:62
bool decode_bool(const uint8_t **bufp, size_t *remainp)
Decodes a boolean value from the given buffer.
Definition: Serialization.h:96
bool has_attr
Boolean value indicating whether or not this entry is a directory.
Definition: DirEntryAttr.h:65
void encode_dir_entry_attr(uint8_t **bufp, const DirEntryAttr &entry)
Encodes (serializes) the given directory entry to a buffer.
Definition: DirEntryAttr.cc:42
uint32_t size
The size of the allocated memory buffer (base)
Logging routines and macros.
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Compatibility Macros for C/C++.
Functions to serialize/deserialize primitives to/from a memory buffer.
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
DirEntryAttr & decode_dir_entry_attr(const uint8_t **bufp, size_t *remainp, DirEntryAttr &entry)
Decodes (unserializes) a directory entry from a buffer.
Definition: DirEntryAttr.cc:53
uint8_t * decode_bytes32(const uint8_t **bufp, size_t *remainp, uint32_t *lenp)
Decodes a variable sized byte array from the given buffer.
This is a generic exception class for Hypertable.
Definition: Error.h:314
void encode_bytes32(uint8_t **bufp, const void *data, int32_t len)
Encodes a variable sized byte array into the given buffer.
uint8_t * add_unchecked(const void *data, size_t len)
Adds additional data without boundary checks.
std::vector< DirEntryAttr > sub_entries
Definition: DirEntryAttr.h:68
int code() const
Returns the error code.
Definition: Error.h:391
#define HT_THROW2(_code_, _ex_, _msg_)
Definition: Error.h:484
size_t encoded_length_dir_entry_attr(const DirEntryAttr &entry)
Returns the number of bytes required to encode (serialize) the given directory entry.
Definition: DirEntryAttr.cc:35