KIO

kioglobal_p_win.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2014 Alex Richardson <arichardson.kde@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.0-only
6*/
7#include "kioglobal_p.h"
8
9#include <QDebug>
10#include <QFile>
11#include <QFileInfo>
12
13#include <qt_windows.h>
14
15KIOCORE_EXPORT bool KIOPrivate::isProcessAlive(qint64 pid)
16{
17 HANDLE procHandle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
18 bool alive = false;
19 if (procHandle != INVALID_HANDLE_VALUE) {
20 DWORD exitCode;
21 if (GetExitCodeProcess(procHandle, &exitCode)) {
22 alive = exitCode == STILL_ACTIVE;
23 }
24 CloseHandle(procHandle);
25 }
26 return alive;
27}
28
29// A callback to shutdown cleanly (no forced kill)
30BOOL CALLBACK closeProcessCallback(HWND hwnd, LPARAM lParam)
31{
32 DWORD id;
33 GetWindowThreadProcessId(hwnd, &id);
34 if (id == (DWORD)lParam) {
35 PostMessage(hwnd, WM_CLOSE, 0, 0);
36 }
37 return TRUE;
38}
39
40KIOCORE_EXPORT void KIOPrivate::sendTerminateSignal(qint64 pid)
41{
42 // no error checking whether kill succeeded, Linux code also just sends a SIGTERM without checking
43 HANDLE procHandle = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, FALSE, pid);
44 if (procHandle != INVALID_HANDLE_VALUE) {
45 EnumWindows(&closeProcessCallback, (LPARAM)pid);
46 CloseHandle(procHandle);
47 }
48}
49
50KIOCORE_EXPORT bool KIOPrivate::createSymlink(const QString &source, const QString &destination, KIOPrivate::SymlinkType type)
51{
52#if _WIN32_WINNT >= 0x600
53 DWORD flag;
54 if (type == KIOPrivate::DirectorySymlink) {
55 flag = SYMBOLIC_LINK_FLAG_DIRECTORY;
56 } else if (type == KIOPrivate::FileSymlink) {
57 flag = 0;
58 } else {
59 // Guess the type of the symlink based on the source path
60 // If the source is a directory we set the flag SYMBOLIC_LINK_FLAG_DIRECTORY, for files
61 // and non-existent paths we create a symlink to a file
62 DWORD sourceAttrs = GetFileAttributesW((LPCWSTR)source.utf16());
63 if (sourceAttrs != INVALID_FILE_ATTRIBUTES && (sourceAttrs & FILE_ATTRIBUTE_DIRECTORY)) {
64 flag = SYMBOLIC_LINK_FLAG_DIRECTORY;
65 } else {
66 flag = 0;
67 }
68 }
69 bool ok = CreateSymbolicLinkW((LPCWSTR)destination.utf16(), (LPCWSTR)source.utf16(), flag);
70 if (!ok) {
71 // create a .lnk file
72 ok = QFile::link(source, destination);
73 }
74 return ok;
75#else
76 qWarning("KIOPrivate::createSymlink: not implemented");
77 return false;
78#endif
79}
80
81KIOCORE_EXPORT int kio_windows_lstat(const char *path, QT_STATBUF *buffer)
82{
83 int result = QT_STAT(path, buffer);
84 if (result != 0) {
85 return result;
86 }
87 const QString pathStr = QFile::decodeName(path);
88 // QFileInfo currently only checks for .lnk file in isSymlink() -> also check native win32 symlinks
89 const DWORD fileAttrs = GetFileAttributesW((LPCWSTR)pathStr.utf16());
90 if (fileAttrs != INVALID_FILE_ATTRIBUTES && (fileAttrs & FILE_ATTRIBUTE_REPARSE_POINT) || QFileInfo(pathStr).isSymLink()) {
91 buffer->st_mode |= QT_STAT_LNK;
92 }
93 return result;
94}
95
96KIOCORE_EXPORT bool KIOPrivate::changeOwnership(const QString &file, KUserId newOwner, KGroupId newGroup)
97{
98#pragma message("TODO")
99 qWarning("KIOPrivate::changeOwnership: not implemented yet");
100 return false;
101}
QString decodeName(const QByteArray &localFileName)
const ushort * utf16() const const
typedef HANDLE
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:18:51 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.