[xmail] [PATCH] LMAIL: preventing of excessive hdd access

2010-05-26 Thread Stephan Mueller
Hi,

please see attached patch for LMAIL. It stops from creating tmp files if
there is no need to do so. This prevents excessive hdd access.

Though, there is one very minor issue - maybe you can help me: Since
SysRemove(pszSSFileName); is always called it usually calls unlink()
which returns in an strace an ENOENT (as the tmp file most of the time
does not exist). I think that this issue is negligible. Still, Davide,
can you please check whether we can simply remove that SysRemove() call
as the tmp file should have never been created with my patch when
iFileCount == 0?

Thanks
Stephan 

--- LMAILSvr.cpp.orig	2010-05-26 08:49:02.0 +0200
+++ LMAILSvr.cpp	2010-05-26 08:49:58.0 +0200
@@ -116,6 +116,7 @@
  int iMaxSSFileName)
 {
 	char szSpoolDir[SYS_MAX_PATH];
+	FILE *pSSFile = NULL;
 
 	LMAILGetSpoolDir(szSpoolDir, sizeof(szSpoolDir));
 
@@ -129,14 +130,6 @@
 
 	UsrGetTmpFile(NULL, pszSSFileName, iMaxSSFileName);
 
-	FILE *pSSFile = fopen(pszSSFileName, wb);
-
-	if (pSSFile == NULL) {
-		ErrorPush();
-		RLckUnlockSH(hResLock);
-		return ErrorPop();
-	}
-
 	int iFileCount = 0;
 	char szSpoolFileName[SYS_MAX_PATH];
 	FSCAN_HANDLE hFileScan = MscFirstFile(szSpoolDir, 0, szSpoolFileName,
@@ -149,13 +142,25 @@
 
 			if ((ulHashValue % (unsigned long) pLMAILCfg-lNumThreads) ==
 			(unsigned long) lThreadId) {
+if(pSSFile == NULL) {
+	pSSFile = fopen(pszSSFileName, wb);
+
+	if (pSSFile == NULL) {
+		ErrorPush();
+		RLckUnlockSH(hResLock);
+		return ErrorPop();
+	}
+}
+
 fprintf(pSSFile, %s\r\n, szSpoolFileName);
 ++iFileCount;
 			}
 		} while (MscNextFile(hFileScan, szSpoolFileName, sizeof(szSpoolFileName)));
 		MscCloseFindFile(hFileScan);
 	}
