author | Michael Krelin <hacker@klever.net> | 2013-01-29 18:28:58 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2013-02-02 12:42:52 (UTC) |
commit | 8cef826610f171b25a7a4aa3a764b1fb04c24d2f (patch) (unidiff) | |
tree | 0f4cd74156e998931955c9117fb263a37ba1f98c | |
parent | 7e4f9ebf4a76de3adafba6b32620d4610341897b (diff) | |
download | iii-8cef826610f171b25a7a4aa3a764b1fb04c24d2f.zip iii-8cef826610f171b25a7a4aa3a764b1fb04c24d2f.tar.gz iii-8cef826610f171b25a7a4aa3a764b1fb04c24d2f.tar.bz2 |
added persistent store for photo status
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | configure.ac | 3 | ||||
-rw-r--r-- | src/Makefile.am | 7 | ||||
-rw-r--r-- | src/eyefiservice.cc | 77 | ||||
-rw-r--r-- | src/eyefiworker.cc | 6 | ||||
-rw-r--r-- | src/iiidb.h | 28 |
5 files changed, 114 insertions, 7 deletions
diff --git a/configure.ac b/configure.ac index 1e0e42c..3b409fc 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -22,4 +22,7 @@ if $have_uuid ; then | |||
22 | fi | 22 | fi |
23 | 23 | ||
24 | PKG_CHECK_MODULES([SQLITE],[sqlite3],[have_sqlite=true],[have_sqlite=false]) | ||
25 | $have_sqlite && AC_DEFINE([HAVE_SQLITE],,[defined in presence of libsqlite3]) | ||
26 | |||
24 | AC_PATH_PROG([SOAPCPP2],[soapcpp2],[false]) | 27 | AC_PATH_PROG([SOAPCPP2],[soapcpp2],[false]) |
25 | test "$SOAPCPP2" = "false" && AC_MSG_ERROR([no soapcpp2 tool, part of gsoap package, found.]) | 28 | test "$SOAPCPP2" = "false" && AC_MSG_ERROR([no soapcpp2 tool, part of gsoap package, found.]) |
diff --git a/src/Makefile.am b/src/Makefile.am index b31bed5..f2ff772 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
@@ -4,10 +4,11 @@ noinst_HEADERS = \ | |||
4 | eyefi.h \ | 4 | eyefi.h \ |
5 | eyekinfig.h eyetil.h \ | 5 | eyekinfig.h eyetil.h \ |
6 | eyefiworker.h | 6 | eyefiworker.h iiidb.h \ |
7 | seclude.h | ||
7 | 8 | ||
8 | AM_CPPFLAGS = ${CPPFLAGS_DEBUG} \ | 9 | AM_CPPFLAGS = ${CPPFLAGS_DEBUG} \ |
9 | -DEYEKIN_CONF_DIR=\"${sysconfdir}/${PACKAGE}\" | 10 | -DEYEKIN_CONF_DIR=\"${sysconfdir}/${PACKAGE}\" |
10 | DEFAULT_INCLUDES = -I${top_builddir} -I${builddir} -I${srcdir} | 11 | DEFAULT_INCLUDES = -I${top_builddir} -I${builddir} -I${srcdir} |
11 | INCLUDES = ${MODULES_CFLAGS} ${UUID_CFLAGS} | 12 | INCLUDES = ${MODULES_CFLAGS} ${UUID_CFLAGS} ${SQLITE_CFLAGS} |
12 | 13 | ||
13 | iiid_SOURCES = iiid.cc \ | 14 | iiid_SOURCES = iiid.cc \ |
@@ -17,5 +18,5 @@ nodist_iiid_SOURCES = \ | |||
17 | ${builddir}/soapC.cpp ${builddir}/soapeyefiService.cpp \ | 18 | ${builddir}/soapC.cpp ${builddir}/soapeyefiService.cpp \ |
18 | COPYING.cc | 19 | COPYING.cc |
19 | iiid_LDADD = ${MODULES_LIBS} ${UUID_LIBS} | 20 | iiid_LDADD = ${MODULES_LIBS} ${UUID_LIBS} ${SQLITE_LIBS} |
20 | 21 | ||
21 | iii_extract_riff_chunk_SOURCES = iii-extract-riff-chunk.cc | 22 | iii_extract_riff_chunk_SOURCES = iii-extract-riff-chunk.cc |
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc index 93bbcca..4a4a179 100644 --- a/src/eyefiservice.cc +++ b/src/eyefiservice.cc | |||
@@ -11,6 +11,23 @@ | |||
11 | #include "eyetil.h" | 11 | #include "eyetil.h" |
12 | #include "soapeyefiService.h" | 12 | #include "soapeyefiService.h" |
13 | #ifdef HAVE_SQLITE | ||
14 | # include "iiidb.h" | ||
15 | #endif | ||
13 | 16 | ||
14 | static binary_t session_nonce; | 17 | static binary_t session_nonce; |
18 | #ifdef HAVE_SQLITE | ||
19 | static struct { | ||
20 | std::string filesignature; | ||
21 | long filesize; | ||
22 | std::string filename; | ||
23 | inline void reset() { filesignature.erase(); filename.erase(); filesize=0; } | ||
24 | inline void set(const std::string n,const std::string sig,long siz) { | ||
25 | filename = n; filesignature = sig; filesize = siz; | ||
26 | } | ||
27 | inline bool is(const std::string n,const std::string sig,long siz) { | ||
28 | return filesize==siz && filename==n && filesignature==sig; | ||
29 | } | ||
30 | } already; | ||
31 | #endif /* HAVE_SQLITE */ | ||
15 | 32 | ||
16 | static bool detached_child() { | 33 | static bool detached_child() { |
@@ -77,5 +94,6 @@ int eyefiService::GetPhotoStatus( | |||
77 | session_nonce.hex().c_str() ); | 94 | session_nonce.hex().c_str() ); |
78 | 95 | ||
79 | std::string computed_credential = binary_t(macaddress+eyekinfig_t(macaddress).get_upload_key()+session_nonce.hex()).md5().hex(); | 96 | eyekinfig_t eyekinfig(macaddress); |
97 | std::string computed_credential = binary_t(macaddress+eyekinfig.get_upload_key()+session_nonce.hex()).md5().hex(); | ||
80 | 98 | ||
81 | #ifndef NDEBUG | 99 | #ifndef NDEBUG |
@@ -85,5 +103,23 @@ int eyefiService::GetPhotoStatus( | |||
85 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); | 103 | if (credential != computed_credential) throw std::runtime_error("card authentication failed"); |
86 | 104 | ||
87 | r.fileid = 1; r.offset = 0; | 105 | #ifdef HAVE_SQLITE |
106 | iiidb_t D(eyekinfig); | ||
107 | seclude::stmt_t S = D.prepare( | ||
108 | "SELECT fileid FROM photo" | ||
109 | " WHERE mac=:mac AND filename=:filename" | ||
110 | " AND filesize=:filesize AND filesignature=:filesignature" | ||
111 | ).bind(":mac",macaddress) | ||
112 | .bind(":filename",filename).bind(":filesize",filesize) | ||
113 | .bind(":filesignature",filesignature); | ||
114 | if(!S.step()) { | ||
115 | r.fileid = 1; r.offset = 0; | ||
116 | }else{ | ||
117 | r.fileid = S.column<long>(0); | ||
118 | r.offset = filesize; | ||
119 | already.set(filename,filesignature,filesize); | ||
120 | } | ||
121 | #else /* HAVE_SQLITE */ | ||
122 | r.fileid=1, r.offset=0; | ||
123 | #endif /* HAVE_SQLITE */ | ||
88 | return SOAP_OK; | 124 | return SOAP_OK; |
89 | }catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } | 125 | }catch(const std::exception& e) { return E(this,"GetPhotoStatus",e); } |
@@ -135,4 +171,7 @@ int eyefiService::UploadPhoto( | |||
135 | std::string tf,lf; | 171 | std::string tf,lf; |
136 | binary_t digest, idigest; | 172 | binary_t digest, idigest; |
173 | #ifdef HAVE_SQLITE | ||
174 | bool beenthere = false; | ||
175 | #endif | ||
137 | 176 | ||
138 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { | 177 | for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { |
@@ -165,4 +204,11 @@ int eyefiService::UploadPhoto( | |||
165 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); | 204 | syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); |
166 | #endif | 205 | #endif |
206 | #ifdef HAVE_SQLITE | ||
207 | if(!(*i).size) { | ||
208 | if(!already.is(filename,filesignature,filesize)) | ||
209 | throw std::runtime_error("got zero-length upload for unknown file"); | ||
210 | beenthere = true; continue; | ||
211 | } | ||
212 | #endif | ||
167 | 213 | ||
168 | tarchive_t a((*i).ptr,(*i).size); | 214 | tarchive_t a((*i).ptr,(*i).size); |
@@ -182,4 +228,11 @@ int eyefiService::UploadPhoto( | |||
182 | } | 228 | } |
183 | 229 | ||
230 | #ifdef HAVE_SQLITE | ||
231 | if(beenthere) { | ||
232 | r.success=true; | ||
233 | return SOAP_OK; | ||
234 | } | ||
235 | #endif | ||
236 | |||
184 | if(tf.empty()) throw std::runtime_error("haven't seen THE file"); | 237 | if(tf.empty()) throw std::runtime_error("haven't seen THE file"); |
185 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); | 238 | if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); |
@@ -204,6 +257,22 @@ int eyefiService::UploadPhoto( | |||
204 | } | 257 | } |
205 | std::string cmd = eyekinfig.get_on_upload_photo(); | 258 | std::string cmd = eyekinfig.get_on_upload_photo(); |
206 | if(success && !cmd.empty()) { | 259 | if(success) { |
207 | if(detached_child()) { | 260 | #ifdef HAVE_SQLITE |
261 | { | ||
262 | iiidb_t D(eyekinfig); | ||
263 | D.prepare( | ||
264 | "INSERT INTO photo" | ||
265 | " (ctime,mac,fileid,filename,filesize,filesignature,encryption,flags)" | ||
266 | " VALUES" | ||
267 | " (:ctime,:mac,:fileid,:filename,:filesize,:filesignature,:encryption,:flags)" | ||
268 | ).bind(":ctime",time(0)) | ||
269 | .bind(":mac",macaddress) | ||
270 | .bind(":fileid",fileid).bind(":filename",filename) | ||
271 | .bind(":filesize",filesize).bind(":filesignature",filesignature) | ||
272 | .bind(":encryption",encryption).bind(":flags",flags) | ||
273 | .step(); | ||
274 | } | ||
275 | #endif /* HAVE_SQLITE */ | ||
276 | if((!cmd.empty()) && detached_child()) { | ||
208 | putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); | 277 | putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); |
209 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); | 278 | putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); |
diff --git a/src/eyefiworker.cc b/src/eyefiworker.cc index c051482..9dcd048 100644 --- a/src/eyefiworker.cc +++ b/src/eyefiworker.cc | |||
@@ -2,4 +2,7 @@ | |||
2 | #include <stdexcept> | 2 | #include <stdexcept> |
3 | #include "eyefiworker.h" | 3 | #include "eyefiworker.h" |
4 | #ifdef HAVE_SQLITE | ||
5 | # include "sqlite3.h" | ||
6 | #endif | ||
4 | 7 | ||
5 | eyefiworker::eyefiworker() | 8 | eyefiworker::eyefiworker() |
@@ -18,4 +21,7 @@ eyefiworker::eyefiworker() | |||
18 | 21 | ||
19 | int eyefiworker::run(int bindport) { | 22 | int eyefiworker::run(int bindport) { |
23 | #ifdef HAVE_SQLITE | ||
24 | sqlite3_initialize(); | ||
25 | #endif | ||
20 | if(!soap_valid_socket(bind(0,bindport,64))) | 26 | if(!soap_valid_socket(bind(0,bindport,64))) |
21 | throw std::runtime_error("failed to bind()"); | 27 | throw std::runtime_error("failed to bind()"); |
diff --git a/src/iiidb.h b/src/iiidb.h new file mode 100644 index 0000000..e77fa09 --- a/dev/null +++ b/src/iiidb.h | |||
@@ -0,0 +1,28 @@ | |||
1 | #ifndef __IIIDB_H | ||
2 | #define __IIIDB_H | ||
3 | |||
4 | #include <autosprintf.h> | ||
5 | #include "seclude.h" | ||
6 | #include "eyekinfig.h" | ||
7 | |||
8 | struct iiidb_t : public seclude::db_t { | ||
9 | iiidb_t(eyekinfig_t& k) : seclude::db_t(gnu::autosprintf("%s/.iii.db",k.get_targetdir().c_str())) { | ||
10 | try { | ||
11 | exec("SELECT 1 FROM photo LIMIT 0"); | ||
12 | }catch(const seclude::sqlite3_error& e) { | ||
13 | exec( "CREATE TABLE photo (" | ||
14 | " id integer PRIMARY KEY AUTOINCREMENT," | ||
15 | " ctime integer NOT NULL," | ||
16 | " mac text NOT NULL," | ||
17 | " fileid integer NOT NULL," | ||
18 | " filename text NOT NULL," | ||
19 | " filesize integer NOT NULL," | ||
20 | " filesignature text NOT NULL UNIQUE," | ||
21 | " encryption text NOT NULL," | ||
22 | " flags integer NOT NULL" | ||
23 | ")" ); | ||
24 | } | ||
25 | } | ||
26 | }; | ||
27 | |||
28 | #endif /* __IIIDB_H */ | ||