drive.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Drive management code
  3. *
  4. * Copyright 2003 Mark Westcott
  5. * Copyright 2003-2004 Mike Hearn
  6. * Copyright 2004 Chris Morgan
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. *
  22. */
  23. #include "config.h"
  24. #include "wine/port.h"
  25. #include <assert.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <ntstatus.h>
  30. #define WIN32_NO_STATUS
  31. #include <windef.h>
  32. #include <winbase.h>
  33. #include <winternl.h>
  34. #include <winioctl.h>
  35. #include <winreg.h>
  36. #include <wine/debug.h>
  37. #include <shellapi.h>
  38. #include <objbase.h>
  39. #include <shlguid.h>
  40. #include <shlwapi.h>
  41. #include <shlobj.h>
  42. #define WINE_MOUNTMGR_EXTENSIONS
  43. #include <ddk/mountmgr.h>
  44. #include "winecfg.h"
  45. #include "resource.h"
  46. WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
  47. struct drive drives[26]; /* one for each drive letter */
  48. static inline int letter_to_index(char letter)
  49. {
  50. return (toupper(letter) - 'A');
  51. }
  52. /* This function produces a mask for each drive letter that isn't
  53. * currently used. Each bit of the long result represents a letter,
  54. * with A being the least significant bit, and Z being the most
  55. * significant.
  56. *
  57. * To calculate this, we loop over each letter, and see if we can get
  58. * a drive entry for it. If so, we set the appropriate bit. At the
  59. * end, we flip each bit, to give the desired result.
  60. *
  61. * The letter parameter is always marked as being available. This is
  62. * so the edit dialog can display the currently used drive letter
  63. * alongside the available ones.
  64. */
  65. ULONG drive_available_mask(char letter)
  66. {
  67. ULONG result = 0;
  68. int i;
  69. WINE_TRACE("\n");
  70. for(i = 0; i < 26; i++)
  71. {
  72. if (!drives[i].in_use) continue;
  73. result |= (1 << (letter_to_index(drives[i].letter)));
  74. }
  75. result = ~result;
  76. if (letter) result |= DRIVE_MASK_BIT(letter);
  77. WINE_TRACE("finished drive letter loop with %x\n", result);
  78. return result;
  79. }
  80. BOOL add_drive(char letter, const char *targetpath, const char *device, const WCHAR *label,
  81. DWORD serial, DWORD type)
  82. {
  83. int driveIndex = letter_to_index(letter);
  84. if(drives[driveIndex].in_use)
  85. return FALSE;
  86. WINE_TRACE("letter == '%c', unixpath == %s, device == %s, label == %s, serial == %08x, type == %d\n",
  87. letter, wine_dbgstr_a(targetpath), wine_dbgstr_a(device),
  88. wine_dbgstr_w(label), serial, type);
  89. drives[driveIndex].letter = toupper(letter);
  90. drives[driveIndex].unixpath = strdupA(targetpath);
  91. drives[driveIndex].device = device ? strdupA(device) : NULL;
  92. drives[driveIndex].label = label ? strdupW(label) : NULL;
  93. drives[driveIndex].serial = serial;
  94. drives[driveIndex].type = type;
  95. drives[driveIndex].in_use = TRUE;
  96. drives[driveIndex].modified = TRUE;
  97. return TRUE;
  98. }
  99. /* deallocates the contents of the drive. does not free the drive itself */
  100. void delete_drive(struct drive *d)
  101. {
  102. HeapFree(GetProcessHeap(), 0, d->unixpath);
  103. d->unixpath = NULL;
  104. HeapFree(GetProcessHeap(), 0, d->device);
  105. d->device = NULL;
  106. HeapFree(GetProcessHeap(), 0, d->label);
  107. d->label = NULL;
  108. d->serial = 0;
  109. d->in_use = FALSE;
  110. d->modified = TRUE;
  111. }
  112. static DWORD get_drive_type( char letter )
  113. {
  114. HKEY hKey;
  115. char driveValue[4];
  116. DWORD ret = DRIVE_UNKNOWN;
  117. sprintf(driveValue, "%c:", letter);
  118. if (RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Drives", &hKey) != ERROR_SUCCESS)
  119. WINE_TRACE(" Unable to open Software\\Wine\\Drives\n" );
  120. else
  121. {
  122. char buffer[80];
  123. DWORD size = sizeof(buffer);
  124. if (!RegQueryValueExA( hKey, driveValue, NULL, NULL, (LPBYTE)buffer, &size ))
  125. {
  126. WINE_TRACE("Got type '%s' for %s\n", buffer, driveValue );
  127. if (!lstrcmpiA( buffer, "hd" )) ret = DRIVE_FIXED;
  128. else if (!lstrcmpiA( buffer, "network" )) ret = DRIVE_REMOTE;
  129. else if (!lstrcmpiA( buffer, "floppy" )) ret = DRIVE_REMOVABLE;
  130. else if (!lstrcmpiA( buffer, "cdrom" )) ret = DRIVE_CDROM;
  131. }
  132. RegCloseKey(hKey);
  133. }
  134. return ret;
  135. }
  136. static void set_drive_label( char letter, const WCHAR *label )
  137. {
  138. static const WCHAR emptyW[1];
  139. WCHAR device[] = {'a',':','\\',0}; /* SetVolumeLabel() requires a trailing slash */
  140. device[0] = letter;
  141. if (!label) label = emptyW;
  142. if(!SetVolumeLabelW(device, label))
  143. {
  144. WINE_WARN("unable to set volume label for devicename of %s, label of %s\n",
  145. wine_dbgstr_w(device), wine_dbgstr_w(label));
  146. PRINTERROR();
  147. }
  148. else
  149. {
  150. WINE_TRACE(" set volume label for devicename of %s, label of %s\n",
  151. wine_dbgstr_w(device), wine_dbgstr_w(label));
  152. }
  153. }
  154. /* set the drive serial number via a .windows-serial file */
  155. static void set_drive_serial( WCHAR letter, DWORD serial )
  156. {
  157. WCHAR filename[] = {'a',':','\\','.','w','i','n','d','o','w','s','-','s','e','r','i','a','l',0};
  158. HANDLE hFile;
  159. filename[0] = letter;
  160. WINE_TRACE("Putting serial number of %08X into file %s\n", serial, wine_dbgstr_w(filename));
  161. hFile = CreateFileW(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
  162. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  163. if (hFile != INVALID_HANDLE_VALUE)
  164. {
  165. DWORD w;
  166. char buffer[16];
  167. sprintf( buffer, "%X\n", serial );
  168. WriteFile(hFile, buffer, strlen(buffer), &w, NULL);
  169. CloseHandle(hFile);
  170. }
  171. }
  172. #if 0
  173. /* currently unused, but if users have this burning desire to be able to rename drives,
  174. we can put it back in.
  175. */
  176. BOOL copyDrive(struct drive *pSrc, struct drive *pDst)
  177. {
  178. if(pDst->in_use)
  179. {
  180. WINE_TRACE("pDst already in use\n");
  181. return FALSE;
  182. }
  183. if(!pSrc->unixpath) WINE_TRACE("!pSrc->unixpath\n");
  184. if(!pSrc->label) WINE_TRACE("!pSrc->label\n");
  185. if(!pSrc->serial) WINE_TRACE("!pSrc->serial\n");
  186. pDst->unixpath = strdupA(pSrc->unixpath);
  187. pDst->label = strdupA(pSrc->label);
  188. pDst->serial = strdupA(pSrc->serial);
  189. pDst->type = pSrc->type;
  190. pDst->in_use = TRUE;
  191. return TRUE;
  192. }
  193. BOOL moveDrive(struct drive *pSrc, struct drive *pDst)
  194. {
  195. WINE_TRACE("pSrc->letter == %c, pDst->letter == %c\n", pSrc->letter, pDst->letter);
  196. if(!copyDrive(pSrc, pDst))
  197. {
  198. WINE_TRACE("copyDrive failed\n");
  199. return FALSE;
  200. }
  201. delete_drive(pSrc);
  202. return TRUE;
  203. }
  204. #endif
  205. static HANDLE open_mountmgr(void)
  206. {
  207. HANDLE ret;
  208. if ((ret = CreateFileW( MOUNTMGR_DOS_DEVICE_NAME, GENERIC_READ|GENERIC_WRITE,
  209. FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
  210. 0, 0 )) == INVALID_HANDLE_VALUE)
  211. WINE_ERR( "failed to open mount manager err %u\n", GetLastError() );
  212. return ret;
  213. }
  214. /* Load currently defined drives into the drives array */
  215. BOOL load_drives(void)
  216. {
  217. DWORD i, size = 1024;
  218. HANDLE mgr;
  219. WCHAR root[] = {'A',':','\\',0};
  220. if ((mgr = open_mountmgr()) == INVALID_HANDLE_VALUE) return FALSE;
  221. while (root[0] <= 'Z')
  222. {
  223. struct mountmgr_unix_drive input;
  224. struct mountmgr_unix_drive *data;
  225. if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) break;
  226. memset( &input, 0, sizeof(input) );
  227. input.letter = root[0];
  228. if (DeviceIoControl( mgr, IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE, &input, sizeof(input),
  229. data, size, NULL, NULL ))
  230. {
  231. char *unixpath = NULL, *device = NULL;
  232. WCHAR volname[MAX_PATH];
  233. DWORD serial;
  234. if (data->mount_point_offset) unixpath = (char *)data + data->mount_point_offset;
  235. if (data->device_offset) device = (char *)data + data->device_offset;
  236. if (!GetVolumeInformationW( root, volname, ARRAY_SIZE(volname),
  237. &serial, NULL, NULL, NULL, 0 ))
  238. {
  239. volname[0] = 0;
  240. serial = 0;
  241. }
  242. if (unixpath) /* FIXME: handle unmounted drives too */
  243. add_drive( root[0], unixpath, device, volname, serial, get_drive_type(root[0]) );
  244. root[0]++;
  245. }
  246. else
  247. {
  248. if (GetLastError() == ERROR_MORE_DATA) size = data->size;
  249. else root[0]++; /* skip this drive */
  250. }
  251. HeapFree( GetProcessHeap(), 0, data );
  252. }
  253. /* reset modified flags */
  254. for (i = 0; i < 26; i++) drives[i].modified = FALSE;
  255. CloseHandle( mgr );
  256. return TRUE;
  257. }
  258. /* some of this code appears to be broken by bugs in Wine: the label
  259. * setting code has no effect, for instance */
  260. void apply_drive_changes(void)
  261. {
  262. int i;
  263. HANDLE mgr;
  264. DWORD len;
  265. struct mountmgr_unix_drive *ioctl;
  266. WINE_TRACE("\n");
  267. if ((mgr = open_mountmgr()) == INVALID_HANDLE_VALUE) return;
  268. /* add each drive and remove as we go */
  269. for(i = 0; i < 26; i++)
  270. {
  271. if (!drives[i].modified) continue;
  272. drives[i].modified = FALSE;
  273. len = sizeof(*ioctl);
  274. if (drives[i].in_use)
  275. {
  276. len += strlen(drives[i].unixpath) + 1;
  277. if (drives[i].device) len += strlen(drives[i].device) + 1;
  278. }
  279. if (!(ioctl = HeapAlloc( GetProcessHeap(), 0, len ))) continue;
  280. ioctl->size = len;
  281. ioctl->letter = 'a' + i;
  282. ioctl->device_offset = 0;
  283. if (drives[i].in_use)
  284. {
  285. char *ptr = (char *)(ioctl + 1);
  286. ioctl->type = drives[i].type;
  287. strcpy( ptr, drives[i].unixpath );
  288. ioctl->mount_point_offset = ptr - (char *)ioctl;
  289. if (drives[i].device)
  290. {
  291. ptr += strlen(ptr) + 1;
  292. strcpy( ptr, drives[i].device );
  293. ioctl->device_offset = ptr - (char *)ioctl;
  294. }
  295. }
  296. else
  297. {
  298. ioctl->type = DRIVE_NO_ROOT_DIR;
  299. ioctl->mount_point_offset = 0;
  300. }
  301. if (DeviceIoControl( mgr, IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE, ioctl, len, NULL, 0, NULL, NULL ))
  302. {
  303. set_drive_label( drives[i].letter, drives[i].label );
  304. if (drives[i].in_use) set_drive_serial( drives[i].letter, drives[i].serial );
  305. WINE_TRACE( "set drive %c: to %s type %u\n", 'a' + i,
  306. wine_dbgstr_a(drives[i].unixpath), drives[i].type );
  307. }
  308. else WINE_WARN( "failed to set drive %c: to %s type %u err %u\n", 'a' + i,
  309. wine_dbgstr_a(drives[i].unixpath), drives[i].type, GetLastError() );
  310. HeapFree( GetProcessHeap(), 0, ioctl );
  311. }
  312. CloseHandle( mgr );
  313. }