summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2011-03-17 22:15:09 (UTC)
committer Michael Krelin <hacker@klever.net>2011-03-19 16:26:43 (UTC)
commit1d47a62116c7e238b950bdafa636e4cc5d3ea9a5 (patch) (unidiff)
treee77bd2d8bb3beed754e1ccccb90ce6c370ae3365
parent38252309fa3b72aa121f9fe7016c3a7138ed93cb (diff)
downloadiii-1d47a62116c7e238b950bdafa636e4cc5d3ea9a5.zip
iii-1d47a62116c7e238b950bdafa636e4cc5d3ea9a5.tar.gz
iii-1d47a62116c7e238b950bdafa636e4cc5d3ea9a5.tar.bz2
extract and store both .JPG and .log files
Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyefiservice.cc54
1 files changed, 34 insertions, 20 deletions
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc
index 291d9fa..49521e0 100644
--- a/src/eyefiservice.cc
+++ b/src/eyefiservice.cc
@@ -135,13 +135,13 @@ int eyefiService::UploadPhoto(
135 135
136 umask(eyekinfig.get_umask()); 136 umask(eyekinfig.get_umask());
137 137
138 std::string td = eyekinfig.get_targetdir(); 138 std::string td = eyekinfig.get_targetdir();
139 tmpdir_t indir(td+"/.incoming.XXXXXX"); 139 tmpdir_t indir(td+"/.incoming.XXXXXX");
140 140
141 std::string jf; 141 std::string jf,lf;
142 binary_t digest, idigest; 142 binary_t digest, idigest;
143 143
144 for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { 144 for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) {
145#ifndef NDEBUG 145#ifndef NDEBUG
146 syslog(LOG_DEBUG, 146 syslog(LOG_DEBUG,
147 " MIME attachment with id=%s, type=%s, size=%ld", 147 " MIME attachment with id=%s, type=%s, size=%ld",
@@ -169,46 +169,60 @@ int eyefiService::UploadPhoto(
169 digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); 169 digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key());
170#ifndef NDEBUG 170#ifndef NDEBUG
171 syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); 171 syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str());
172#endif 172#endif
173 173
174 tarchive_t a((*i).ptr,(*i).size); 174 tarchive_t a((*i).ptr,(*i).size);
175 if(!a.read_next_header()) 175 while(a.read_next_header()) {
176 throw std::runtime_error("failed to tarchive_t::read_next_header())"); 176 std::string f = indir.get_file(a.entry_pathname());
177 jf = indir.get_file(a.entry_pathname()); 177 std::string::size_type fl = f.length();
178 int fd=open(jf.c_str(),O_CREAT|O_WRONLY,0666); 178 if(fl<4) continue;
179 assert(fd>0); 179 const char *s = f.c_str()+fl-4;
180 a.read_data_into_fd(fd); 180 if(!strcasecmp(s,".JPG"))
181 close(fd); 181 jf = f;
182 else if(!strcasecmp(s,".log"))
183 lf = f;
184 else continue;
185 int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666);
186 if(fd<0)
187 throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str()));
188 if(!a.read_data_into_fd(fd))
189 throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str()));
190 close(fd);
191 }
182 } 192 }
183 } 193 }
184 194
185 if(jf.empty()) throw std::runtime_error("haven't seen jpeg file"); 195 if(jf.empty()) throw std::runtime_error("haven't seen jpeg file");
186 if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); 196 if(digest!=idigest) throw std::runtime_error("integrity digest verification failed");
187 197
188 std::string::size_type ls = jf.rfind('/'); 198 std::string::size_type ls = jf.rfind('/');
199 // XXX: actually, lack of '/' signifies error here
189 std::string jbn = (ls==std::string::npos)?jf:jf.substr(ls+1); 200 std::string jbn = (ls==std::string::npos)?jf:jf.substr(ls+1);
190 std::string tf = td+'/'+jbn; 201 ls = lf.rfind('/');
202 std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1);
203 std::string tjf,tlf;
191 bool success = false; 204 bool success = false;
192 if(!link(jf.c_str(), tf.c_str())) { 205 for(int i=0;i<32767;++i) {
193 unlink(jf.c_str()); success = true; 206 const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s";
194 }else{ 207 tjf = (const char*)gnu::autosprintf(fmt,td.c_str(),jbn.c_str(),i);
195 for(int i=1;i<32767;++i) { 208 if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i);
196 tf = (const char*)gnu::autosprintf( "%s/(%05d)%s", 209 if( (!link(jf.c_str(),tjf.c_str())) && (lf.empty()) || !link(lf.c_str(),tlf.c_str()) ) {
197 td.c_str(), i, jbn.c_str() ); 210 unlink(jf.c_str());
198 if(!link(jf.c_str(), tf.c_str())) { 211 if(!lf.empty()) unlink(lf.c_str());
199 unlink(jf.c_str()); success = true; 212 success=true;
200 break; 213 break;
201 }
202 } 214 }
203 } 215 }
204 std::string cmd = eyekinfig.get_on_upload_photo(); 216 std::string cmd = eyekinfig.get_on_upload_photo();
205 if(success && !cmd.empty()) { 217 if(success && !cmd.empty()) {
206 if(detached_child()) { 218 if(detached_child()) {
219 putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",jbn.c_str()) );
207 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); 220 putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) );
208 putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",tf.c_str()) ); 221 putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",tjf.c_str()) );
222 if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) );
209 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; 223 char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 };
210 execv("/bin/sh",argv); 224 execv("/bin/sh",argv);
211 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); 225 syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str());
212 _exit(-1); 226 _exit(-1);
213 } 227 }
214 } 228 }