mdconvert.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * mdconvert - Maildir UID scheme converter
  3. * Copyright (C) 2004 Oswald Buddenhagen <ossi@users.sf.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <sys/stat.h>
  25. #include <stdarg.h>
  26. #include <dirent.h>
  27. #include <limits.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <db.h>
  32. #define EXE "mdconvert"
  33. static int
  34. nfsnprintf( char *buf, int blen, const char *fmt, ... )
  35. {
  36. int ret;
  37. va_list va;
  38. va_start( va, fmt );
  39. if (blen <= 0 || (unsigned)(ret = vsnprintf( buf, blen, fmt, va )) >= (unsigned)blen) {
  40. fputs( "Fatal: buffer too small. Please report a bug.\n", stderr );
  41. abort();
  42. }
  43. va_end( va );
  44. return ret;
  45. }
  46. static const char *subdirs[] = { "cur", "new" };
  47. static struct flock lck;
  48. static DBT key, value;
  49. static inline int
  50. convert( const char *box, int altmap )
  51. {
  52. DB *db;
  53. DIR *d;
  54. struct dirent *e;
  55. const char *u, *ru;
  56. char *p, *s, *dpath, *spath, *dbpath;
  57. int i, n, ret, sfd, dfd, bl, ml, uv[2], uid;
  58. struct stat st;
  59. char buf[_POSIX_PATH_MAX], buf2[_POSIX_PATH_MAX];
  60. char umpath[_POSIX_PATH_MAX], uvpath[_POSIX_PATH_MAX], tdpath[_POSIX_PATH_MAX];
  61. if (stat( box, &st ) || !S_ISDIR(st.st_mode)) {
  62. fprintf( stderr, "'%s' is no Maildir mailbox.\n", box );
  63. return 1;
  64. }
  65. nfsnprintf( umpath, sizeof(umpath), "%s/.isyncuidmap.db", box );
  66. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", box );
  67. if (altmap)
  68. dpath = umpath, spath = uvpath, dbpath = tdpath;
  69. else
  70. spath = umpath, dpath = uvpath, dbpath = umpath;
  71. nfsnprintf( tdpath, sizeof(tdpath), "%s.tmp", dpath );
  72. if ((sfd = open( spath, O_RDWR )) < 0) {
  73. if (errno != ENOENT)
  74. perror( spath );
  75. return 1;
  76. }
  77. if (fcntl( sfd, F_SETLKW, &lck )) {
  78. perror( spath );
  79. goto sbork;
  80. }
  81. if ((dfd = open( tdpath, O_RDWR|O_CREAT, 0600 )) < 0) {
  82. perror( tdpath );
  83. goto sbork;
  84. }
  85. if (db_create( &db, 0, 0 )) {
  86. fputs( "Error: db_create() failed\n", stderr );
  87. goto tbork;
  88. }
  89. if ((ret = (db->open)( db, 0, dbpath, 0, DB_HASH, DB_CREATE, 0 ))) {
  90. db->err( db, ret, "Error: db->open(%s)", dbpath );
  91. dbork:
  92. db->close( db, 0 );
  93. tbork:
  94. unlink( tdpath );
  95. close( dfd );
  96. sbork:
  97. close( sfd );
  98. return 1;
  99. }
  100. key.data = (void *)"UIDVALIDITY";
  101. key.size = 11;
  102. if (altmap) {
  103. if ((n = read( sfd, buf, sizeof(buf) )) <= 0 ||
  104. (buf[n] = 0, sscanf( buf, "%d\n%d", &uv[0], &uv[1] ) != 2))
  105. {
  106. fprintf( stderr, "Error: cannot read UIDVALIDITY of '%s'.\n", box );
  107. goto dbork;
  108. }
  109. value.data = uv;
  110. value.size = sizeof(uv);
  111. if ((ret = db->put( db, 0, &key, &value, 0 ))) {
  112. db->err( db, ret, "Error: cannot write UIDVALIDITY for '%s'", box );
  113. goto dbork;
  114. }
  115. } else {
  116. if ((ret = db->get( db, 0, &key, &value, 0 ))) {
  117. db->err( db, ret, "Error: cannot read UIDVALIDITY of '%s'", box );
  118. goto dbork;
  119. }
  120. n = sprintf( buf, "%d\n%d\n", ((int *)value.data)[0], ((int *)value.data)[1] );
  121. if (write( dfd, buf, n ) != n) {
  122. fprintf( stderr, "Error: cannot write UIDVALIDITY for '%s'.\n", box );
  123. goto dbork;
  124. }
  125. }
  126. again:
  127. for (i = 0; i < 2; i++) {
  128. bl = nfsnprintf( buf, sizeof(buf), "%s/%s/", box, subdirs[i] );
  129. if (!(d = opendir( buf ))) {
  130. perror( "opendir" );
  131. goto dbork;
  132. }
  133. while ((e = readdir( d ))) {
  134. if (*e->d_name == '.')
  135. continue;
  136. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s", e->d_name );
  137. memcpy( buf2, buf, bl );
  138. p = strstr( e->d_name, ",U=" );
  139. if (p)
  140. for (u = p, ru = p + 3; isdigit( (unsigned char)*ru ); ru++);
  141. else
  142. u = ru = strchr( e->d_name, ':' );
  143. if (u)
  144. ml = u - e->d_name;
  145. else
  146. ru = "", ml = sizeof(buf);
  147. if (altmap) {
  148. if (!p)
  149. continue;
  150. key.data = e->d_name;
  151. key.size = (size_t)(strchr( e->d_name, ',' ) - e->d_name);
  152. uid = atoi( p + 3 );
  153. value.data = &uid;
  154. value.size = sizeof(uid);
  155. if ((ret = db->put( db, 0, &key, &value, 0 ))) {
  156. db->err( db, ret, "Error: cannot write UID for '%s'", box );
  157. goto ebork;
  158. }
  159. nfsnprintf( buf2 + bl, sizeof(buf2) - bl, "%.*s%s", ml, e->d_name, ru );
  160. } else {
  161. s = strpbrk( e->d_name, ",:" );
  162. key.data = e->d_name;
  163. key.size = s ? (size_t)(s - e->d_name) : strlen( e->d_name );
  164. if ((ret = db->get( db, 0, &key, &value, 0 ))) {
  165. if (ret != DB_NOTFOUND) {
  166. db->err( db, ret, "Error: cannot read UID for '%s'", box );
  167. goto ebork;
  168. }
  169. continue;
  170. }
  171. uid = *(int *)value.data;
  172. nfsnprintf( buf2 + bl, sizeof(buf2) - bl, "%.*s,U=%d%s", ml, e->d_name, uid, ru );
  173. }
  174. if (rename( buf, buf2 )) {
  175. if (errno == ENOENT)
  176. goto again;
  177. perror( buf );
  178. ebork:
  179. closedir( d );
  180. goto dbork;
  181. }
  182. }
  183. closedir( d );
  184. }
  185. db->close( db, 0 );
  186. close( dfd );
  187. if (rename( tdpath, dpath )) {
  188. perror( dpath );
  189. return 1;
  190. }
  191. if (unlink( spath ))
  192. perror( spath );
  193. close( sfd );
  194. return 0;
  195. }
  196. int
  197. main( int argc, char **argv )
  198. {
  199. int oint, ret, altmap = 0;
  200. for (oint = 1; oint < argc; oint++) {
  201. if (!strcmp( argv[oint], "-h" ) || !strcmp( argv[oint], "--help" )) {
  202. puts(
  203. "Usage: " EXE " [-a] mailbox...\n"
  204. " -a, --alt convert to alternative (DB based) UID scheme\n"
  205. " -n, --native convert to native (file name based) UID scheme (default)\n"
  206. " -h, --help show this help message\n"
  207. " -v, --version display version"
  208. );
  209. return 0;
  210. } else if (!strcmp( argv[oint], "-v" ) || !strcmp( argv[oint], "--version" )) {
  211. puts( EXE " " VERSION " - Maildir UID scheme converter" );
  212. return 0;
  213. } else if (!strcmp( argv[oint], "-a" ) || !strcmp( argv[oint], "--alt" )) {
  214. altmap = 1;
  215. } else if (!strcmp( argv[oint], "-n" ) || !strcmp( argv[oint], "--native" )) {
  216. altmap = 0;
  217. } else if (!strcmp( argv[oint], "--" )) {
  218. oint++;
  219. break;
  220. } else if (argv[oint][0] == '-') {
  221. fprintf( stderr, "Unrecognized option '%s'. Try " EXE " -h\n", argv[oint] );
  222. return 1;
  223. } else
  224. break;
  225. }
  226. if (oint == argc) {
  227. fprintf( stderr, "Mailbox specification missing. Try " EXE " -h\n" );
  228. return 1;
  229. }
  230. #if SEEK_SET != 0
  231. lck.l_whence = SEEK_SET;
  232. #endif
  233. #if F_WRLCK != 0
  234. lck.l_type = F_WRLCK;
  235. #endif
  236. ret = 0;
  237. for (; oint < argc; oint++)
  238. ret |= convert( argv[oint], altmap );
  239. return ret;
  240. }