?? osal_win32.cpp
字號(hào):
if (DeviceIoControl (handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof (DISK_GEOMETRY), &len, NULL)&&len>0) { *size_in_bytes = geo.BytesPerSector; return (OSAL_OK); } else
{
DWORD nFree=0;
DWORD dwCylinders;
const char *name;
name=osal_get_device_name(handle);
if(name&&GetDiskFreeSpace(name,&geo.SectorsPerTrack,&geo.BytesPerSector,&nFree,&dwCylinders))
{
*size_in_bytes=geo.BytesPerSector;
return (OSAL_OK);
}
}
SetLastError(ERROR_EMPTY); return (OSAL_ERR);}/**************************************************************/intosal_get_volume_sect_size (const char *volume_root, u_int32_t *size_in_bytes){ char volume [10]; /* copy drive letter and a slash - "C:\" */ char *p = volume, *end = volume + sizeof (volume) - 2; while (*volume_root != '\\' && p < end) *p++ = *volume_root++; if (p < end) { DWORD sectors_per_clust, bytes_per_sect, free_clusters, total_clusters; *p++ = '\\'; *p = '\0'; if (GetDiskFreeSpace (volume, §ors_per_clust, &bytes_per_sect, &free_clusters, &total_clusters)) { *size_in_bytes = bytes_per_sect; return (OSAL_OK); } else return (OSAL_ERR); } else { /* if called with no volume name ask for full path */ char full_path [MAX_PATH], *dummy; DWORD result = GetFullPathName (volume_root, MAX_PATH, full_path, &dummy); if (result <= MAX_PATH) return (osal_get_volume_sect_size (full_path, size_in_bytes)); else return (RET_BAD_FORMAT); }}/**************************************************************/intosal_get_file_size (osal_handle_t handle, u_int64_t *size_in_bytes){ LARGE_INTEGER size; if (GetFileSizeEx (handle, &size)) { *size_in_bytes = size.QuadPart; return (OSAL_OK); } else return (OSAL_ERR);}/**************************************************************/intosal_get_file_size_ex (const char *path, u_int64_t *size_in_bytes){ osal_handle_t in; int result = osal_open (path, &in, 1); if (result == OSAL_OK) { result = osal_get_file_size (in, size_in_bytes); osal_close (in); } return (result);}/**************************************************************/intosal_seek (osal_handle_t handle, u_int64_t abs_pos){ LARGE_INTEGER offs; offs.QuadPart = abs_pos; return (SetFilePointerEx (handle, offs, NULL, FILE_BEGIN) ? OSAL_OK : OSAL_ERR);}/**************************************************************/int /* OSAL_OK, OSAL_ERR */osal_read (osal_handle_t handle, void *out, u_int32_t bytes, u_int32_t *stored){ DWORD len; if (ReadFile (handle, out, bytes, &len, NULL)) { *stored = len; return (OSAL_OK); } else return (OSAL_ERR);}/**************************************************************/int /* OSAL_OK, OSAL_ERR */osal_write (osal_handle_t handle, const void *in, u_int32_t bytes, u_int32_t *stored){ DWORD len; if (WriteFile (handle, in, bytes, &len, NULL)) { *stored = len; return (OSAL_OK); } else return (OSAL_ERR);}/**************************************************************/int /* OSAL_OK, OSAL_ERR */osal_close (osal_handle_t handle){
if(handle!=INVALID_HANDLE_VALUE)
UnregisterDevice(handle); return (CloseHandle (handle) ? OSAL_OK : OSAL_ERR);}/**************************************************************/void*osal_alloc (u_int32_t bytes){ return (LocalAlloc (LMEM_FIXED, bytes));}/**************************************************************/voidosal_free (void *ptr){ if (ptr != NULL) LocalFree (ptr);}/**************************************************************/intosal_query_hard_drives (osal_dlist_t **hard_drives){ u_int32_t i; int result; *hard_drives = NULL; result = osal_dlist_alloc (hard_drives); for (i=0; result == RET_OK && i<16; ++i) { char device_name [20]; HANDLE device; sprintf (device_name, "\\\\.\\PhysicalDrive%u", (unsigned int) i); if (osal_open (device_name, &device, TRUE) == OSAL_OK) { /* device exists */ u_int64_t size_in_bytes; sprintf (device_name, "hdd%u:", (unsigned int) i); if (osal_get_estimated_device_size (device, &size_in_bytes) == OSAL_OK) result = osal_dlist_add (*hard_drives, device_name, size_in_bytes, is_apa_partition (device), ERROR_SUCCESS); else result = osal_dlist_add (*hard_drives, device_name, (u_int64_t) 0, 0, GetLastError ()); osal_close (device); } else break; /* first open error is the end of list */ } if (result != RET_OK && *hard_drives != NULL) osal_dlist_free (*hard_drives); return (result);}/**************************************************************/intosal_query_optical_drives (osal_dlist_t **optical_drives){ u_int32_t i; int result; *optical_drives = NULL; result = osal_dlist_alloc (optical_drives); for (i=0; result == RET_OK && i<16; ++i) { char device_name [20]; HANDLE device; sprintf (device_name, "\\\\.\\CdRom%u", (unsigned int) i); if (osal_open (device_name, &device, TRUE) == OSAL_OK) { /* device exists */ u_int64_t size_in_bytes; sprintf (device_name, "cd%u:", (unsigned int) i); if (osal_get_estimated_device_size (device, &size_in_bytes) == OSAL_OK) result = osal_dlist_add (*optical_drives, device_name, size_in_bytes, 0, ERROR_SUCCESS); else result = osal_dlist_add (*optical_drives, device_name, (u_int64_t) 0, 0, GetLastError()); osal_close(device); } else break; /* first open error is the end of list */ } if (result != RET_OK && *optical_drives != NULL) osal_dlist_free (*optical_drives); return (result);}/**************************************************************/intosal_query_devices (osal_dlist_t **hard_drives, osal_dlist_t **optical_drives){ int result = osal_query_hard_drives (hard_drives); if (result == RET_OK) result = osal_query_optical_drives (optical_drives); return (result);}/**************************************************************/static intosal_dlist_alloc (osal_dlist_t **dlist){ *dlist = (osal_dlist_t *)osal_alloc (sizeof (osal_dlist_t)); if (*dlist != NULL) { (*dlist)->allocated = (*dlist)->used = 0; (*dlist)->device = NULL; return (RET_OK); } else return (RET_NO_MEM);}/**************************************************************/static intosal_dlist_add (osal_dlist_t *dlist, const char *name, u_int64_t capacity, int is_ps2, unsigned long status){ osal_dev_t *dev; if (dlist->allocated == dlist->used) { /* allocate memory if necessary */ osal_dev_t *tmp = (osal_dev_t *)osal_alloc ((dlist->allocated + 16) * sizeof (osal_dev_t)); if (tmp != NULL) { if (dlist->device != NULL) { memcpy (tmp, dlist->device, dlist->used * sizeof (osal_dev_t)); osal_free (dlist->device); } dlist->device = tmp; dlist->allocated += 16; } else return (RET_NO_MEM); } /* add the new entry */ dev = dlist->device + dlist->used; strncpy (dev->name, name, DEV_MAX_NAME_LEN); dev->name [DEV_MAX_NAME_LEN - 1] = '\0'; dev->capacity = capacity; dev->is_ps2 = is_ps2; dev->status = status; ++dlist->used; return (RET_OK);}/**************************************************************/voidosal_dlist_free (osal_dlist_t *dlist){ if (dlist != NULL) { if (dlist->device != NULL) osal_free (dlist->device); osal_free (dlist); }}/**************************************************************/int /* RET_OK, RET_BAD_FORMAT, RET_BAD_DEVICE */osal_map_device_name (const char *input, char output [MAX_PATH]){ if (memcmp (input, "hdd", 3) == 0) { char *endp; long index = strtol (input + 3, &endp, 10); if (endp == input + 3) return (RET_BAD_FORMAT); /* bad format: no number after hdd */ if (endp [0] == ':' && endp [1] == '\0') { sprintf (output, "\\\\.\\PhysicalDrive%ld", index); return (RET_OK); } else return (RET_BAD_FORMAT); } else if (memcmp (input, "cd", 2) == 0) { char *endp; long index = strtol (input + 2, &endp, 10); if (endp == input + 2) return (RET_BAD_FORMAT); /* bad format: no number after hdd */ if (endp [0] == ':' && endp [1] == '\0') { sprintf (output, "\\\\.\\CdRom%ld", index); return (RET_OK); } else return (RET_BAD_FORMAT); } else return (RET_BAD_DEVICE);}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -