mdconvert.c 7.4 KB

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