Warning, file /utilities/isoimagewriter/isoimagewriter/physicaldevice.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2016 ROSA
0003     SPDX-License-Identifier: GPL-3.0-or-later
0004 */
0005 
0006 ////////////////////////////////////////////////////////////////////////////////
0007 // Implementation of PhysicalDevice
0008 
0009 #include <KLocalizedString>
0010 
0011 #include "physicaldevice.h"
0012 
0013 PhysicalDevice::PhysicalDevice(const QString& name) :
0014     QFile(name)
0015 {
0016 }
0017 
0018 // Opens the selected device in WriteOnly mode
0019 bool PhysicalDevice::open()
0020 {
0021 #if defined(Q_OS_WIN32)
0022     DWORD bret;
0023 
0024     // In Windows QFile with write mode uses disposition OPEN_ALWAYS, but WinAPI
0025     // requires OPEN_EXISTING for physical devices. Therefore we have to use native API.
0026     m_fileHandle = CreateFile(
0027         reinterpret_cast<const wchar_t*>(fileName().utf16()),
0028         GENERIC_WRITE,
0029         FILE_SHARE_READ | FILE_SHARE_WRITE,
0030         NULL,
0031         OPEN_EXISTING,
0032         FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING,
0033         NULL
0034     );
0035     if (m_fileHandle == INVALID_HANDLE_VALUE)
0036     {
0037         setErrorString(errorMessageFromCode());
0038         return false;
0039     }
0040     // Lock the opened device
0041     if (!DeviceIoControl(m_fileHandle, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &bret, NULL))
0042     {
0043         setErrorString(formatErrorMessageFromCode(i18n("Could not acquire lock:")));
0044         return false;
0045     }
0046     // Construct QFile around the device handle; close() will now close the handle automatically
0047     if (QFile::open(_open_osfhandle(reinterpret_cast<intptr_t>(m_fileHandle), 0), QIODevice::WriteOnly | QIODevice::Unbuffered, AutoCloseHandle))
0048         return true;
0049     else
0050     {
0051         CloseHandle(m_fileHandle);
0052         return false;
0053     }
0054 #elif defined(Q_OS_LINUX) || defined(Q_OS_MAC)
0055     // Simply use QFile, it works fine in Linux
0056     // TODO: Use system call open with O_DIRECT
0057     return QFile::open(QIODevice::WriteOnly);
0058 #else
0059     return false;
0060 #endif
0061 }