log_archive.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:10k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*-
  2.  * See the file LICENSE for redistribution information.
  3.  *
  4.  * Copyright (c) 1997, 1998, 1999, 2000
  5.  * Sleepycat Software.  All rights reserved.
  6.  */
  7. #include "db_config.h"
  8. #ifndef lint
  9. static const char revid[] = "$Id: log_archive.c,v 11.13 2000/11/30 00:58:40 ubell Exp $";
  10. #endif /* not lint */
  11. #ifndef NO_SYSTEM_INCLUDES
  12. #include <sys/types.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #endif
  17. #ifdef  HAVE_RPC
  18. #include "db_server.h"
  19. #endif
  20. #include "db_int.h"
  21. #include "db_dispatch.h"
  22. #include "log.h"
  23. #include "clib_ext.h" /* XXX: needed for getcwd. */
  24. #ifdef HAVE_RPC
  25. #include "gen_client_ext.h"
  26. #include "rpc_client_ext.h"
  27. #endif
  28. static int __absname __P((DB_ENV *, char *, char *, char **));
  29. static int __build_data __P((DB_ENV *, char *, char ***, void *(*)(size_t)));
  30. static int __cmpfunc __P((const void *, const void *));
  31. static int __usermem __P((DB_ENV *, char ***, void *(*)(size_t)));
  32. /*
  33.  * log_archive --
  34.  * Supporting function for db_archive(1).
  35.  */
  36. int
  37. log_archive(dbenv, listp, flags, db_malloc)
  38. DB_ENV *dbenv;
  39. char ***listp;
  40. u_int32_t flags;
  41. void *(*db_malloc) __P((size_t));
  42. {
  43. DBT rec;
  44. DB_LOG *dblp;
  45. DB_LSN stable_lsn;
  46. u_int32_t fnum;
  47. int array_size, n, ret;
  48. char **array, **arrayp, *name, *p, *pref, buf[MAXPATHLEN];
  49. #ifdef HAVE_RPC
  50. if (F_ISSET(dbenv, DB_ENV_RPCCLIENT))
  51. return (__dbcl_log_archive(dbenv, listp, flags, db_malloc));
  52. #endif
  53. PANIC_CHECK(dbenv);
  54. ENV_REQUIRES_CONFIG(dbenv, dbenv->lg_handle, DB_INIT_LOG);
  55. name = NULL;
  56. dblp = dbenv->lg_handle;
  57. COMPQUIET(fnum, 0);
  58. #define OKFLAGS (DB_ARCH_ABS | DB_ARCH_DATA | DB_ARCH_LOG)
  59. if (flags != 0) {
  60. if ((ret =
  61.     __db_fchk(dbenv, "log_archive", flags, OKFLAGS)) != 0)
  62. return (ret);
  63. if ((ret =
  64.     __db_fcchk(dbenv,
  65. "log_archive", flags, DB_ARCH_DATA, DB_ARCH_LOG)) != 0)
  66. return (ret);
  67. }
  68. /*
  69.  * Get the absolute pathname of the current directory.  It would
  70.  * be nice to get the shortest pathname of the database directory,
  71.  * but that's just not possible.
  72.  *
  73.  * XXX
  74.  * Can't trust getcwd(3) to set a valid errno.  If it doesn't, just
  75.  * guess that we ran out of memory.
  76.  */
  77. if (LF_ISSET(DB_ARCH_ABS)) {
  78. __os_set_errno(0);
  79. if ((pref = getcwd(buf, sizeof(buf))) == NULL) {
  80. if (__os_get_errno() == 0)
  81. __os_set_errno(ENOMEM);
  82. return (__os_get_errno());
  83. }
  84. } else
  85. pref = NULL;
  86. switch (LF_ISSET(~DB_ARCH_ABS)) {
  87. case DB_ARCH_DATA:
  88. return (__build_data(dbenv, pref, listp, db_malloc));
  89. case DB_ARCH_LOG:
  90. memset(&rec, 0, sizeof(rec));
  91. if (F_ISSET(dbenv, DB_ENV_THREAD))
  92. F_SET(&rec, DB_DBT_MALLOC);
  93. if ((ret = log_get(dbenv, &stable_lsn, &rec, DB_LAST)) != 0)
  94. return (ret);
  95. if (F_ISSET(dbenv, DB_ENV_THREAD))
  96. __os_free(rec.data, rec.size);
  97. fnum = stable_lsn.file;
  98. break;
  99. case 0:
  100. if ((ret = __log_findckp(dbenv, &stable_lsn)) != 0) {
  101. /*
  102.  * A return of DB_NOTFOUND means that we didn't find
  103.  * any records in the log (so we are not going to be
  104.  * deleting any log files).
  105.  */
  106. if (ret != DB_NOTFOUND)
  107. return (ret);
  108. *listp = NULL;
  109. return (0);
  110. }
  111. /* Remove any log files before the last stable LSN. */
  112. fnum = stable_lsn.file - 1;
  113. break;
  114. }
  115. #define LIST_INCREMENT 64
  116. /* Get some initial space. */
  117. array_size = 10;
  118. if ((ret = __os_malloc(dbenv,
  119.     sizeof(char *) * array_size, NULL, &array)) != 0)
  120. return (ret);
  121. array[0] = NULL;
  122. /* Build an array of the file names. */
  123. for (n = 0; fnum > 0; --fnum) {
  124. if ((ret = __log_name(dblp, fnum, &name, NULL, 0)) != 0)
  125. goto err;
  126. if (__os_exists(name, NULL) != 0) {
  127. if (LF_ISSET(DB_ARCH_LOG) && fnum == stable_lsn.file)
  128. continue;
  129. __os_freestr(name);
  130. name = NULL;
  131. break;
  132. }
  133. if (n >= array_size - 1) {
  134. array_size += LIST_INCREMENT;
  135. if ((ret = __os_realloc(dbenv,
  136.     sizeof(char *) * array_size, NULL, &array)) != 0)
  137. goto err;
  138. }
  139. if (LF_ISSET(DB_ARCH_ABS)) {
  140. if ((ret = __absname(dbenv,
  141.     pref, name, &array[n])) != 0)
  142. goto err;
  143. __os_freestr(name);
  144. } else if ((p = __db_rpath(name)) != NULL) {
  145. if ((ret = __os_strdup(dbenv, p + 1, &array[n])) != 0)
  146. goto err;
  147. __os_freestr(name);
  148. } else
  149. array[n] = name;
  150. name = NULL;
  151. array[++n] = NULL;
  152. }
  153. /* If there's nothing to return, we're done. */
  154. if (n == 0) {
  155. *listp = NULL;
  156. ret = 0;
  157. goto err;
  158. }
  159. /* Sort the list. */
  160. qsort(array, (size_t)n, sizeof(char *), __cmpfunc);
  161. /* Rework the memory. */
  162. if ((ret = __usermem(dbenv, &array, db_malloc)) != 0)
  163. goto err;
  164. *listp = array;
  165. return (0);
  166. err: if (array != NULL) {
  167. for (arrayp = array; *arrayp != NULL; ++arrayp)
  168. __os_freestr(*arrayp);
  169. __os_free(array, sizeof(char *) * array_size);
  170. }
  171. if (name != NULL)
  172. __os_freestr(name);
  173. return (ret);
  174. }
  175. /*
  176.  * __build_data --
  177.  * Build a list of datafiles for return.
  178.  */
  179. static int
  180. __build_data(dbenv, pref, listp, db_malloc)
  181. DB_ENV *dbenv;
  182. char *pref, ***listp;
  183. void *(*db_malloc) __P((size_t));
  184. {
  185. DBT rec;
  186. DB_LSN lsn;
  187. __log_register_args *argp;
  188. u_int32_t rectype;
  189. int array_size, last, n, nxt, ret;
  190. char **array, **arrayp, *p, *real_name;
  191. /* Get some initial space. */
  192. array_size = 10;
  193. if ((ret = __os_malloc(dbenv,
  194.     sizeof(char *) * array_size, NULL, &array)) != 0)
  195. return (ret);
  196. array[0] = NULL;
  197. memset(&rec, 0, sizeof(rec));
  198. if (F_ISSET(dbenv, DB_ENV_THREAD))
  199. F_SET(&rec, DB_DBT_MALLOC);
  200. for (n = 0, ret = log_get(dbenv, &lsn, &rec, DB_FIRST);
  201.     ret == 0; ret = log_get(dbenv, &lsn, &rec, DB_NEXT)) {
  202. if (rec.size < sizeof(rectype)) {
  203. ret = EINVAL;
  204. __db_err(dbenv, "log_archive: bad log record");
  205. goto lg_free;
  206. }
  207. memcpy(&rectype, rec.data, sizeof(rectype));
  208. if (rectype != DB_log_register) {
  209. if (F_ISSET(dbenv, DB_ENV_THREAD)) {
  210. __os_free(rec.data, rec.size);
  211. rec.data = NULL;
  212. }
  213. continue;
  214. }
  215. if ((ret = __log_register_read(dbenv, rec.data, &argp)) != 0) {
  216. ret = EINVAL;
  217. __db_err(dbenv,
  218.     "log_archive: unable to read log record");
  219. goto lg_free;
  220. }
  221. if (n >= array_size - 1) {
  222. array_size += LIST_INCREMENT;
  223. if ((ret = __os_realloc(dbenv,
  224.     sizeof(char *) * array_size, NULL, &array)) != 0)
  225. goto lg_free;
  226. }
  227. if ((ret = __os_strdup(dbenv,
  228.     argp->name.data, &array[n])) != 0) {
  229. lg_free: if (F_ISSET(&rec, DB_DBT_MALLOC) && rec.data != NULL)
  230. __os_free(rec.data, rec.size);
  231. goto err1;
  232. }
  233. array[++n] = NULL;
  234. __os_free(argp, 0);
  235. if (F_ISSET(dbenv, DB_ENV_THREAD)) {
  236. __os_free(rec.data, rec.size);
  237. rec.data = NULL;
  238. }
  239. }
  240. /* If there's nothing to return, we're done. */
  241. if (n == 0) {
  242. ret = 0;
  243. *listp = NULL;
  244. goto err1;
  245. }
  246. /* Sort the list. */
  247. qsort(array, (size_t)n, sizeof(char *), __cmpfunc);
  248. /*
  249.  * Build the real pathnames, discarding nonexistent files and
  250.  * duplicates.
  251.  */
  252. for (last = nxt = 0; nxt < n;) {
  253. /*
  254.  * Discard duplicates.  Last is the next slot we're going
  255.  * to return to the user, nxt is the next slot that we're
  256.  * going to consider.
  257.  */
  258. if (last != nxt) {
  259. array[last] = array[nxt];
  260. array[nxt] = NULL;
  261. }
  262. for (++nxt; nxt < n &&
  263.     strcmp(array[last], array[nxt]) == 0; ++nxt) {
  264. __os_freestr(array[nxt]);
  265. array[nxt] = NULL;
  266. }
  267. /* Get the real name. */
  268. if ((ret = __db_appname(dbenv,
  269.     DB_APP_DATA, NULL, array[last], 0, NULL, &real_name)) != 0)
  270. goto err2;
  271. /* If the file doesn't exist, ignore it. */
  272. if (__os_exists(real_name, NULL) != 0) {
  273. __os_freestr(real_name);
  274. __os_freestr(array[last]);
  275. array[last] = NULL;
  276. continue;
  277. }
  278. /* Rework the name as requested by the user. */
  279. __os_freestr(array[last]);
  280. array[last] = NULL;
  281. if (pref != NULL) {
  282. ret = __absname(dbenv, pref, real_name, &array[last]);
  283. __os_freestr(real_name);
  284. if (ret != 0)
  285. goto err2;
  286. } else if ((p = __db_rpath(real_name)) != NULL) {
  287. ret = __os_strdup(dbenv, p + 1, &array[last]);
  288. __os_freestr(real_name);
  289. if (ret != 0)
  290. goto err2;
  291. } else
  292. array[last] = real_name;
  293. ++last;
  294. }
  295. /* NULL-terminate the list. */
  296. array[last] = NULL;
  297. /* Rework the memory. */
  298. if ((ret = __usermem(dbenv, &array, db_malloc)) != 0)
  299. goto err1;
  300. *listp = array;
  301. return (0);
  302. err2: /*
  303.  * XXX
  304.  * We've possibly inserted NULLs into the array list, so clean up a
  305.  * bit so that the other error processing works.
  306.  */
  307. if (array != NULL)
  308. for (; nxt < n; ++nxt)
  309. __os_freestr(array[nxt]);
  310. /* FALLTHROUGH */
  311. err1: if (array != NULL) {
  312. for (arrayp = array; *arrayp != NULL; ++arrayp)
  313. __os_freestr(*arrayp);
  314. __os_free(array, array_size * sizeof(char *));
  315. }
  316. return (ret);
  317. }
  318. /*
  319.  * __absname --
  320.  * Return an absolute path name for the file.
  321.  */
  322. static int
  323. __absname(dbenv, pref, name, newnamep)
  324. DB_ENV *dbenv;
  325. char *pref, *name, **newnamep;
  326. {
  327. size_t l_pref, l_name;
  328. int isabspath, ret;
  329. char *newname;
  330. l_name = strlen(name);
  331. isabspath = __os_abspath(name);
  332. l_pref = isabspath ? 0 : strlen(pref);
  333. /* Malloc space for concatenating the two. */
  334. if ((ret = __os_malloc(dbenv,
  335.     l_pref + l_name + 2, NULL, &newname)) != 0)
  336. return (ret);
  337. *newnamep = newname;
  338. /* Build the name.  If `name' is an absolute path, ignore any prefix. */
  339. if (!isabspath) {
  340. memcpy(newname, pref, l_pref);
  341. if (strchr(PATH_SEPARATOR, newname[l_pref - 1]) == NULL)
  342. newname[l_pref++] = PATH_SEPARATOR[0];
  343. }
  344. memcpy(newname + l_pref, name, l_name + 1);
  345. return (0);
  346. }
  347. /*
  348.  * __usermem --
  349.  * Create a single chunk of memory that holds the returned information.
  350.  * If the user has their own malloc routine, use it.
  351.  */
  352. static int
  353. __usermem(dbenv, listp, db_malloc)
  354. DB_ENV *dbenv;
  355. char ***listp;
  356. void *(*db_malloc) __P((size_t));
  357. {
  358. size_t len;
  359. int ret;
  360. char **array, **arrayp, **orig, *strp;
  361. /* Find out how much space we need. */
  362. for (len = 0, orig = *listp; *orig != NULL; ++orig)
  363. len += sizeof(char *) + strlen(*orig) + 1;
  364. len += sizeof(char *);
  365. /* Allocate it and set up the pointers. */
  366. if ((ret = __os_malloc(dbenv, len, db_malloc, &array)) != 0)
  367. return (ret);
  368. strp = (char *)(array + (orig - *listp) + 1);
  369. /* Copy the original information into the new memory. */
  370. for (orig = *listp, arrayp = array; *orig != NULL; ++orig, ++arrayp) {
  371. len = strlen(*orig);
  372. memcpy(strp, *orig, len + 1);
  373. *arrayp = strp;
  374. strp += len + 1;
  375. __os_freestr(*orig);
  376. }
  377. /* NULL-terminate the list. */
  378. *arrayp = NULL;
  379. __os_free(*listp, 0);
  380. *listp = array;
  381. return (0);
  382. }
  383. static int
  384. __cmpfunc(p1, p2)
  385. const void *p1, *p2;
  386. {
  387. return (strcmp(*((char * const *)p1), *((char * const *)p2)));
  388. }