author | Michael Krelin <hacker@klever.net> | 2009-04-04 23:28:48 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2009-04-05 00:40:46 (UTC) |
commit | 6b71fd1e4edd46b7caf47135740c961d5d4f051c (patch) (unidiff) | |
tree | 03dbdfb8fdbdf1c823af1a5bd32611d736f5ddf1 /src/eyetil.cc | |
parent | aadaa8b5d7eda23e72dbded9d6437b40358353f3 (diff) | |
download | iii-6b71fd1e4edd46b7caf47135740c961d5d4f051c.zip iii-6b71fd1e4edd46b7caf47135740c961d5d4f051c.tar.gz iii-6b71fd1e4edd46b7caf47135740c961d5d4f051c.tar.bz2 |
make upload directory if it doesn't exist
Signed-off-by: Michael Krelin <hacker@klever.net>
-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,9 +1,10 @@ | |||
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) { |
@@ -46,17 +47,32 @@ 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 | } |