-	fclose(pSSFile);
+
+	if(pSSFile != NULL)
+		fclose(pSSFile);
 	RLckUnlockSH(hResLock);
 
 	if (iFileCount == 0) {
___
xmail mailing list
xmail@xmailserver.org
http://xmailserver.org/mailman/listinfo/xmail


Re: [xmail] [PATCH] LMAIL: preventing of excessive hdd access

2010-05-26 Thread Stephan Mueller
An update of the patch: I forgot to also close the directory file handle
which leads to a file descriptor leak.

Furthermore, I removed the SysRemove() call as I think it is not
necessary any more - the file is never created if it is not needed. and
If it is needed, it should not be created.

Ciao
Stephan

Am Mittwoch, den 26.05.2010, 11:42 +0200 schrieb Stephan Mueller:
 Hi,
 
 please see attached patch for LMAIL. It stops from creating tmp files if
 there is no need to do so. This prevents excessive hdd access.
 
 Though, there is one very minor issue - maybe you can help me: Since
 SysRemove(pszSSFileName); is always called it usually calls unlink()
 which returns in an strace an ENOENT (as the tmp file most of the time
 does not exist). I think that this issue is negligible. Still, Davide,
 can you please check whether we can simply remove that SysRemove() call
 as the tmp file should have never been created with my patch when
 iFileCount == 0?
 
 Thanks
 Stephan 
 
 ___
 xmail mailing list
 xmail@xmailserver.org
 http://xmailserver.org/mailman/listinfo/xmail

--- LMAILSvr.cpp.orig	2010-05-26 08:32:53.381926212 +0200
+++ LMAILSvr.cpp	2010-05-26 13:31:52.271926317 +0200
@@ -116,6 +116,7 @@
  int iMaxSSFileName)
 {
 	char szSpoolDir[SYS_MAX_PATH];
+	FILE *pSSFile = NULL;
 
 	LMAILGetSpoolDir(szSpoolDir, sizeof(szSpoolDir));
 
@@ -129,14 +130,6 @@
 
 	UsrGetTmpFile(NULL, pszSSFileName, iMaxSSFileName);
 
-	FILE *pSSFile = fopen(pszSSFileName, wb);
-
-	if (pSSFile == NULL) {
-		ErrorPush();
-		RLckUnlockSH(hResLock);
-		return ErrorPop();
-	}
-
 	int iFileCount = 0;
 	char szSpoolFileName[SYS_MAX_PATH];
 	FSCAN_HANDLE hFileScan = MscFirstFile(szSpoolDir, 0, szSpoolFileName,
@@ -149,6 +142,17 @@
 
 			if ((ulHashValue % (unsigned long) pLMAILCfg-lNumThreads) ==
 			(unsigned long) lThreadId) {
+if(pSSFile == NULL) {
+	pSSFile = fopen(pszSSFileName, wb);
+
+	if (pSSFile == NULL) {
+		ErrorPush();
+		MscCloseFindFile(hFileScan);
+		RLckUnlockSH(hResLock);
+		return ErrorPop();
+	}
+}
+
 fprintf(pSSFile, %s\r\n, szSpoolFileName);
 ++iFileCount;
 			}
@@ -159,7 +163,6 @@
 	RLckUnlockSH(hResLock);
 
 	if (iFileCount == 0) {
-		SysRemove(pszSSFileName);
 		SetEmptyString(pszSSFileName);
 
 		ErrSetErrorCode(ERR_NO_LOCAL_SPOOL_FILES);
___
xmail mailing list
xmail@xmailserver.org
http://xmailserver.org/mailman/listinfo/xmail


Re: [xmail] [PATCH] LMAIL: preventing of excessive hdd access

2010-05-26 Thread Stephan Mueller
Sorry, another patch as the previous one omitted a sanity check that the
first patch had included.

The attached patch now has everything. It works on my system.

Ciao
Stephan

Am Mittwoch, den 26.05.2010, 13:35 +0200 schrieb Stephan Mueller:
 An update of the patch: I forgot to also close the directory file handle
 which leads to a file descriptor leak.
 
 Furthermore, I removed the SysRemove() call as I think it is not
 necessary any more - the file is never created if it is not needed. and
 If it is needed, it should not be created.
 
 Ciao
 Stephan
 
 Am Mittwoch, den 26.05.2010, 11:42 +0200 schrieb Stephan Mueller:
  Hi,
  
  please see attached patch for LMAIL. It stops from creating tmp files if
  there is no need to do so. This prevents excessive hdd access.
  
  Though, there is one very minor issue - maybe you can help me: Since
  SysRemove(pszSSFileName); is always called it usually calls unlink()
  which returns in an strace an ENOENT (as the tmp file most of the time
  does not exist). I think that this issue is negligible. Still, Davide,
  can you please check whether we can simply remove that SysRemove() call
  as the tmp file should have never been created with my patch when
  iFileCount == 0?
  
  Thanks
  Stephan 
  
  ___
  xmail mailing list
  xmail@xmailserver.org
  http://xmailserver.org/mailman/listinfo/xmail
 
 ___
 xmail mailing list
 xmail@xmailserver.org
 http://xmailserver.org/mailman/listinfo/xmail


--- LMAILSvr.cpp.orig	2010-05-26 08:49:02.0 +0200
+++ LMAILSvr.cpp	2010-05-26 17:09:08.0 +0200
@@ -116,6 +116,7 @@
  int iMaxSSFileName)
 {
 	char szSpoolDir[SYS_MAX_PATH];
+	FILE *pSSFile = NULL;
 
 	LMAILGetSpoolDir(szSpoolDir, sizeof(szSpoolDir));
 
@@ -129,14 +130,6 @@
 
 	UsrGetTmpFile(NULL, pszSSFileName, iMaxSSFileName);
 
-	FILE *pSSFile = fopen(pszSSFileName, wb);
-
-	if (pSSFile == NULL) {
-		ErrorPush();
-		RLckUnlockSH(hResLock);
-		return ErrorPop();
-	}
-
 	int iFileCount = 0;
 	char szSpoolFileName[SYS_MAX_PATH];
 	FSCAN_HANDLE hFileScan = MscFirstFile(szSpoolDir, 0, szSpoolFileName,
@@ -149,17 +142,29 @@
 
 			if ((ulHashValue % (unsigned long) pLMAILCfg-lNumThreads) ==
 			(unsigned long) lThreadId) {
+if(pSSFile == NULL) {
+	pSSFile = fopen(pszSSFileName, wb);
+
+	if (pSSFile == NULL) {
+		ErrorPush();
+		MscCloseFindFile(hFileScan);
+		RLckUnlockSH(hResLock);
+		return ErrorPop();
+	}
+}
+
 fprintf(pSSFile, %s\r\n, szSpoolFileName);
 ++iFileCount;
 			}
 		} while (MscNextFile(hFileScan, szSpoolFileName, sizeof(szSpoolFileName)));
 		MscCloseFindFile(hFileScan);
 	}
-	fclose(pSSFile);
+
+	if(pSSFile != NULL)
+		fclose(pSSFile);
 	RLckUnlockSH(hResLock);
 
 	if (iFileCount == 0) {
-		SysRemove(pszSSFileName);
 		SetEmptyString(pszSSFileName);
 
 		ErrSetErrorCode(ERR_NO_LOCAL_SPOOL_FILES);
___
xmail mailing list
xmail@xmailserver.org
http://xmailserver.org/mailman/listinfo/xmail


Re: [xmail] [PATCH] LMAIL: preventing of excessive hdd access

2010-05-26 Thread Davide Libenzi
On Wed, 26 May 2010, Stephan Mueller wrote:

 An update of the patch: I forgot to also close the directory file handle
 which leads to a file descriptor leak.
 
 Furthermore, I removed the SysRemove() call as I think it is not
 necessary any more - the file is never created if it is not needed. and
 If it is needed, it should not be created.

Heh, it could have been 10 years that I do not touch LMAIL :)
There is no need at all to create the temp file actually, although unless 
you're running LMAIL every 100 milliseconds, it does not really matter.



- Davide



diff --git a/LMAILSvr.cpp b/LMAILSvr.cpp
index ab92383..cfaacd5 100644
--- a/LMAILSvr.cpp
+++ b/LMAILSvr.cpp
@@ -112,8 +112,8 @@ static int LMAILLogEnabled(SHB_HANDLE hShbLMAIL, 
LMAILConfig *pLMAILCfg)
return (ulFlags  LMAILF_LOG_ENABLED) ? 1 : 0;
 }
 
-static int LMAILGetFilesSnapShot(LMAILConfig *pLMAILCfg, long lThreadId, char 
*pszSSFileName,
-int iMaxSSFileName)
+static int LMAILGetFilesSnapShot(LMAILConfig *pLMAILCfg, long lThreadId,
+SysListHead *pHead)
 {
char szSpoolDir[SYS_MAX_PATH];
 
@@ -127,15 +127,7 @@ static int LMAILGetFilesSnapShot(LMAILConfig *pLMAILCfg, 
long lThreadId, char *p
if (hResLock == INVALID_RLCK_HANDLE)
return ErrGetErrorCode();
 
-   UsrGetTmpFile(NULL, pszSSFileName, iMaxSSFileName);
-
-   FILE *pSSFile = fopen(pszSSFileName, wb);
-
-   if (pSSFile == NULL) {
-   ErrorPush();
-   RLckUnlockSH(hResLock);
-   return ErrorPop();
-   }
+   SYS_INIT_LIST_HEAD(pHead);
 
int iFileCount = 0;
char szSpoolFileName[SYS_MAX_PATH];
@@ -149,27 +141,19 @@ static int LMAILGetFilesSnapShot(LMAILConfig *pLMAILCfg, 
long lThreadId, char *p
 
if ((ulHashValue % (unsigned long) 
pLMAILCfg-lNumThreads) ==
(unsigned long) lThreadId) {
-   fprintf(pSSFile, %s\r\n, szSpoolFileName);
+   MscLstDatumAddT(pHead, szSpoolFileName,
+   strlen(szSpoolFileName));
++iFileCount;
}
} while (MscNextFile(hFileScan, szSpoolFileName, 
sizeof(szSpoolFileName)));
MscCloseFindFile(hFileScan);
}
-   fclose(pSSFile);
RLckUnlockSH(hResLock);
 
-   if (iFileCount == 0) {
-   SysRemove(pszSSFileName);
-   SetEmptyString(pszSSFileName);
-
-   ErrSetErrorCode(ERR_NO_LOCAL_SPOOL_FILES);
-   return ERR_NO_LOCAL_SPOOL_FILES;
-   }
-
-   return 0;
+   return iFileCount;
 }
 
-static int LMAILRemoveProcessed(LMAILConfig *pLMAILCfg, char const 
*pszListFileName)
+static int LMAILRemoveProcessed(LMAILConfig *pLMAILCfg, SysListHead *pHead)
 {
char szSpoolDir[SYS_MAX_PATH];
 
@@ -183,23 +167,17 @@ static int LMAILRemoveProcessed(LMAILConfig *pLMAILCfg, 
char const *pszListFileN
if (hResLock == INVALID_RLCK_HANDLE)
return ErrGetErrorCode();
 
-   FILE *pSSFile = fopen(pszListFileName, rb);
-
-   if (pSSFile == NULL) {
-   ErrorPush();
-   RLckUnlockEX(hResLock);
-   return ErrorPop();
-   }
+   SysListHead *pPos;
 
-   char szSpoolFileName[SYS_MAX_PATH];
-
-   while (MscGetString(pSSFile, szSpoolFileName, sizeof(szSpoolFileName) - 
1) != NULL) {
+   for (pPos = SYS_LIST_FIRST(pHead); pPos != NULL;
+pPos = SYS_LIST_NEXT(pPos, pHead)) {
+   LstDatum *pLDm = SYS_LIST_ENTRY(pPos, LstDatum, LLnk);
char szSpoolFilePath[SYS_MAX_PATH];
 
-   sprintf(szSpoolFilePath, %s%s%s, szSpoolDir, SYS_SLASH_STR, 
szSpoolFileName);
+   sprintf(szSpoolFilePath, %s%s%s, szSpoolDir, SYS_SLASH_STR,
+   pLDm-Data.pData);
CheckRemoveFile(szSpoolFilePath);
}
-   fclose(pSSFile);
RLckUnlockEX(hResLock);
 
return 0;
@@ -402,7 +380,8 @@ static int LMAILSubmitLocalFile(LMAILConfig *pLMAILCfg, 
const char *pszMailFile,
return 0;
 }
 
-static int LMAILProcessList(LMAILConfig *pLMAILCfg, long lThreadId, char const 
*pszSSFileName)
+static int LMAILProcessList(LMAILConfig *pLMAILCfg, long lThreadId,
+   SysListHead *pHead)
 {
char szSpoolDir[SYS_MAX_PATH];
 
@@ -423,15 +402,15 @@ static int LMAILProcessList(LMAILConfig *pLMAILCfg, long 
lThreadId, char const *
}
SvrReleaseConfigHandle(hSvrConfig);
 
-   FILE *pSSFile;
-   char szSpoolFileName[SYS_MAX_PATH];
+   SysListHead *pPos;
 
-   if ((pSSFile = fopen(pszSSFileName, rb)) == NULL)
-   return ErrGetErrorCode();
-   while (MscGetString(pSSFile, szSpoolFileName, sizeof(szSpoolFileName) - 
1) != NULL) {
+   for (pPos = SYS_LIST_FIRST(pHead); pPos != NULL;
+