-rw-r--r-- | src/eyetil.cc | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/eyetil.cc b/src/eyetil.cc index d00c2ee..2fbd687 100644 --- a/src/eyetil.cc +++ b/src/eyetil.cc | |||
@@ -1,103 +1,119 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include <sys/stat.h> | ||
2 | #include <syslog.h> | 3 | #include <syslog.h> |
3 | #include <iostream> | 4 | #include <iostream> |
4 | #include <cassert> | 5 | #include <cassert> |
5 | #include <stdexcept> | 6 | #include <stdexcept> |
6 | #include <openssl/md5.h> | 7 | #include <openssl/md5.h> |
7 | #include "eyetil.h" | 8 | #include "eyetil.h" |
8 | 9 | ||
9 | binary_t& binary_t::from_hex(const std::string& h) { | 10 | binary_t& binary_t::from_hex(const std::string& h) { |
10 | /* TODO: algorithmize */ | 11 | /* TODO: algorithmize */ |
11 | std::string::size_type hs = h.length(); | 12 | std::string::size_type hs = h.length(); |
12 | if(hs&1) | 13 | if(hs&1) |
13 | throw std::runtime_error("odd number of characters in hexadecimal number"); | 14 | throw std::runtime_error("odd number of characters in hexadecimal number"); |
14 | int rvs = hs>>1; | 15 | int rvs = hs>>1; |
15 | resize(rvs); | 16 | resize(rvs); |
16 | const unsigned char *hp = (const unsigned char*)h.data(); | 17 | const unsigned char *hp = (const unsigned char*)h.data(); |
17 | iterator oi=begin(); | 18 | iterator oi=begin(); |
18 | char t[3] = { 0,0,0 }; | 19 | char t[3] = { 0,0,0 }; |
19 | for(int i=0;i<rvs;++i) { | 20 | for(int i=0;i<rvs;++i) { |
20 | t[0]=*(hp++); t[1]=*(hp++); | 21 | t[0]=*(hp++); t[1]=*(hp++); |
21 | *(oi++) = strtol(t,0,16); | 22 | *(oi++) = strtol(t,0,16); |
22 | } | 23 | } |
23 | return *this; | 24 | return *this; |
24 | } | 25 | } |
25 | 26 | ||
26 | binary_t& binary_t::from_data(const void *d,size_t s) { | 27 | binary_t& binary_t::from_data(const void *d,size_t s) { |
27 | resize(s); | 28 | resize(s); |
28 | std::copy((const unsigned char*)d,(const unsigned char *)d+s, | 29 | std::copy((const unsigned char*)d,(const unsigned char *)d+s, |
29 | begin() ); | 30 | begin() ); |
30 | return *this; | 31 | return *this; |
31 | } | 32 | } |
32 | 33 | ||
33 | std::string binary_t::hex() const { | 34 | std::string binary_t::hex() const { |
34 | std::string rv; | 35 | std::string rv; |
35 | rv.reserve((size()<<1)+1); | 36 | rv.reserve((size()<<1)+1); |
36 | char t[3] = {0,0,0}; | 37 | char t[3] = {0,0,0}; |
37 | for(const_iterator i=begin(),ie=end();i!=ie;++i) { | 38 | for(const_iterator i=begin(),ie=end();i!=ie;++i) { |
38 | int rc = snprintf(t,sizeof(t),"%02x",*i); | 39 | int rc = snprintf(t,sizeof(t),"%02x",*i); |
39 | assert(rc<sizeof(t)); | 40 | assert(rc<sizeof(t)); |
40 | rv += t; | 41 | rv += t; |
41 | } | 42 | } |
42 | return rv; | 43 | return rv; |
43 | } | 44 | } |
44 | 45 | ||
45 | binary_t binary_t::md5() const { | 46 | binary_t binary_t::md5() const { |
46 | binary_t rv(MD5_DIGEST_LENGTH); | 47 | binary_t rv(MD5_DIGEST_LENGTH); |
47 | if(!MD5( | 48 | if(!MD5( |
48 | (const unsigned char*)&(front()),size(), | 49 | (const unsigned char*)&(front()),size(), |
49 | (unsigned char*)&(rv.front()) )) | 50 | (unsigned char*)&(rv.front()) )) |
50 | throw std::runtime_error("failed to md5()"); | 51 | throw std::runtime_error("failed to md5()"); |
51 | return rv; | 52 | return rv; |
52 | } | 53 | } |
53 | 54 | ||
55 | static void make_path_for_template(const std::string& p,mode_t m) { | ||
56 | struct stat st; | ||
57 | std::string pp; | ||
58 | for(std::string::size_type sl=p.find('/',1); | ||
59 | sl!=std::string::npos; | ||
60 | sl=p.find('/',sl+1)) { | ||
61 | if(stat( (pp=p.substr(0,sl)).c_str() ,&st) | ||
62 | || !S_ISDIR(st.st_mode)) { | ||
63 | if(mkdir(pp.c_str(),m)) | ||
64 | throw std::runtime_error("failed to mkdir()"); | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
54 | tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { | 69 | tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { |
70 | make_path_for_template(dt,0777); | ||
55 | if(!mkdtemp((char*)dir.data())) | 71 | if(!mkdtemp((char*)dir.data())) |
56 | throw std::runtime_error("failed to mkdtmp()"); | 72 | throw std::runtime_error("failed to mkdtmp()"); |
57 | } | 73 | } |
58 | tmpdir_t::~tmpdir_t() { | 74 | tmpdir_t::~tmpdir_t() { |
59 | assert(!dir.empty()); | 75 | assert(!dir.empty()); |
60 | if(rmdir(dir.c_str())) { | 76 | if(rmdir(dir.c_str())) { |
61 | syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); | 77 | syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); |
62 | } | 78 | } |
63 | } | 79 | } |
64 | 80 | ||
65 | std::string tmpdir_t::get_file(const std::string& f) { | 81 | std::string tmpdir_t::get_file(const std::string& f) { |
66 | std::string::size_type ls = f.rfind('/'); | 82 | std::string::size_type ls = f.rfind('/'); |
67 | return dir+'/'+( | 83 | return dir+'/'+( |
68 | (ls==std::string::npos) | 84 | (ls==std::string::npos) |
69 | ? f | 85 | ? f |
70 | : f.substr(ls+1) | 86 | : f.substr(ls+1) |
71 | ); | 87 | ); |
72 | } | 88 | } |
73 | 89 | ||
74 | tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) { | 90 | tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) { |
75 | if(!a) throw std::runtime_error("failed to archive_read_new()"); | 91 | if(!a) throw std::runtime_error("failed to archive_read_new()"); |
76 | if(archive_read_support_format_tar(a)) { | 92 | if(archive_read_support_format_tar(a)) { |
77 | archive_read_finish(a); | 93 | archive_read_finish(a); |
78 | throw std::runtime_error("failed to archive_read_support_format_tar()"); | 94 | throw std::runtime_error("failed to archive_read_support_format_tar()"); |
79 | } | 95 | } |
80 | if(archive_read_open_memory(a,p,s)) { | 96 | if(archive_read_open_memory(a,p,s)) { |
81 | archive_read_finish(a); | 97 | archive_read_finish(a); |
82 | throw std::runtime_error("failed to archive_read_open_memory()"); | 98 | throw std::runtime_error("failed to archive_read_open_memory()"); |
83 | } | 99 | } |
84 | } | 100 | } |
85 | tarchive_t::~tarchive_t() { | 101 | tarchive_t::~tarchive_t() { |
86 | assert(a); | 102 | assert(a); |
87 | archive_read_finish(a); | 103 | archive_read_finish(a); |
88 | } | 104 | } |
89 | 105 | ||
90 | bool tarchive_t::read_next_header() { | 106 | bool tarchive_t::read_next_header() { |
91 | assert(a); | 107 | assert(a); |
92 | return archive_read_next_header(a,&e)==ARCHIVE_OK; | 108 | return archive_read_next_header(a,&e)==ARCHIVE_OK; |
93 | } | 109 | } |
94 | 110 | ||
95 | std::string tarchive_t::entry_pathname() { | 111 | std::string tarchive_t::entry_pathname() { |
96 | assert(a); assert(e); | 112 | assert(a); assert(e); |
97 | return archive_entry_pathname(e); | 113 | return archive_entry_pathname(e); |
98 | } | 114 | } |
99 | 115 | ||
100 | bool tarchive_t::read_data_into_fd(int fd) { | 116 | bool tarchive_t::read_data_into_fd(int fd) { |
101 | assert(a); | 117 | assert(a); |
102 | return archive_read_data_into_fd(a,fd)==ARCHIVE_OK; | 118 | return archive_read_data_into_fd(a,fd)==ARCHIVE_OK; |
103 | } | 119 | } |