mdconvert.c 7.5 KB

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