Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.0 KiB

  1. #include "faxrtp.h"
  2. #pragma hdrstop
  3. #include <strsafe.h>
  4. static
  5. DWORD
  6. CreateUniqueTIFfile (
  7. IN LPCTSTR wszDstDir,
  8. OUT LPTSTR wszDstFile,
  9. IN DWORD dwDstFileSize
  10. )
  11. /*++
  12. Routine name : CreateUniqueTIFfile
  13. Routine description:
  14. Finds a unique TIF file name in the specified directory.
  15. The file is in the format path\FaxXXXXXXXX.TIF
  16. where:
  17. path = wszDstDir
  18. XXXXXXXX = Hexadecimal representation of a unique ID
  19. Author:
  20. Eran Yariv (EranY), Jun, 1999
  21. Arguments:
  22. wszDstDir [in] - Destiantion directory fo the file (must exist)
  23. wszDstFile [out] - Resulting unique file name
  24. dwDstFileSize [in] - The size of the buffer, pointed to by wszDstFile in TCHARs
  25. Return Value:
  26. DWORD - Win32 error code
  27. --*/
  28. {
  29. DEBUG_FUNCTION_NAME(TEXT("CreateUniqueTIFfile"));
  30. static DWORD dwLastID = 0xffffffff;
  31. DWORD dwPrevLastID = dwLastID;
  32. for (DWORD dwCurID = dwLastID + 1; dwCurID != dwPrevLastID; dwCurID++)
  33. {
  34. //
  35. // Try with the current Id
  36. //
  37. HRESULT hr = StringCchPrintf( wszDstFile,
  38. dwDstFileSize,
  39. _T("%s\\Fax%08x.TIF"),
  40. wszDstDir,
  41. dwCurID );
  42. if (FAILED(hr))
  43. {
  44. return HRESULT_CODE(hr);
  45. }
  46. HANDLE hFile;
  47. hFile = SafeCreateFile (
  48. wszDstFile,
  49. GENERIC_WRITE,
  50. 0,
  51. NULL,
  52. CREATE_NEW,
  53. FILE_ATTRIBUTE_NORMAL,
  54. NULL);
  55. if (INVALID_HANDLE_VALUE == hFile)
  56. {
  57. DWORD dwErr = GetLastError ();
  58. if (ERROR_FILE_EXISTS == dwErr)
  59. {
  60. //
  61. // This ID is already in use
  62. //
  63. continue;
  64. }
  65. //
  66. // Otherwise, this is another error
  67. //
  68. DebugPrintEx (DEBUG_ERR,
  69. L"Error while calling CreateFile on %s (ec = %ld)",
  70. wszDstFile,
  71. dwErr
  72. );
  73. return dwErr;
  74. }
  75. //
  76. // Otherwise, we succeeded.
  77. //
  78. CloseHandle (hFile);
  79. dwLastID = dwCurID;
  80. return ERROR_SUCCESS;
  81. }
  82. //
  83. // All IDs are occupied
  84. //
  85. DebugPrintEx (DEBUG_ERR,
  86. L"All IDs are occupied");
  87. return ERROR_NO_MORE_FILES;
  88. } // CreateUniqueTIFfile
  89. BOOL
  90. FaxMoveFile(
  91. LPCTSTR TiffFileName,
  92. LPCTSTR DestDir
  93. )
  94. /*++
  95. Routine Description:
  96. Stores a FAX in the specified directory. This routine will also
  97. cached network connections.
  98. Arguments:
  99. TiffFileName - Name of TIFF file to store
  100. DestDir - Name of directory to store it in
  101. Return Value:
  102. TRUE for success, FALSE on error
  103. --*/
  104. {
  105. WCHAR TempDstDir [MAX_PATH + 1];
  106. WCHAR DstFile[MAX_PATH * 2] = {0};
  107. DWORD dwErr = ERROR_SUCCESS;
  108. int iDstPathLen;
  109. DEBUG_FUNCTION_NAME(TEXT("FaxMoveFile"));
  110. Assert (DestDir);
  111. //
  112. // Remove any '\' characters at end of destination directory
  113. //
  114. HRESULT hr = StringCchCopy(
  115. TempDstDir,
  116. ARR_SIZE(TempDstDir),
  117. DestDir );
  118. if (FAILED(hr))
  119. {
  120. DebugPrintEx (
  121. DEBUG_ERR,
  122. L"Store folder name exceeds MAX_PATH chars");
  123. dwErr = HRESULT_CODE(hr);
  124. goto end;
  125. }
  126. iDstPathLen = lstrlen (TempDstDir);
  127. Assert (iDstPathLen);
  128. if ('\\' == TempDstDir[iDstPathLen - 1])
  129. {
  130. TempDstDir[iDstPathLen - 1] = L'\0';
  131. }
  132. //
  133. // Create unique destiantion file name
  134. //
  135. dwErr = CreateUniqueTIFfile (TempDstDir, DstFile, ARR_SIZE(TempDstDir));
  136. if (ERROR_SUCCESS != dwErr)
  137. {
  138. goto end;
  139. }
  140. //
  141. // Try to copy the file.
  142. // We use FALSE as 3rd parameter because CreateUniqueTIFfile creates
  143. // and empty unique file.
  144. //
  145. if (!CopyFile (TiffFileName, DstFile, FALSE))
  146. {
  147. dwErr = GetLastError ();
  148. DebugPrintEx (DEBUG_ERR,
  149. L"Can't copy file (ec = %ld)",
  150. dwErr
  151. );
  152. goto end;
  153. }
  154. end:
  155. if (ERROR_SUCCESS != dwErr)
  156. {
  157. FaxLog(
  158. FAXLOG_CATEGORY_INBOUND,
  159. FAXLOG_LEVEL_MIN,
  160. 3,
  161. MSG_FAX_SAVE_FAILED,
  162. TiffFileName,
  163. (*DstFile)?DstFile:TempDstDir,
  164. DWORD2HEX(dwErr)
  165. );
  166. return FALSE;
  167. }
  168. else
  169. {
  170. FaxLog(
  171. FAXLOG_CATEGORY_INBOUND,
  172. FAXLOG_LEVEL_MAX,
  173. 2,
  174. MSG_FAX_SAVE_SUCCESS,
  175. TiffFileName,
  176. (*DstFile)?DstFile:TempDstDir
  177. );
  178. return TRUE;
  179. }
  180. }