mdconvert.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
  34. # define ATTR_NORETURN __attribute__((noreturn))
  35. #else
  36. # define ATTR_NORETURN
  37. #endif
  38. static void ATTR_NORETURN
  39. oob( void )
  40. {
  41. fputs( "Fatal: buffer too small. Please report a bug.\n", stderr );
  42. abort();
  43. }
  44. static void
  45. sys_error( const char *msg, ... )
  46. {
  47. va_list va;
  48. char buf[1024];
  49. va_start( va, msg );
  50. if ((unsigned)vsnprintf( buf, sizeof(buf), msg, va ) >= sizeof(buf))
  51. oob();
  52. va_end( va );
  53. perror( buf );
  54. }
  55. static int
  56. nfsnprintf( char *buf, int blen, const char *fmt, ... )
  57. {
  58. int ret;
  59. va_list va;
  60. va_start( va, fmt );
  61. if (blen <= 0 || (unsigned)(ret = vsnprintf( buf, blen, fmt, va )) >= (unsigned)blen)
  62. oob();
  63. va_end( va );
  64. return ret;
  65. }
  66. static const char *subdirs[] = { "cur", "new" };
  67. static struct flock lck;
  68. static DBT key, value;
  69. static int
  70. convert( const char *box, int altmap )
  71. {
  72. DB *db;
  73. DIR *d;
  74. struct dirent *e;
  75. const char *u, *ru;
  76. char *p, *s, *dpath, *spath, *dbpath;
  77. int i, n, ret, sfd, dfd, bl, ml, uv[2], uid;
  78. struct stat st;
  79. char buf[_POSIX_PATH_MAX], buf2[_POSIX_PATH_MAX];
  80. char umpath[_POSIX_PATH_MAX], uvpath[_POSIX_PATH_MAX], tdpath[_POSIX_PATH_MAX];
  81. if (stat( box, &st ) || !S_ISDIR(st.st_mode)) {
  82. fprintf( stderr, "'%s' is no Maildir mailbox.\n", box );
  83. return 1;
  84. }
  85. nfsnprintf( umpath, sizeof(umpath), "%s/.isyncuidmap.db", box );
  86. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", box );
  87. if (altmap)
  88. dpath = umpath, spath = uvpath, dbpath = tdpath;
  89. else
  90. spath = umpath, dpath = uvpath, dbpath = umpath;
  91. nfsnprintf( tdpath, sizeof(tdpath), "%s.tmp", dpath );
  92. if ((sfd = open( spath, O_RDWR )) < 0) {
  93. if (errno != ENOENT)
  94. sys_error( "Cannot open %s", spath );
  95. return 1;
  96. }
  97. if (fcntl( sfd, F_SETLKW, &lck )) {
  98. sys_error( "Cannot lock %s", spath );
  99. goto sbork;
  100. }
  101. if ((dfd = open( tdpath, O_RDWR|O_CREAT, 0600 )) < 0) {
  102. sys_error( "Cannot create %s", tdpath );
  103. goto sbork;
  104. }
  105. if (db_create( &db, 0, 0 )) {
  106. fputs( "Error: db_create() failed\n", stderr );
  107. goto tbork;
  108. }
  109. if ((ret = (db->open)( db, 0, dbpath, 0, DB_HASH, DB_CREATE, 0 ))) {
  110. db->err( db, ret, "Error: db->open(%s)", dbpath );
  111. dbork:
  112. db->close( db, 0 );
  113. tbork:
  114. unlink( tdpath );
  115. close( dfd );
  116. sbork:
  117. close( sfd );
  118. return 1;
  119. }
  120. key.data = (void *)"UIDVALIDITY";
  121. key.size = 11;
  122. if (altmap) {
  123. if ((n = read( sfd, buf, sizeof(buf) )) <= 0 ||
  124. (buf[n] = 0, sscanf( buf, "%d\n%d", &uv[0], &uv[1] ) != 2))
  125. {
  126. fprintf( stderr, "Error: cannot read UIDVALIDITY of '%s'.\n", box );
  127. goto dbork;
  128. }
  129. value.data = uv;
  130. value.size = sizeof(uv);
  131. if ((ret = db->put( db, 0, &key, &value, 0 ))) {
  132. db->err( db, ret, "Error: cannot write UIDVALIDITY for '%s'", box );
  133. goto dbork;
  134. }
  135. } else {
  136. if ((ret = db->get( db, 0, &key, &value, 0 ))) {
  137. db->err( db, ret, "Error: cannot read UIDVALIDITY of '%s'", box );
  138. goto dbork;
  139. }
  140. n = sprintf( buf, "%d\n%d\n", ((int *)value.data)[0], ((int *)value.data)[1] );
  141. if (write( dfd, buf, n ) != n) {
  142. fprintf( stderr, "Error: cannot write UIDVALIDITY for '%s'.\n", box );
  143. goto dbork;
  144. }
  145. }
  146. again:
  147. for (i = 0; i < 2; i++) {
  148. bl = nfsnprintf( buf, sizeof(buf), "%s/%s/", box, subdirs[i] );
  149. if (!(d = opendir( buf ))) {
  150. sys_error( "Cannot list %s", buf );
  151. goto dbork;
  152. }
  153. while ((e = readdir( d ))) {
  154. if (*e->d_name == '.')
  155. continue;
  156. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s", e->d_name );
  157. memcpy( buf2, buf, bl );
  158. p = strstr( e->d_name, ",U=" );
  159. if (p)
  160. for (u = p, ru = p + 3; isdigit( (unsigned char)*ru ); ru++);
  161. else
  162. u = ru = strchr( e->d_name, ':' );
  163. if (u)
  164. ml = u - e->d_name;
  165. else
  166. ru = "", ml = sizeof(buf);
  167. if (altmap) {
  168. if (!p)
  169. continue;
  170. key.data = e->d_name;
  171. key.size = (size_t)(strchr( e->d_name, ',' ) - e->d_name);
  172. uid = atoi( p + 3 );
  173. value.data = &uid;
  174. value.size = sizeof(uid);
  175. if ((ret = db->put( db, 0, &key, &value, 0 ))) {
  176. db->err( db, ret, "Error: cannot write UID for '%s'", box );
  177. goto ebork;
  178. }
  179. nfsnprintf( buf2 + bl, sizeof(buf2) - bl, "%.*s%s", ml, e->d_name, ru );
  180. } else {
  181. s = strpbrk( e->d_name, ",:" );
  182. key.data = e->d_name;
  183. key.size = s ? (size_t)(s - e->d_name) : strlen( e->d_name );
  184. if ((ret = db->get( db, 0, &key, &value, 0 ))) {
  185. if (ret != DB_NOTFOUND) {
  186. db->err( db, ret, "Error: cannot read UID for '%s'", box );
  187. goto ebork;
  188. }
  189. continue;
  190. }
  191. uid = *(int *)value.data;
  192. nfsnprintf( buf2 + bl, sizeof(buf2) - bl, "%.*s,U=%d%s", ml, e->d_name, uid, ru );
  193. }
  194. if (rename( buf, buf2 )) {
  195. if (errno == ENOENT)
  196. goto again;
  197. sys_error( "Cannot rename %s to %s", buf, buf2 );
  198. ebork:
  199. closedir( d );
  200. goto dbork;
  201. }
  202. }
  203. closedir( d );
  204. }
  205. db->close( db, 0 );
  206. close( dfd );
  207. if (rename( tdpath, dpath )) {
  208. sys_error( "Cannot rename %s to %s", tdpath, dpath );
  209. return 1;
  210. }
  211. if (unlink( spath ))
  212. sys_error( "Cannot remove %s", spath );
  213. close( sfd );
  214. return 0;
  215. }
  216. int
  217. main( int argc, char **argv )
  218. {
  219. int oint, ret, altmap = 0;
  220. for (oint = 1; oint < argc; oint++) {
  221. if (!strcmp( argv[oint], "-h" ) || !strcmp( argv[oint], "--help" )) {
  222. puts(
  223. "Usage: " EXE " [-a] mailbox...\n"
  224. " -a, --alt convert to alternative (DB based) UID scheme\n"
  225. " -n, --native convert to native (file name based) UID scheme (default)\n"
  226. " -h, --help show this help message\n"
  227. " -v, --version display version"
  228. );
  229. return 0;
  230. } else if (!strcmp( argv[oint], "-v" ) || !strcmp( argv[oint], "--version" )) {
  231. puts( EXE " " VERSION " - Maildir UID scheme converter" );
  232. return 0;
  233. } else if (!strcmp( argv[oint], "-a" ) || !strcmp( argv[oint], "--alt" )) {
  234. altmap = 1;
  235. } else if (!strcmp( argv[oint], "-n" ) || !strcmp( argv[oint], "--native" )) {
  236. altmap = 0;
  237. } else if (!strcmp( argv[oint], "--" )) {
  238. oint++;
  239. break;
  240. } else if (argv[oint][0] == '-') {
  241. fprintf( stderr, "Unrecognized option '%s'. Try " EXE " -h\n", argv[oint] );
  242. return 1;
  243. } else
  244. break;
  245. }
  246. if (oint == argc) {
  247. fprintf( stderr, "Mailbox specification missing. Try " EXE " -h\n" );
  248. return 1;
  249. }
  250. #if SEEK_SET != 0
  251. lck.l_whence = SEEK_SET;
  252. #endif
  253. #if F_WRLCK != 0
  254. lck.l_type = F_WRLCK;
  255. #endif
  256. ret = 0;
  257. for (; oint < argc; oint++)
  258. ret |= convert( argv[oint], altmap );
  259. return ret;
  260. }