home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / winnt / pop3 / popfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  9.4 KB  |  374 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples.
  4. *       Copyright (C) 1992-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. //+---------------------------------------------------------------------------
  13. //
  14. //  File:       popfile.c
  15. //
  16. //  Contents:
  17. //
  18. //  Classes:
  19. //
  20. //  Functions:
  21. //
  22. //----------------------------------------------------------------------------
  23.  
  24. #include "pop3srvp.h"
  25. #pragma hdrstop
  26.  
  27. #define POP3_LOCKFILE       TEXT("poplock")
  28. #define POP3_LOCKFILE_NAME  TEXT("\\") POP3_LOCKFILE
  29. #define POP3_SEARCH         TEXT("\\*.*")
  30.  
  31.  
  32. WCHAR   BaseDirectory[MAX_PATH];
  33.  
  34. //+---------------------------------------------------------------------------
  35. //
  36. //  Function:   LockMailDirectory
  37. //
  38. //  Synopsis:   Attempts to lock the mail directory
  39. //
  40. //  Arguments:  [hUserToken] -- Token of user
  41. //              [pszPath]    -- path to the mail directory
  42. //
  43. //  Returns:    Handle of lock file on success, or NULL
  44. //
  45. //  History:    1-06-95   RichardW   Created
  46. //
  47. //  Notes:
  48. //
  49. //----------------------------------------------------------------------------
  50. HANDLE
  51. LockMailDirectory(
  52.     HANDLE          hUserToken,
  53.     PCHAR           pszPath)
  54. {
  55.  
  56.     WCHAR   PathToOpen[MAX_PATH];
  57.     WCHAR   WidePath[64];
  58.     HANDLE  hLockFile;
  59.  
  60.     //
  61.     // Build lock file name
  62.     //
  63.     MultiByteToWideChar(
  64.         CP_ACP, MB_PRECOMPOSED,
  65.         pszPath, -1,
  66.         WidePath, sizeof(WidePath) / sizeof(WCHAR) );
  67.  
  68.     wcscpy(PathToOpen, BaseDirectory);
  69.     wcscat(PathToOpen, WidePath);
  70.     wcscat(PathToOpen, POP3_LOCKFILE_NAME);
  71.  
  72.     //
  73.     // Impersonate the client
  74.     //
  75. #ifdef SECURE_BUILD
  76.  
  77.     ImpersonateLoggedOnUser(hUserToken);
  78.  
  79. #endif
  80.  
  81.     //
  82.     // Attempt to create the file.  This will fail if it already exists,
  83.     // which would indicate that another thread/client is accessing this
  84.     // mailbox.
  85.     //
  86.     DebugLog((DEB_TRACE, "Opening lock file as %ws\n", PathToOpen));
  87.  
  88.     hLockFile = CreateFile( PathToOpen,
  89.                             GENERIC_READ | GENERIC_WRITE,
  90.                             0,
  91.                             NULL,
  92.                             CREATE_NEW,
  93.                             FILE_ATTRIBUTE_NORMAL |
  94.                             FILE_FLAG_DELETE_ON_CLOSE,
  95.                             NULL);
  96.  
  97.     //
  98.     // Done being the client.  Back to server security
  99.     //
  100.     RevertToSelf();
  101.  
  102.     //
  103.     // Return a handle, or NULL if failed
  104.     //
  105.     return(hLockFile != INVALID_HANDLE_VALUE ? hLockFile : NULL);
  106.  
  107. }
  108.  
  109. //+---------------------------------------------------------------------------
  110. //
  111. //  Function:   OpenMailMessage
  112. //
  113. //  Synopsis:   Opens the specified mail message as the user
  114. //
  115. //  Arguments:  [MessageId]  --
  116. //              [pDir]       --
  117. //              [hUserToken] --
  118. //
  119. //  History:    1-08-95   RichardW   Created
  120. //
  121. //  Notes:
  122. //
  123. //----------------------------------------------------------------------------
  124. HANDLE
  125. OpenMailMessage(
  126.     DWORD               MessageId,
  127.     PPopMailDirectory   pDir,
  128.     HANDLE              hUserToken)
  129. {
  130.     HANDLE  hMailFile;
  131.     WCHAR   Path[MAX_PATH];
  132.  
  133.     //
  134.     // Impersonate the client
  135.     //
  136. #ifdef SECURE_BUILD
  137.  
  138.     ImpersonateLoggedOnUser(hUserToken);
  139.  
  140. #endif
  141.  
  142.     //
  143.     // Attempt to create the file.  This will fail if it already exists,
  144.     // which would indicate that another thread/client is accessing this
  145.     // mailbox.
  146.     //
  147.     wcscpy(Path, pDir->pBaseDir);
  148.     wcscat(Path, TEXT("\\"));
  149.     wcscat(Path, pDir->Messages[MessageId].pszFileName);
  150.  
  151.     hMailFile = CreateFile( Path,
  152.                             GENERIC_READ | GENERIC_WRITE,
  153.                             0,
  154.                             NULL,
  155.                             OPEN_EXISTING,
  156.                             FILE_ATTRIBUTE_HIDDEN,
  157.                             NULL);
  158.  
  159.     //
  160.     // Done being the client.  Back to server security
  161.     //
  162.     RevertToSelf();
  163.  
  164.     //
  165.     // Return a handle, or NULL if failed
  166.     //
  167.     return(hMailFile != INVALID_HANDLE_VALUE ? hMailFile : NULL);
  168. }
  169.  
  170.  
  171. //+---------------------------------------------------------------------------
  172. //
  173. //  Function:   UnlockMailDirectory
  174. //
  175. //  Synopsis:   Unlocks the mail directory
  176. //
  177. //  Arguments:  [hLockFile] -- Handle of lock file to close
  178. //
  179. //  Returns:    TRUE if success, FALSE if failure
  180. //
  181. //  History:    1-06-95   RichardW   Created
  182. //
  183. //  Notes:
  184. //
  185. //----------------------------------------------------------------------------
  186. BOOL
  187. UnlockMailDirectory(
  188.     HANDLE      hLockFile)
  189. {
  190.     //
  191.     // Since the lock file was opened with DELETE_ON_CLOSE semantics, it will
  192.     // softly and silently vanish away when we close this, the one handle to
  193.     // it.
  194.     //
  195.  
  196.     return(CloseHandle(hLockFile));
  197. }
  198.  
  199.  
  200.  
  201. //+---------------------------------------------------------------------------
  202. //
  203. //  Function:   ReadMailDirectory
  204. //
  205. //  Synopsis:   Reads in a list of all the files in the directory
  206. //
  207. //  Arguments:  [hUserToken] --
  208. //              [pszPath]    --
  209. //
  210. //  Returns:
  211. //
  212. //  History:    1-08-95   RichardW   Created
  213. //
  214. //  Notes:
  215. //
  216. //----------------------------------------------------------------------------
  217. PPopMailDirectory
  218. ReadMailDirectory(
  219.     HANDLE          hUserToken,
  220.     PCHAR           pszPath)
  221. {
  222.     WIN32_FIND_DATA     FindData;
  223.     PPopMailDirectory   pDir;
  224.     WCHAR               MailDir[MAX_PATH];
  225.     WCHAR               WidePath[64];
  226.     HANDLE              hSearch;
  227.     PPopMessageHeader   pExtraHeaders;
  228.     PPopMessageHeader   pHeader;
  229.     DWORD               RemainingHeaders;
  230.  
  231.     MultiByteToWideChar(
  232.         CP_ACP, MB_PRECOMPOSED,
  233.         pszPath, -1,
  234.         WidePath, sizeof(WidePath) / sizeof(WCHAR) );
  235.     wcscpy(MailDir, BaseDirectory);
  236.     wcscat(MailDir, WidePath);
  237.  
  238.     pDir = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, sizeof(PopMailDirectory));
  239.     pDir->pBaseDir = LocalAlloc(LMEM_FIXED, (wcslen(MailDir) + 1) * sizeof(WCHAR));
  240.     wcscpy(pDir->pBaseDir, MailDir);
  241.  
  242.     wcscat(MailDir, POP3_SEARCH);
  243.  
  244. #ifdef SECURE_BUILD
  245.  
  246.     ImpersonateLoggedOnUser(hUserToken);
  247.  
  248. #endif
  249.  
  250.     hSearch = FindFirstFile(MailDir, &FindData);
  251.  
  252.     if (hSearch == INVALID_HANDLE_VALUE)
  253.     {
  254.         DebugLog((DEB_TRACE, "FindFirst FAILED!, %d\n", GetLastError()));
  255.         RevertToSelf();
  256.  
  257.         return(NULL);
  258.     }
  259.  
  260.     pExtraHeaders = LocalAlloc(LMEM_FIXED, sizeof(PopMessageHeader) * 10);
  261.     RemainingHeaders = 10;
  262.     pHeader = pExtraHeaders;
  263.  
  264.     do
  265.     {
  266.         if (FindData.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY |
  267.                                         FILE_ATTRIBUTE_SYSTEM |
  268.                                         FILE_ATTRIBUTE_HIDDEN) )
  269.         {
  270.             continue;   // Skip non-normal files
  271.         }
  272.         if (wcsicmp(FindData.cFileName,POP3_LOCKFILE) == 0)
  273.         {
  274.             continue;   // Skip our lock file
  275.         }
  276.         DebugLog((DEB_TRACE, "ReadDir:  Message %ws, size %d\n",
  277.                 FindData.cFileName, FindData.nFileSizeLow));
  278.  
  279.         pHeader->Flags = 0;
  280.         pDir->TotalSize += FindData.nFileSizeLow;
  281.         pHeader->Size = FindData.nFileSizeLow;
  282.  
  283.         pHeader->pszFileName = LocalAlloc(LMEM_FIXED,
  284.                             (wcslen(FindData.cFileName) + 1) * sizeof(WCHAR) );
  285.  
  286.         wcscpy(pHeader->pszFileName, FindData.cFileName);
  287.         pHeader++;
  288.         RemainingHeaders--;
  289.  
  290.  
  291.  
  292.         if (RemainingHeaders == 0)
  293.         {
  294.             PPopMessageHeader   pTemp;
  295.  
  296.  
  297.             pTemp = LocalAlloc(LMEM_FIXED, sizeof(PopMessageHeader) * (pDir->cMessages + 20));
  298.  
  299.             CopyMemory(pTemp, pExtraHeaders, sizeof(PopMessageHeader) * (pDir->cMessages + 10));
  300.  
  301.             LocalFree(pExtraHeaders);
  302.  
  303.             pExtraHeaders = pTemp;
  304.  
  305.             RemainingHeaders = 10;
  306.  
  307.             pDir->cMessages += 10;
  308.         }
  309.  
  310.     } while (FindNextFile(hSearch, &FindData));
  311.  
  312.     pDir->cMessages += (10 - RemainingHeaders);
  313.     pDir->Messages = pExtraHeaders;
  314.  
  315.     FindClose(hSearch);
  316.  
  317.     pDir->cAvailMessages = pDir->cMessages;
  318.     pDir->AvailSize = pDir->TotalSize;
  319.  
  320.     RevertToSelf();
  321.  
  322.     return(pDir);
  323. }
  324.  
  325. BOOL
  326. CommitMailDirectory(
  327.     PPopMailDirectory   pDir,
  328.     HANDLE              hUserToken)
  329. {
  330.     DWORD   i;
  331.     WCHAR   Path[MAX_PATH];
  332.     PWSTR   pszPath;
  333.  
  334.     wcscpy(Path, pDir->pBaseDir);
  335.     wcscat(Path, TEXT("\\"));
  336.     pszPath = Path + wcslen(Path);
  337.  
  338.  
  339. #ifdef SECURE_BUILD
  340.  
  341.     ImpersonateLoggedOnUser(hUserToken);
  342.  
  343. #endif
  344.  
  345.     for (i = 0; i < pDir->cMessages ; i++)
  346.     {
  347.         if (pDir->Messages[i].Flags & POP3_MESSAGE_DELETE)
  348.         {
  349.             wcscpy(pszPath, pDir->Messages[i].pszFileName);
  350.             DeleteFile(Path);
  351.         }
  352.     }
  353.  
  354.     RevertToSelf();
  355.  
  356.     return(TRUE);
  357. }
  358.  
  359. void
  360. FreeMailDirectory(
  361.     PPopMailDirectory   pDir)
  362. {
  363.     DWORD   i;
  364.  
  365.     for (i = 0 ; i < pDir->cMessages ; i++ )
  366.     {
  367.         LocalFree(pDir->Messages[i].pszFileName);
  368.     }
  369.  
  370.     LocalFree(pDir->Messages);
  371.     LocalFree(pDir);
  372.  
  373. }
  374.