Teach mailsplit about Maildir's
PATCH 1/1 Teach mailsplit about Maildir's
---
builtin-mailsplit.c | 107 ++++++++++++++++++++++++++++++++++++++++++--------- builtin.h | 2 +- 2 files changed, 89 insertions(+), 20 deletions(-)
builtin-mailsplit.c +88 -19
@@ -96,44 +96,93 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)exit (1 ); } -int split_mbox (const char **mbox,const char *dir,int allow_bare,int nr_prec,int skip) +int split_maildir (const char *maildir,const char *dir,int nr_prec,int skip) { -char *name =xmalloc (strlen (dir) +2 +3 *sizeof (skip)); +char file[PATH_MAX ]; +char curdir[PATH_MAX ]; +char name[PATH_MAX ]; +DIR *mddir; +struct dirent *maildent;int ret =-1 ; -while (*mbox) { -const char *file = *mbox++; -FILE *f = !strcmp (file,"-" ) ? stdin :fopen (file,"r" ); -int file_done =0 ; +snprintf (curdir,sizeof (curdir),"%s/cur" , maildir); +if ((mddir =opendir (curdir)) ==NULL ) { +error ("cannot diropen %s (%s)" , curdir,strerror (errno)); + goto out; + } + +while ((maildent =readdir (mddir)) !=NULL ) { +FILE *f; + +snprintf (file,sizeof (file),"%s/%s" , + curdir, maildent->d_name); + +if (maildent->d_name[0 ] =='.' ) +continue ; -if ( !f ) { -error ("cannot open mbox %s" , file); + f =fopen (file,"r" ); +if (!f) { +error ("cannot open mail %s (%s)" , file,strerror (errno)); goto out; }if (fgets (buf,sizeof (buf), f) ==NULL ) { -if (f == stdin) -break ;/* empty stdin is OK */ -error ("cannot read mbox %s" , file); +error ("cannot read mail %s (%s)" , file,strerror (errno)); goto out; } -while (!file_done) { -sprintf (name,"%s/%0*d" , dir, nr_prec, ++skip); - file_done =split_one (f, name, allow_bare); +sprintf (name,"%s/%0*d" , dir, nr_prec, ++skip); +split_one (f, name,1 ); + +fclose (f); + } + +closedir (mddir); + + ret = skip; +out: +return ret; +} + +int split_mbox (const char *file,const char *dir,int allow_bare, +int nr_prec,int skip) +{ +char name[PATH_MAX ]; +int ret =-1 ; + +FILE *f = !strcmp (file,"-" ) ? stdin :fopen (file,"r" ); +int file_done =0 ; + +if (!f) { +error ("cannot open mbox %s" , file); + goto out; + } + +if (fgets (buf,sizeof (buf), f) ==NULL ) { +/* empty stdin is OK */ +if (f != stdin) { +error ("cannot read mbox %s" , file); + goto out; } + file_done =1 ; + } -if (f != stdin) -fclose (f); +while (!file_done) { +sprintf (name,"%s/%0*d" , dir, nr_prec, ++skip); + file_done =split_one (f, name, allow_bare); } + +if (f != stdin) +fclose (f); + ret = skip; out: -free (name);return ret; } +int cmd_mailsplit (int argc,const char **argv,const char *prefix) { -int nr =0 , nr_prec =4 , ret; +int nr =0 , nr_prec =4 , ret =0 ;int allow_bare =0 ;const char *dir =NULL ;const char **argp;
@@ -186,7 +235,27 @@ int cmd_mailsplit(int argc, const char **argv, const char *prefix) argp = stdin_only; } - ret =split_mbox (argp, dir, allow_bare, nr_prec, nr); +while (*argp) { +const char *arg = *argp++; +struct stat argstat; + +if (arg[0 ] =='-' && arg[1 ] ==0 ) { + ret |=split_mbox (arg, dir, allow_bare, nr_prec, nr); +continue ; + } + +if (stat (arg, &argstat) ==-1 ) { +error ("cannot stat %s (%s)" , arg,strerror (errno)); +return 1 ; + } + +if (S_ISDIR (argstat.st_mode)) { + ret |=split_maildir (arg, dir, nr_prec, nr); + }else { + ret |=split_mbox (arg, dir, allow_bare, nr_prec, nr); + } + } +if (ret !=-1 )printf ("%d\n" , ret);
builtin.h +1 -1
@@ -8,7 +8,7 @@ extern const char git_usage_string[];extern void help_unknown_cmd (const char *cmd);extern int mailinfo (FILE *in,FILE *out,int ks,const char *encoding,const char *msg,const char *patch); -extern int split_mbox (const char **mbox,const char *dir,int allow_bare,int nr_prec,int skip); +extern int split_mbox (const char *file,const char *dir,int allow_bare,int nr_prec,int skip);extern void stripspace (FILE *in,FILE *out);extern int write_tree (unsigned char *sha1,int missing_ok,const char *prefix);extern void prune_packed_objects (int );
Signature · 1 line
1.5.1.2
Quoted 7 lines · Fernando J. Pereda
Signed-off-by Fernando J. Pereda <[email protected]>---
builtin-mailsplit.c | 107 ++++++++++++++++++++++++++++++++++++++++++--------- builtin.h | 2 +- 2 files changed, 89 insertions(+), 20 deletions(-)
Actually, I forgot to update the documentation, I'll send an updated patch.
- ferdy
Signature · 2 lines
Fernando J. Pereda Garcimartín 20BB BDC3 761A 4781 E6ED ED0B 0A48 5B0C 60BD 28D4
Quoted 14 lines · Fernando J. Pereda
builtin-mailsplit.c +11
-0+int split_maildir (const char *maildir,const char *dir,int nr_prec,int skip) { +while ((maildent =readdir (mddir)) !=NULL ) { +FILE *f; + +snprintf (file,sizeof (file),"%s/%s" , + curdir, maildent->d_name); + +if (maildent->d_name[0 ] =='.' ) +continue ; +sprintf (name,"%s/%0*d" , dir, nr_prec, ++skip); +split_one (f, name,1 );
builtin-mailsplit.c static int split_one(FILE *mbox, const char *name, int allow_bare) · line 145
Quoted 10 lines
builtin-mailsplit.c +10
-0+ +fclose (f); + } + +closedir (mddir); + + ret = skip; +out: +return ret; +}
I do not personally deal with maildir so I do not know for sure, but this feels very wrong.
What order are you emitting the output?
split_mbox() is designed to number the messages the same order as they are found in the mailbox, but the above loop relies on readdir() to give them in a reasonable order to you, which does not seem a right assumption to me (otherwise "/bin/ls" and friends would not sort what they read from the filesystem would they?).
Quoted 30 lines · Junio C Hamano
Quoted 14 lines · Fernando J. Pereda
builtin-mailsplit.c +11
-0+int split_maildir (const char *maildir,const char *dir,int nr_prec,int skip) { +while ((maildent =readdir (mddir)) !=NULL ) { +FILE *f; + +snprintf (file,sizeof (file),"%s/%s" , + curdir, maildent->d_name); + +if (maildent->d_name[0 ] =='.' ) +continue ; +sprintf (name,"%s/%0*d" , dir, nr_prec, ++skip); +split_one (f, name,1 );builtin-mailsplit.c static int split_one(FILE *mbox, const char *name, int allow_bare) · line 145
Quoted 10 lines
builtin-mailsplit.c +10
-0+ +fclose (f); + } + +closedir (mddir); + + ret = skip; +out: +return ret; +}I do not personally deal with maildir so I do not know for sure, but this feels very wrong.
Quoted 8 lines
What order are you emitting the output?
split_mbox()is designed to number the messages the same order as they are found in the mailbox, but the above loop relies onreaddir()to give them in a reasonable order to you, which does not seem a right assumption to me (otherwise "/bin/ls" and friends would not sort what they read from the filesystem would they?).
It is indeed very wrong. You can't sort them without opening and parsing the headers. Please drop this patch.
Sorry for the noise.
- ferdy
Signature · 2 lines
Fernando J. Pereda Garcimartín 20BB BDC3 761A 4781 E6ED ED0B 0A48 5B0C 60BD 28D4