Example #1
0
HRESULT CIoSupport::ToggleTray()
{
  if (GetTrayState() == TRAY_OPEN || GetTrayState() == DRIVE_OPEN)
    return CloseTray();
  else
    return EjectTray();
}
Example #2
0
HRESULT CIoSupport::ToggleTray()
{
#if defined(TARGET_WINDOWS)
  return CWIN32Util::ToggleTray();
#else
  if (GetTrayState() == TRAY_OPEN || GetTrayState() == DRIVE_OPEN)
    return CloseTray();
  else
    return EjectTray();
#endif
}
Example #3
0
static int eject_impl(const char *_device, int device_len, int command, zend_bool use_proc_mount) {
    int status = 0;
#ifdef PHP_WIN32
	TCHAR device[128];
	HANDLE fd;
	
	_stprintf_s(device, sizeof(device)/sizeof(TCHAR), _T("%s"), _device);
	fd = CreateFile(device, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
	if (fd == INVALID_HANDLE_VALUE) {
		php_error(E_ERROR, "failed to open device\n");
		return 0;
	}
	
#else
    char *fullName;    /* expanded name */
    int ld = 6;	   /* symbolic link max depth */
    char *linkName;    /* name of device's symbolic link */
    int mounted = 0;   /* true if device is mounted */
    char *mountName;   /* name of device's mount point */
    char *deviceName;  /* name of device */
    int mountable = 0; /* true if device is in /etc/fstab */
    int fd; 	   /* file descriptor for device */\
    
    char *device = estrdup(_device);
    if (!device) {
        php_error(E_ERROR, "failed to allocate memory.\n");
        return 0;
    }
    
    /* Strip any trailing slash from name in case user used bash/tcsh
       style filename completion (e.g. /mnt/cdrom/) */
    if (device[device_len - 1] == '/')
            device[device_len - 1] = 0;
    
    /* figure out full device or mount point name */
    fullName = FindDevice(device);
    if (!fullName) {
            php_error(E_WARNING, "unable to find or open device for: `%s'\n", device);
            return 0;
    }
    
    /* check for a symbolic link */
    while ((linkName = SymLink(fullName)) && (ld > 0)) {
            efree(fullName);
            fullName = strdup(linkName);
            efree(linkName);
            linkName = 0;
            ld--;
    }
    /* handle max depth exceeded option */
    if (ld <= 0) {
            php_error(E_WARNING, "maximum symbolic link depth exceeded: `%s'\n", fullName);
            return 0;
    }
    
    /* if mount point, get device name */
    mounted = MountedDevice(fullName, &mountName, &deviceName, (int)use_proc_mount);
    if (!mounted) {
            deviceName = estrdup(fullName);
    }
    
    /* if not currently mounted, see if it is a possible mount point */
    if (!mounted) {
            mountable = MountableDevice(fullName, &mountName, &deviceName);
    }
    
    fd = OpenDevice(deviceName);
    if (fd == -1) {
        php_error(E_WARNING, "unable to open `%s'\n", fullName);
        return 0;
    }
#endif /* PHP_WIN32 */
    
    switch (command) {
        case EJECT_COMMAND_CLOSE:
            status = CloseTray(fd);
            break;
        case EJECT_COMMAND_TOGGLE:
            status = ToggleTray(fd);
            break;
        default:
            status = 0;
            break;
    }
    
#ifdef PHP_WIN32
	if (fd != INVALID_HANDLE_VALUE) CloseHandle(fd);
#else    
    if (mountName) efree(mountName);
    if (deviceName) efree(deviceName);
#endif /* PHP_WIN32 */
    return status;
}
Example #4
0
static int ToggleTray(int fd)
#endif
{
	struct timeval time_start, time_stop;
	int time_elapsed;

#ifdef PHP_WIN32
	DWORD bytesReturned;
	BOOL result;
	LPVOID lpMsgBuf;
#endif


	/* Try to open the CDROM tray and measure the time therefor
	 * needed.  In my experience the function needs less than 0.05
	 * seconds if the tray was already open, and at least 1.5 seconds
	 * if it was closed.  */
	gettimeofday(&time_start, NULL);
	
#ifdef PHP_WIN32

	bytesReturned = 0;
	result = DeviceIoControl(
		fd,
		IOCTL_STORAGE_EJECT_MEDIA,
		NULL,
		0,
		NULL,
		0,
		&bytesReturned,
		NULL);
	if (!result) {
		
		DWORD dw = GetLastError(); 
		
		FormatMessage(
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM |
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			dw,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(LPTSTR) &lpMsgBuf,
			0, NULL );
		_tprintf(TEXT("%d, %s"), dw, lpMsgBuf);
		php_error(E_ERROR, "%d, %s", dw, lpMsgBuf);
		LocalFree(lpMsgBuf);
		return 0;
	}

#else
	/* Send the CDROMEJECT command to the device. */
	if (ioctl(fd, CDROMEJECT, 0) < 0) {
		perror("ioctl");
		return 0;
	}
#endif /* PHP_WIN32 */

	/* Get the second timestamp, to measure the time needed to open
	 * the tray.  */
	gettimeofday(&time_stop, NULL);

	time_elapsed = (time_stop.tv_sec * 1000000 + time_stop.tv_usec) -
		(time_start.tv_sec * 1000000 + time_start.tv_usec);

	/* If the tray "opened" too fast, we can be nearly sure, that it
	 * was already open. In this case, close it now. Else the tray was
	 * closed before. This would mean that we are done.  */
	if (time_elapsed < TRAY_WAS_ALREADY_OPEN_USECS) {
            return CloseTray(fd);
        } else {
            return 1;
        }
}