-rw-r--r-- | src/eyefiservice.cc | 50 |
1 files changed, 32 insertions, 18 deletions
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc index 291d9fa..49521e0 100644 --- a/src/eyefiservice.cc +++ b/src/eyefiservice.cc | |||
@@ -138,7 +138,7 @@ int eyefiService::UploadPhoto( | |||
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) { |
@@ -172,40 +172,54 @@ int eyefiService::UploadPhoto( | |||
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 | 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())); | ||
181 | close(fd); | 190 | close(fd); |
182 | } | 191 | } |
183 | } | 192 | } |
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 | } | 214 | } |
202 | } | 215 | } |
203 | } | ||
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()); |