summaryrefslogtreecommitdiffabout
path: root/src
authorMichael Krelin <hacker@klever.net>2009-04-04 23:28:48 (UTC)
committer Michael Krelin <hacker@klever.net>2009-04-05 00:40:46 (UTC)
commit6b71fd1e4edd46b7caf47135740c961d5d4f051c (patch) (unidiff)
tree03dbdfb8fdbdf1c823af1a5bd32611d736f5ddf1 /src
parentaadaa8b5d7eda23e72dbded9d6437b40358353f3 (diff)
downloadiii-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>
Diffstat (limited to 'src') (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyefiservice.cc1
-rw-r--r--src/eyetil.cc16
2 files changed, 16 insertions, 1 deletions
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc
index 30c06fa..97cb33b 100644
--- a/src/eyefiservice.cc
+++ b/src/eyefiservice.cc
@@ -75,97 +75,96 @@ int eyefiService::GetPhotoStatus(
75#ifndef NDEBUG 75#ifndef NDEBUG
76 syslog(LOG_DEBUG, 76 syslog(LOG_DEBUG,
77 "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s", 77 "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s",
78 macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str() ); 78 macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str() );
79#endif 79#endif
80 r.fileid = 1; r.offset = 0; 80 r.fileid = 1; r.offset = 0;
81 return SOAP_OK; 81 return SOAP_OK;
82} 82}
83 83
84int eyefiService::MarkLastPhotoInRoll( 84int eyefiService::MarkLastPhotoInRoll(
85 std::string macaddress, int mergedelta, 85 std::string macaddress, int mergedelta,
86 struct rns__MarkLastPhotoInRollResponse &r ) { 86 struct rns__MarkLastPhotoInRollResponse &r ) {
87#ifndef NDEBUG 87#ifndef NDEBUG
88 syslog(LOG_DEBUG, 88 syslog(LOG_DEBUG,
89 "MarkLastPhotoInRoll request from %s with mergedelta=%d", 89 "MarkLastPhotoInRoll request from %s with mergedelta=%d",
90 macaddress.c_str(), mergedelta ); 90 macaddress.c_str(), mergedelta );
91#endif 91#endif
92 std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); 92 std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll();
93 if(!cmd.empty()) { 93 if(!cmd.empty()) {
94 if(detached_child()) { 94 if(detached_child()) {
95 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); 95 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) );
96 putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); 96 putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) );
97 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; 97 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 };
98 execv("/bin/sh",argv); 98 execv("/bin/sh",argv);
99 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); 99 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str());
100 _exit(-1); 100 _exit(-1);
101 } 101 }
102 } 102 }
103 return SOAP_OK; 103 return SOAP_OK;
104} 104}
105 105
106int eyefiService::UploadPhoto( 106int eyefiService::UploadPhoto(
107 int fileid, std::string macaddress, 107 int fileid, std::string macaddress,
108 std::string filename, long filesize, std::string filesignature, 108 std::string filename, long filesize, std::string filesignature,
109 std::string encryption, int flags, 109 std::string encryption, int flags,
110 struct rns__UploadPhotoResponse& r ) { 110 struct rns__UploadPhotoResponse& r ) {
111#ifndef NDEBUG 111#ifndef NDEBUG
112 syslog(LOG_DEBUG, 112 syslog(LOG_DEBUG,
113 "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," 113 "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld,"
114 " filesignature=%s, encryption=%s, flags=%04X", 114 " filesignature=%s, encryption=%s, flags=%04X",
115 macaddress.c_str(), fileid, filename.c_str(), filesize, 115 macaddress.c_str(), fileid, filename.c_str(), filesize,
116 filesignature.c_str(), encryption.c_str(), flags ); 116 filesignature.c_str(), encryption.c_str(), flags );
117#endif 117#endif
118 eyekinfig_t eyekinfig(macaddress); 118 eyekinfig_t eyekinfig(macaddress);
119 119
120 umask(eyekinfig.get_umask()); 120 umask(eyekinfig.get_umask());
121 121
122 std::string td = eyekinfig.get_targetdir(); 122 std::string td = eyekinfig.get_targetdir();
123 /* TODO: try to create, if needed */
124 tmpdir_t indir(td+"/.incoming.XXXXXX"); 123 tmpdir_t indir(td+"/.incoming.XXXXXX");
125 124
126 for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { 125 for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) {
127#ifndef NDEBUG 126#ifndef NDEBUG
128 syslog(LOG_DEBUG, 127 syslog(LOG_DEBUG,
129 " MIME attachment with id=%s, type=%s, size=%ld", 128 " MIME attachment with id=%s, type=%s, size=%ld",
130 (*i).id, (*i).type, (long)(*i).size ); 129 (*i).id, (*i).type, (long)(*i).size );
131#endif 130#endif
132 131
133#ifndef NDEBUG 132#ifndef NDEBUG
134 if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { 133 if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) {
135 std::string idigest((*i).ptr,(*i).size); 134 std::string idigest((*i).ptr,(*i).size);
136 syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigest.c_str()); 135 syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigest.c_str());
137 } 136 }
138#endif 137#endif
139 if( (*i).id && !strcmp((*i).id,"FILENAME") ) { 138 if( (*i).id && !strcmp((*i).id,"FILENAME") ) {
140 assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); 139 assert( (*i).type && !strcmp((*i).type,"application/x-tar") );
141#ifdef III_SAVE_TARS 140#ifdef III_SAVE_TARS
142 std::string tarfile = indir.get_file(filename); 141 std::string tarfile = indir.get_file(filename);
143 { 142 {
144 std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); 143 std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size);
145 } 144 }
146#endif 145#endif
147 tarchive_t a((*i).ptr,(*i).size); 146 tarchive_t a((*i).ptr,(*i).size);
148 if(!a.read_next_header()) 147 if(!a.read_next_header())
149 throw std::runtime_error("failed to tarchive_t::read_next_header())"); 148 throw std::runtime_error("failed to tarchive_t::read_next_header())");
150 std::string jf = indir.get_file(a.entry_pathname()); 149 std::string jf = indir.get_file(a.entry_pathname());
151 std::string::size_type ls = jf.rfind('/'); 150 std::string::size_type ls = jf.rfind('/');
152 std::string jbn = (ls==std::string::npos)?jf:jf.substr(ls+1); 151 std::string jbn = (ls==std::string::npos)?jf:jf.substr(ls+1);
153 int fd=open(jf.c_str(),O_CREAT|O_WRONLY,0666); 152 int fd=open(jf.c_str(),O_CREAT|O_WRONLY,0666);
154 assert(fd>0); 153 assert(fd>0);
155 a.read_data_into_fd(fd); 154 a.read_data_into_fd(fd);
156 close(fd); 155 close(fd);
157 std::string tf = td+'/'+jbn; 156 std::string tf = td+'/'+jbn;
158 bool success = false; 157 bool success = false;
159 if(!link(jf.c_str(), tf.c_str())) { 158 if(!link(jf.c_str(), tf.c_str())) {
160 unlink(jf.c_str()); success = true; 159 unlink(jf.c_str()); success = true;
161 }else{ 160 }else{
162 for(int i=1;i<32767;++i) { 161 for(int i=1;i<32767;++i) {
163 tf = (const char*)gnu::autosprintf( "%s/(%05d)%s", 162 tf = (const char*)gnu::autosprintf( "%s/(%05d)%s",
164 td.c_str(), i, jbn.c_str() ); 163 td.c_str(), i, jbn.c_str() );
165 if(!link(jf.c_str(), tf.c_str())) { 164 if(!link(jf.c_str(), tf.c_str())) {
166 unlink(jf.c_str()); success = true; 165 unlink(jf.c_str()); success = true;
167 break; 166 break;
168 } 167 }
169 } 168 }
170 } 169 }
171 std::string cmd = eyekinfig.get_on_upload_photo(); 170 std::string cmd = eyekinfig.get_on_upload_photo();
diff --git a/src/eyetil.cc b/src/eyetil.cc
index d00c2ee..2fbd687 100644
--- a/src/eyetil.cc
+++ b/src/eyetil.cc
@@ -1,102 +1,118 @@
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
9binary_t& binary_t::from_hex(const std::string& h) { 10binary_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
26binary_t& binary_t::from_data(const void *d,size_t s) { 27binary_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
33std::string binary_t::hex() const { 34std::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
45binary_t binary_t::md5() const { 46binary_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
55static 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
54tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { 69tmpdir_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}
58tmpdir_t::~tmpdir_t() { 74tmpdir_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
65std::string tmpdir_t::get_file(const std::string& f) { 81std::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
74tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) { 90tarchive_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}
85tarchive_t::~tarchive_t() { 101tarchive_t::~tarchive_t() {
86 assert(a); 102 assert(a);
87 archive_read_finish(a); 103 archive_read_finish(a);
88} 104}
89 105
90bool tarchive_t::read_next_header() { 106bool 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
95std::string tarchive_t::entry_pathname() { 111std::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
100bool tarchive_t::read_data_into_fd(int fd) { 116bool 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;