Source code of Windows XP (NT5)
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.

288 lines
5.6 KiB

  1. #include "faxrtp.h"
  2. #pragma hdrstop
  3. BOOL
  4. IsExistingConnection(
  5. LPCTSTR RemoteName
  6. )
  7. /*++
  8. Routine Description:
  9. Checks to see if we are connected already.
  10. Arguments:
  11. RemoteName - UNC name of remote host
  12. Return Value:
  13. TRUE for success, FALSE on error
  14. --*/
  15. {
  16. DWORD rc;
  17. HANDLE hEnum;
  18. DWORD Entries;
  19. NETRESOURCE *nrr = NULL;
  20. DWORD cb;
  21. DWORD i;
  22. DWORD ss;
  23. BOOL rval = FALSE;
  24. rc = WNetOpenEnum( RESOURCE_CONNECTED, RESOURCETYPE_ANY, 0, NULL, &hEnum );
  25. if (rc != NO_ERROR) {
  26. return FALSE;
  27. }
  28. ss = 0;
  29. cb = 64 * 1024;
  30. nrr = (NETRESOURCE*) MemAlloc( cb );
  31. if (!nrr) {
  32. WNetCloseEnum( hEnum );
  33. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  34. return FALSE;
  35. }
  36. ZeroMemory( nrr, cb );
  37. while( TRUE ) {
  38. Entries = (DWORD)-1;
  39. rc = WNetEnumResource( hEnum, &Entries, nrr, &cb );
  40. if (rc == ERROR_NO_MORE_ITEMS) {
  41. break;
  42. } else if (rc == ERROR_MORE_DATA) {
  43. cb += 16;
  44. MemFree( nrr );
  45. nrr = (NETRESOURCE*) MemAlloc( cb );
  46. if (!nrr) {
  47. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  48. break;
  49. }
  50. ZeroMemory( nrr, cb );
  51. continue;
  52. } else if (rc != NO_ERROR) {
  53. break;
  54. }
  55. for (i=0; i<Entries; i++) {
  56. if (_tcsicmp( nrr[i].lpRemoteName, RemoteName ) == 0) {
  57. rval = TRUE;
  58. break;
  59. }
  60. }
  61. }
  62. if (nrr) MemFree( nrr );
  63. WNetCloseEnum( hEnum );
  64. return rval;
  65. }
  66. BOOL
  67. EstablishConnection(
  68. LPCTSTR FileName
  69. )
  70. /*++
  71. Routine Description:
  72. Tries to establish a network connection if file is remote.
  73. Arguments:
  74. FileName - Name of file
  75. Return Value:
  76. TRUE for success, FALSE on error
  77. --*/
  78. {
  79. NETRESOURCE nr;
  80. DWORD rc;
  81. DWORD i;
  82. LPTSTR RemoteName;
  83. LPTSTR p;
  84. if (!FileName) {
  85. return FALSE;
  86. }
  87. if (!( (FileName[0] == TEXT('\\')) &&
  88. (FileName[1] == TEXT('\\')) )) {
  89. return FALSE;
  90. }
  91. p = _tcschr( &FileName[2], TEXT('\\') );
  92. if (!p) {
  93. //
  94. // malformed name
  95. //
  96. return FALSE;
  97. }
  98. p = _tcschr( p+1, TEXT('\\') );
  99. if (!p) {
  100. p = (LPTSTR) &FileName[_tcsclen(FileName)];
  101. }
  102. i = (DWORD)(p - FileName);
  103. RemoteName = (LPTSTR) MemAlloc( (i + 1) * sizeof(TCHAR) );
  104. if (!RemoteName) {
  105. return FALSE;
  106. }
  107. _tcsnccpy( RemoteName, FileName, i );
  108. RemoteName[i] = 0;
  109. if (IsExistingConnection( RemoteName )) {
  110. MemFree( RemoteName );
  111. return TRUE;
  112. }
  113. nr.dwScope = 0;
  114. nr.dwType = RESOURCETYPE_DISK;
  115. nr.dwDisplayType = 0;
  116. nr.dwUsage = 0;
  117. nr.lpLocalName = NULL;
  118. nr.lpRemoteName = RemoteName;
  119. nr.lpComment = NULL;
  120. nr.lpProvider = NULL;
  121. rc = WNetAddConnection2( &nr, NULL, NULL, 0 );
  122. if (rc != NO_ERROR) {
  123. MemFree( RemoteName );
  124. return FALSE;
  125. }
  126. MemFree( RemoteName );
  127. return TRUE;
  128. }
  129. BOOL
  130. FaxMoveFile(
  131. LPCTSTR TiffFileName,
  132. LPCTSTR DestDir
  133. )
  134. /*++
  135. Routine Description:
  136. Stores a FAX in the specified directory. This routine will also
  137. cached network connections and attemp to create the destination directory
  138. if it does not exist.
  139. Arguments:
  140. TiffFileName - Name of TIFF file to store
  141. DestDir - Name of directory to store it in
  142. Return Value:
  143. TRUE for success, FALSE on error
  144. --*/
  145. {
  146. LPTSTR NameBuffer = NULL;
  147. LPTSTR DstFName = NULL;
  148. LPTSTR FBaseName;
  149. DWORD StrSize;
  150. BOOL RVal = FALSE;
  151. LPTSTR pStr;
  152. StrSize = GetFullPathName (
  153. (LPTSTR)TiffFileName,
  154. 0,
  155. DstFName,
  156. &FBaseName
  157. );
  158. DstFName = (LPTSTR) MemAlloc( (StrSize + 1) * sizeof(TCHAR));
  159. if (!DstFName) {
  160. goto exit;
  161. }
  162. GetFullPathName (
  163. TiffFileName,
  164. StrSize,
  165. DstFName,
  166. &FBaseName
  167. );
  168. StrSize = StringSize( DestDir );
  169. NameBuffer = (LPTSTR) MemAlloc( StrSize + 4 + StringSize( FBaseName ) );
  170. if (!NameBuffer) {
  171. goto exit;
  172. }
  173. _tcscpy( NameBuffer, DestDir );
  174. pStr = &NameBuffer[(StrSize/sizeof(TCHAR)) - 2];
  175. if (*pStr != TEXT( '\\' )) {
  176. *++pStr = TEXT( '\\' );
  177. }
  178. pStr++;
  179. _tcscpy( pStr, FBaseName );
  180. EstablishConnection (NameBuffer);
  181. if (CopyFile (TiffFileName, NameBuffer, TRUE)) {
  182. RVal = TRUE;
  183. goto exit;
  184. }
  185. //
  186. // try to create the directory
  187. //
  188. if (GetLastError() == ERROR_PATH_NOT_FOUND) {
  189. // if the pathname is too long, return a more descriptive error
  190. if (StringSize( NameBuffer ) >= MAX_PATH) {
  191. SetLastError( ERROR_BUFFER_OVERFLOW );
  192. RVal = FALSE;
  193. }
  194. else {
  195. MakeDirectory(DestDir);
  196. RVal = CopyFile( TiffFileName, NameBuffer, TRUE );
  197. }
  198. }
  199. exit:
  200. if (RVal) {
  201. FaxLog(
  202. FAXLOG_CATEGORY_INBOUND,
  203. FAXLOG_LEVEL_MAX,
  204. 2,
  205. MSG_FAX_SAVE_SUCCESS,
  206. TiffFileName,
  207. NameBuffer
  208. );
  209. } else {
  210. FaxLog(
  211. FAXLOG_CATEGORY_INBOUND,
  212. FAXLOG_LEVEL_MIN,
  213. 3,
  214. MSG_FAX_SAVE_FAILED,
  215. TiffFileName,
  216. NameBuffer,
  217. GetLastErrorText(GetLastError())
  218. );
  219. }
  220. if (DstFName) {
  221. MemFree( DstFName );
  222. }
  223. if (NameBuffer) {
  224. MemFree( NameBuffer );
  225. }
  226. return RVal;
  227. }