database.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. ** Copyright (C) 2005-2011 Erik de Castro Lopo
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "config.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #if HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #else
  24. #include "sf_unistd.h"
  25. #endif
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #ifdef HAVE_DIRECT_H
  29. #include <direct.h>
  30. #endif
  31. #include <sys/stat.h>
  32. #include <sndfile.h>
  33. #include "regtest.h"
  34. #if HAVE_SQLITE3
  35. #include <ctype.h>
  36. #include <sqlite3.h>
  37. typedef struct
  38. { sqlite3 *sql ;
  39. int count ;
  40. int ekey_max ;
  41. /* Filename and pathname for file. */
  42. char filename [256] ;
  43. char pathname [512] ;
  44. /* Storage for createding SQL commands. Must be larger than logbuf below. */
  45. char cmdbuf [1 << 15] ;
  46. /* Storage for log buffer retrieved from SNDFILE* .*/
  47. char logbuf [1 << 14] ;
  48. } REGTEST_DB ;
  49. /* In checksum.c */
  50. int calc_checksum (SNDFILE * file, const SF_INFO * info) ;
  51. static void get_filename_pathname (REGTEST_DB * db, const char *filepath) ;
  52. static void single_quote_replace (char * buf) ;
  53. static int get_ekey_from_filename (REGTEST_DB * db, const char *filepath) ;
  54. static int get_filename_pathname_by_ekey (REGTEST_DB * db, int ekey) ;
  55. static int check_file_by_ekey (REGTEST_DB * db, int ekey) ;
  56. static int count_callback (REGTEST_DB * db, int argc, char **argv, char **colname) ;
  57. static int ekey_max_callback (REGTEST_DB * db, int argc, char **argv, char **colname) ;
  58. static int callback (void *unused, int argc, char **argv, char **colname) ;
  59. static const char *db_basename (const char *fname);
  60. /* Windows accepts both '\\' and '/' in paths */
  61. #ifdef _WIN32
  62. #define IS_SLASH(c) ((c) == '\\' || (c) == '/')
  63. #define HAS_DRIVELETTER(path) (isalpha ((int)(path[0])) && path[1] == ':' && IS_SLASH(path[2]))
  64. #else
  65. #define IS_SLASH(c) ((c) == '/')
  66. #define HAS_DRIVELETTER(path) 0
  67. #endif
  68. REG_DB *
  69. db_open (const char * db_name)
  70. { REGTEST_DB * db ;
  71. int err ;
  72. if ((db = malloc (sizeof (REGTEST_DB))) == NULL)
  73. { perror ("malloc") ;
  74. exit (1) ;
  75. } ;
  76. if ((err = sqlite3_open (db_name, &(db->sql))) != 0)
  77. { printf ("Can't open database: %s\n", sqlite3_errmsg (db->sql)) ;
  78. sqlite3_close (db->sql) ;
  79. free (db) ;
  80. exit (1) ;
  81. } ;
  82. return (REG_DB *) db ;
  83. } /* db_open */
  84. int
  85. db_create (const char * db_name)
  86. { REGTEST_DB * db ;
  87. const char *cmd ;
  88. char * errmsg = NULL ;
  89. int err ;
  90. db = (REGTEST_DB *) db_open (db_name) ;
  91. cmd = "create table sndfile (ekey INTEGER PRIMARY KEY,"
  92. "fname VARCHAR(1),"
  93. "fpath VARCHAR(1),"
  94. "srate INTEGER,"
  95. "frames VARCHAR(1),"
  96. "channels INTEGER,"
  97. "format VARCHAR(1),"
  98. "checksum VARCHAR(1),"
  99. "logbuf VARCHAR(1)"
  100. ");" ;
  101. err = sqlite3_exec (db->sql, cmd, callback, 0, &errmsg) ;
  102. if (err != SQLITE_OK)
  103. printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  104. sqlite3_close (db->sql) ;
  105. free (db) ;
  106. return 0 ;
  107. } /* db_create */
  108. int
  109. db_close (REG_DB * db_handle)
  110. { REGTEST_DB * db ;
  111. db = (REGTEST_DB *) db_handle ;
  112. sqlite3_close (db->sql) ;
  113. free (db) ;
  114. return 0 ;
  115. } /* db_close */
  116. /*==============================================================================
  117. */
  118. int
  119. db_file_exists (REG_DB * db_handle, const char * filename)
  120. { REGTEST_DB * db ;
  121. char * errmsg ;
  122. int err ;
  123. db = (REGTEST_DB *) db_handle ;
  124. filename = db_basename (filename);
  125. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select fname from sndfile where fname='%s'", filename) ;
  126. db->count = 0 ;
  127. err = sqlite3_exec (db->sql, db->cmdbuf, (sqlite3_callback) count_callback, db, &errmsg) ;
  128. if (err == 0 && db->count == 1)
  129. return 1 ;
  130. return 0 ;
  131. } /* db_file_exists */
  132. int
  133. db_add_file (REG_DB * db_handle, const char * filepath)
  134. { REGTEST_DB * db ;
  135. SNDFILE * sndfile ;
  136. SF_INFO info ;
  137. char * errmsg ;
  138. int err, checksum ;
  139. db = (REGTEST_DB *) db_handle ;
  140. get_filename_pathname (db, filepath) ;
  141. if (db_file_exists (db_handle, filepath))
  142. { printf (" %s : already in database\n", db->filename) ;
  143. return 0 ;
  144. } ;
  145. memset (&info, 0, sizeof (info)) ;
  146. sndfile = sf_open (db->pathname, SFM_READ, &info) ;
  147. sf_command (sndfile, SFC_GET_LOG_INFO, db->logbuf, sizeof (db->logbuf)) ;
  148. checksum = (sndfile == NULL) ? 0 : calc_checksum (sndfile, &info) ;
  149. sf_close (sndfile) ;
  150. if (sndfile == NULL)
  151. { printf (" %s : could not open : %s, filepath: '%s'\n", db->filename, sf_strerror (NULL), filepath) ;
  152. puts (db->logbuf) ;
  153. return 1 ;
  154. } ;
  155. single_quote_replace (db->logbuf) ;
  156. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "insert into sndfile "
  157. "(fname, fpath, srate, frames, channels, format, checksum, logbuf) values"
  158. "('%s','%s',%d,'%ld', %d, '0x%08x', '0x%08x', '%s');",
  159. db->filename, db->pathname, info.samplerate, (long) info.frames, info.channels, info.format, checksum, db->logbuf) ;
  160. if (strlen (db->cmdbuf) >= sizeof (db->cmdbuf) - 1)
  161. { printf ("strlen (db->cmdbuf) too long.\n") ;
  162. exit (1) ;
  163. } ;
  164. err = sqlite3_exec (db->sql, db->cmdbuf, callback, 0, &errmsg) ;
  165. if (err != SQLITE_OK)
  166. { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  167. puts (db->cmdbuf) ;
  168. } ;
  169. return 0 ;
  170. } /* db_add_file */
  171. int
  172. db_check_file (REG_DB * db_handle, const char * filepath)
  173. { REGTEST_DB * db ;
  174. int ekey ;
  175. if (db_file_exists (db_handle, filepath) == 0)
  176. { printf ("\nFile not in database.\n\n") ;
  177. exit (0) ;
  178. } ;
  179. db = (REGTEST_DB *) db_handle ;
  180. ekey = get_ekey_from_filename (db, filepath) ;
  181. return check_file_by_ekey (db, ekey) ;
  182. } /* db_check_file */
  183. /*==============================================================================
  184. */
  185. int
  186. db_check_all (REG_DB * db_handle)
  187. { REGTEST_DB * db ;
  188. char * errmsg ;
  189. int err, ekey ;
  190. db = (REGTEST_DB *) db_handle ;
  191. db->ekey_max = 0 ;
  192. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select ekey from sndfile") ;
  193. err = sqlite3_exec (db->sql, db->cmdbuf, (sqlite3_callback) ekey_max_callback, db, &errmsg) ;
  194. if (err != SQLITE_OK)
  195. { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  196. puts (db->cmdbuf) ;
  197. } ;
  198. for (ekey = 1 ; ekey <= db->ekey_max ; ekey++)
  199. if (get_filename_pathname_by_ekey (db, ekey) != 0)
  200. check_file_by_ekey (db, ekey) ;
  201. return 0 ;
  202. } /* db_check_all */
  203. int
  204. db_list_all (REG_DB * db_handle)
  205. {
  206. printf ("%s : %p\n", __func__, (void *) db_handle) ;
  207. return 0 ;
  208. } /* db_list_all */
  209. int
  210. db_del_entry (REG_DB * db_handle, const char * entry)
  211. {
  212. printf ("%s : %p %s\n", __func__, (void *) db_handle, entry) ;
  213. return 0 ;
  214. } /* db_del_entry */
  215. /*==============================================================================
  216. */
  217. static int
  218. get_ekey_from_filename (REGTEST_DB * db, const char *filepath)
  219. { char * errmsg, **result ;
  220. int err, ekey = 0, rows, cols ;
  221. get_filename_pathname (db, filepath) ;
  222. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select ekey from sndfile where fname='%s'", db->filename) ;
  223. err = sqlite3_get_table (db->sql, db->cmdbuf, &result, &rows, &cols, &errmsg) ;
  224. if (err != SQLITE_OK)
  225. { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  226. puts (db->cmdbuf) ;
  227. } ;
  228. if (cols != 1 || rows != 1)
  229. { printf ("Bad juju!! rows = %d cols = %d\n", rows, cols) ;
  230. exit (1) ;
  231. } ;
  232. ekey = strtol (result [1], NULL, 10) ;
  233. sqlite3_free_table (result) ;
  234. return ekey ;
  235. } /* get_ekey_from_filename */
  236. static int
  237. get_filename_pathname_by_ekey (REGTEST_DB * db, int ekey)
  238. { char *errmsg, **result ;
  239. int err, rows, cols ;
  240. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select fname,fpath from sndfile where ekey='%d'", ekey) ;
  241. err = sqlite3_get_table (db->sql, db->cmdbuf, &result, &rows, &cols, &errmsg) ;
  242. if (err != SQLITE_OK)
  243. { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  244. puts (db->cmdbuf) ;
  245. return 0 ;
  246. } ;
  247. if (cols != 2 || rows != 1)
  248. { printf ("\nError (%s %d) : rows = %d cols = %d\n", __func__, __LINE__, rows, cols) ;
  249. exit (1) ;
  250. } ;
  251. snprintf (db->filename, sizeof (db->filename), "%s", result [2]) ;
  252. snprintf (db->pathname, sizeof (db->pathname), "%s", result [3]) ;
  253. sqlite3_free_table (result) ;
  254. return 1 ;
  255. } /* get_filename_pathname_by_ekey */
  256. static int
  257. check_file_by_ekey (REGTEST_DB * db, int ekey)
  258. { SNDFILE * sndfile ;
  259. SF_INFO info ;
  260. char * errmsg, **result ;
  261. int err, k, rows, cols, checksum ;
  262. printf (" %s : ", db->filename) ;
  263. fflush (stdout) ;
  264. memset (&info, 0, sizeof (info)) ;
  265. sndfile = sf_open (db->pathname, SFM_READ, &info) ;
  266. sf_command (sndfile, SFC_GET_LOG_INFO, db->logbuf, sizeof (db->logbuf)) ;
  267. checksum = (sndfile == NULL) ? 0 : calc_checksum (sndfile, &info) ;
  268. sf_close (sndfile) ;
  269. if (sndfile == NULL)
  270. { printf ("\n\nError : Could not open '%s' : %s\n", db->pathname, sf_strerror (NULL)) ;
  271. puts (db->logbuf) ;
  272. exit (1) ;
  273. } ;
  274. single_quote_replace (db->logbuf) ;
  275. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select fname,srate,frames,channels,format,"
  276. "checksum,logbuf from sndfile where ekey='%d'", ekey) ;
  277. err = sqlite3_get_table (db->sql, db->cmdbuf, &result, &rows, &cols, &errmsg) ;
  278. if (err != SQLITE_OK)
  279. { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  280. puts (db->cmdbuf) ;
  281. } ;
  282. for (k = 0 ; k < cols ; k++)
  283. { if (strcmp (result [k], "fname") == 0)
  284. { if (strcmp (result [k + cols], db->filename) == 0)
  285. continue ;
  286. printf ("\n\nError : fname doesn't match : %s != %s\n", result [k + cols], db->filename) ;
  287. } ;
  288. if (strcmp (result [k], "srate") == 0)
  289. { if (strtol (result [k + cols], NULL, 10) == info.samplerate)
  290. continue ;
  291. printf ("\n\nError : srate doesn't match : %s == %d\n", result [k + cols], info.samplerate) ;
  292. } ;
  293. if (strcmp (result [k], "frames") == 0)
  294. { if (strtoll (result [k + cols], NULL, 10) == info.frames)
  295. continue ;
  296. printf ("\n\nError : frames doesn't match : %s == %ld\n", result [k + cols], (long) info.frames) ;
  297. } ;
  298. if (strcmp (result [k], "channels") == 0)
  299. { if (strtol (result [k + cols], NULL, 10) == info.channels)
  300. continue ;
  301. printf ("\n\nError : channels doesn't match : %s == %d\n", result [k + cols], info.channels) ;
  302. } ;
  303. if (strcmp (result [k], "format") == 0)
  304. { if (strtol (result [k + cols], NULL, 16) == info.format)
  305. continue ;
  306. printf ("\n\nError : format doesn't match : %s == 0x%08x\n", result [k + cols], info.format) ;
  307. } ;
  308. if (strcmp (result [k], "checksum") == 0)
  309. { int db_val = (int) strtoll (result [k + cols], NULL, 16) ;
  310. if (db_val == checksum)
  311. continue ;
  312. printf ("\n\nError : checksum doesn't match : 0x%08x == 0x%08x\n", db_val, checksum) ;
  313. } ;
  314. if (strcmp (result [k], "logbuf") == 0)
  315. continue ;
  316. printf ("\nHere is the old logubuffer :\n\n%s\n\nand the new :\n\n%s\n\n", result [2 * cols - 1], db->logbuf) ;
  317. exit (1) ;
  318. } ;
  319. sqlite3_free_table (result) ;
  320. puts ("ok") ;
  321. return 0 ;
  322. } /* check_file_by_ekey */
  323. /*==============================================================================
  324. */
  325. static void
  326. get_filename_pathname (REGTEST_DB * db, const char *filepath)
  327. {
  328. const char * basename = db_basename (filepath) ;
  329. size_t slen ;
  330. /* Test for a relative path
  331. */
  332. if (!IS_SLASH(filepath [0]) && !HAS_DRIVELETTER(filepath))
  333. { memset (db->pathname, 0, sizeof (db->pathname)) ;
  334. if (getcwd (db->pathname, sizeof (db->pathname)) == NULL)
  335. { perror ("\ngetcwd failed") ;
  336. exit (1) ;
  337. } ;
  338. slen = strlen (db->pathname) ;
  339. /* a '/' is fine for Windows too */
  340. snprintf (db->pathname + slen, sizeof (db->pathname) - slen, "/%s", filepath) ;
  341. }
  342. else
  343. snprintf (db->pathname, sizeof (db->pathname), "%s", filepath) ;
  344. snprintf (db->filename, sizeof (db->filename), "%s", basename) ;
  345. basename = db_basename (db->pathname) ;
  346. if (basename == db->pathname)
  347. { printf ("\nError : bad pathname %s\n", filepath) ;
  348. exit (1) ;
  349. } ;
  350. } /* get filename_pathname */
  351. static void
  352. single_quote_replace (char * buf)
  353. { while ((buf = strchr (buf, '\'')) != 0)
  354. buf [0] = '"' ;
  355. } /* single_quote_replace */
  356. static int
  357. count_callback (REGTEST_DB * db, int argc, char **argv, char **colname)
  358. { db->count ++ ;
  359. (void) argc ;
  360. (void) argv ;
  361. (void) colname ;
  362. return 0 ;
  363. } /* count_callback */
  364. static int
  365. ekey_max_callback (REGTEST_DB * db, int argc, char **argv, char **unused)
  366. { int ekey ;
  367. (void) argc ;
  368. (void) unused ;
  369. ekey = strtol (argv [0], NULL, 10) ;
  370. if (ekey > db->ekey_max)
  371. db->ekey_max = ekey ;
  372. return 0 ;
  373. } /* ekey_max_callback */
  374. static int
  375. callback (void *unused, int argc, char **argv, char **colname)
  376. { int k ;
  377. (void) unused ;
  378. for (k = 0 ; k < argc ; k++)
  379. printf ("%s = %s\n", colname [k], argv [k] ? argv [k] : "NULL") ;
  380. printf ("\n") ;
  381. return 0 ;
  382. } /* callback */
  383. /*
  384. * Win32: Strip drive-letter and directory from a filename.
  385. * non-Win32: Strip directory from a filename.
  386. */
  387. static const char *db_basename (const char *fname)
  388. {
  389. const char *base = fname;
  390. #if !defined(_WIN32)
  391. const char *slash = strrchr (base, '/');
  392. if (slash)
  393. base = slash + 1 ;
  394. #else
  395. if (fname[0] && fname[1] == ':') {
  396. fname += 2;
  397. base = fname;
  398. }
  399. while (*fname) {
  400. if (IS_SLASH(*fname))
  401. base = fname + 1;
  402. fname++;
  403. }
  404. #endif
  405. return base ;
  406. }
  407. #else
  408. int dummy (void) ;
  409. int
  410. dummy (void)
  411. { /*
  412. ** Empty dummy fnction so tha compiler doesn't winge about an
  413. ** empty file.
  414. */
  415. return 0 ;
  416. } /* dummy */
  417. #endif