| 12 |
|
|
| 13 |
#include "mix3.h" |
#include "mix3.h" |
| 14 |
|
|
| 15 |
|
#ifdef WIN32 |
| 16 |
|
#include <io.h> |
| 17 |
|
#include <direct.h> |
| 18 |
|
#include <process.h> |
| 19 |
|
#define sleep(s) Sleep(s*1000) |
| 20 |
|
#define S_IWUSR _S_IWRITE |
| 21 |
|
#define S_IRUSR _S_IREAD |
| 22 |
|
#else |
| 23 |
#include <unistd.h> |
#include <unistd.h> |
| 24 |
|
#endif |
| 25 |
#include <fcntl.h> |
#include <fcntl.h> |
| 26 |
#include <time.h> |
#include <time.h> |
| 27 |
#include <string.h> |
#include <string.h> |
| 29 |
#include <sys/types.h> |
#include <sys/types.h> |
| 30 |
#include <errno.h> |
#include <errno.h> |
| 31 |
#include <stdarg.h> |
#include <stdarg.h> |
| 32 |
|
#include <assert.h> |
| 33 |
|
|
| 34 |
|
#if defined(S_IFDIR) && !defined(S_ISDIR) |
| 35 |
|
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 36 |
|
#endif |
| 37 |
|
|
| 38 |
static unsigned long namecounter = 0; |
static unsigned long namecounter = 0; |
| 39 |
|
|
| 73 |
#define MAX_SUBNAME 123 /* actual length should be smaller than 115 bytes */ |
#define MAX_SUBNAME 123 /* actual length should be smaller than 115 bytes */ |
| 74 |
int maildirWrite(char *maildir, BUFFER *message, int create) { |
int maildirWrite(char *maildir, BUFFER *message, int create) { |
| 75 |
int fd; |
int fd; |
|
int currDir; |
|
| 76 |
int count; |
int count; |
| 77 |
int returnValue; |
int returnValue; |
| 78 |
char hostname[64]; |
char hostname[64]; |
| 81 |
char tmpname[MAX_SUBNAME]; |
char tmpname[MAX_SUBNAME]; |
| 82 |
char newname[MAX_SUBNAME]; |
char newname[MAX_SUBNAME]; |
| 83 |
int messagesize; |
int messagesize; |
| 84 |
|
char olddirectory[PATHMAX] = ""; |
| 85 |
|
|
| 86 |
/* Declare a handler for SIGALRM so we can time out. */ |
/* Declare a handler for SIGALRM so we can time out. */ |
| 87 |
/* set_handler(SIGALRM, alarm_handler); */ |
/* set_handler(SIGALRM, alarm_handler); */ |
| 101 |
|
|
| 102 |
messagesize = message->length; |
messagesize = message->length; |
| 103 |
|
|
| 104 |
/* Step 1: chdir to maildir (and save actual dir by opening it) */ |
/* Step 1: chdir to maildir (and save current dir) */ |
| 105 |
if((currDir = open(".", O_RDONLY)) < 0) { |
if (getcwd(olddirectory, PATHMAX) == NULL) { |
| 106 |
returnValue = -1; |
returnValue = -1; |
| 107 |
goto realend; |
goto realend; |
| 108 |
} |
} |
| 109 |
|
olddirectory[PATHMAX-1] = '\0'; |
| 110 |
if(chdir(maildir) != 0) { |
if(chdir(maildir) != 0) { |
| 111 |
returnValue = -1; |
returnValue = -1; |
| 112 |
goto functionExit; |
goto functionExit; |
| 119 |
snprintf(basename, MAX_BASENAME, "%lu.%u_%lu.%s,S=%u", |
snprintf(basename, MAX_BASENAME, "%lu.%u_%lu.%s,S=%u", |
| 120 |
time(NULL), getpid(), namecounter++, hostname, messagesize); |
time(NULL), getpid(), namecounter++, hostname, messagesize); |
| 121 |
basename[MAX_BASENAME-1] = '\0'; |
basename[MAX_BASENAME-1] = '\0'; |
| 122 |
strncat(tmpname, "tmp/", MAX_SUBNAME); |
strncat(tmpname, "tmp" DIRSEPSTR, MAX_SUBNAME); |
| 123 |
|
tmpname[MAX_SUBNAME-1] = '\0'; |
| 124 |
strncat(tmpname, basename, MAX_SUBNAME); |
strncat(tmpname, basename, MAX_SUBNAME); |
| 125 |
tmpname[MAX_SUBNAME-1] = '\0'; |
tmpname[MAX_SUBNAME-1] = '\0'; |
| 126 |
strncat(newname, "new/", MAX_SUBNAME); |
strncat(newname, "new" DIRSEPSTR, MAX_SUBNAME); |
| 127 |
|
newname[MAX_SUBNAME-1] = '\0'; |
| 128 |
strncat(newname, basename, MAX_SUBNAME); |
strncat(newname, basename, MAX_SUBNAME); |
| 129 |
newname[MAX_SUBNAME-1] = '\0'; |
newname[MAX_SUBNAME-1] = '\0'; |
| 130 |
|
|
| 206 |
snprintf(basename, MAX_BASENAME, "%lu.%u_%lu.%s,S=%u", |
snprintf(basename, MAX_BASENAME, "%lu.%u_%lu.%s,S=%u", |
| 207 |
time(NULL), getpid(), namecounter++, hostname, messagesize); |
time(NULL), getpid(), namecounter++, hostname, messagesize); |
| 208 |
basename[MAX_BASENAME-1] = '\0'; |
basename[MAX_BASENAME-1] = '\0'; |
| 209 |
strncat(newname, "new/", MAX_SUBNAME); |
strncat(newname, "new" DIRSEPSTR, MAX_SUBNAME); |
| 210 |
|
newname[MAX_SUBNAME-1] = '\0'; |
| 211 |
strncat(newname, basename, MAX_SUBNAME); |
strncat(newname, basename, MAX_SUBNAME); |
| 212 |
newname[MAX_SUBNAME-1] = '\0'; |
newname[MAX_SUBNAME-1] = '\0'; |
| 213 |
} |
} |
| 225 |
|
|
| 226 |
functionExit: |
functionExit: |
| 227 |
/* return to original directory */ |
/* return to original directory */ |
| 228 |
if ((fchdir(currDir) != 0) || (close(currDir) != 0)) |
assert(olddirectory[0] != '\0'); |
| 229 |
|
if(chdir(olddirectory) != 0) |
| 230 |
returnValue = -1; |
returnValue = -1; |
| 231 |
|
|
| 232 |
realend: |
realend: |
| 241 |
#endif |
#endif |
| 242 |
|
|
| 243 |
#include <dirent.h> |
#include <dirent.h> |
|
#include <assert.h> |
|
| 244 |
|
|
| 245 |
/* mock-up of errlog for unittest */ |
/* mock-up of errlog for unittest */ |
| 246 |
void errlog(int type, char *fmt,...) |
void errlog(int type, char *fmt,...) |