• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KIOSlave

  • sources
  • kde-4.12
  • kdelibs
  • kioslave
  • file
file_win.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000-2002 Stephan Kulow <coolo@kde.org>
3  Copyright (C) 2000-2002 David Faure <faure@kde.org>
4  Copyright (C) 2000-2002 Waldo Bastian <bastian@kde.org>
5  Copyright (C) 2006 Allan Sandfeld Jensen <sandfeld@kde.org>
6  Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
7  Copyright (C) 2007 Christian Ehrlicher <ch.ehrlicher@gmx.de>
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Library General Public
11  License (LGPL) as published by the Free Software Foundation;
12  either version 2 of the License, or (at your option) any later
13  version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Library General Public License for more details.
19 
20  You should have received a copy of the GNU Library General Public License
21  along with this library; see the file COPYING.LIB. If not, write to
22  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  Boston, MA 02110-1301, USA.
24 */
25 
26 #include "file.h"
27 
28 #include <windows.h>
29 
30 #include <QtCore/QDir>
31 #include <QtCore/QDirIterator>
32 #include <QtCore/QFileInfo>
33 
34 #include <config.h>
35 
36 #include <kconfiggroup.h>
37 #include <kdebug.h>
38 
39 using namespace KIO;
40 
41 static DWORD CALLBACK CopyProgressRoutine(
42  LARGE_INTEGER TotalFileSize,
43  LARGE_INTEGER TotalBytesTransferred,
44  LARGE_INTEGER StreamSize,
45  LARGE_INTEGER StreamBytesTransferred,
46  DWORD dwStreamNumber,
47  DWORD dwCallbackReason,
48  HANDLE hSourceFile,
49  HANDLE hDestinationFile,
50  LPVOID lpData
51 ) {
52  FileProtocol *f = reinterpret_cast<FileProtocol*>(lpData);
53  f->processedSize( TotalBytesTransferred.QuadPart );
54  return PROGRESS_CONTINUE;
55 }
56 
57 static UDSEntry createUDSEntryWin( const QFileInfo &fileInfo )
58 {
59  UDSEntry entry;
60 
61  entry.insert( KIO::UDSEntry::UDS_NAME, fileInfo.fileName() );
62  if( fileInfo.isSymLink() ) {
63  entry.insert( KIO::UDSEntry::UDS_TARGET_URL, fileInfo.symLinkTarget() );
64 /* TODO - or not useful on windows?
65  if ( details > 1 ) {
66  // It is a link pointing to nowhere
67  type = S_IFMT - 1;
68  access = S_IRWXU | S_IRWXG | S_IRWXO;
69 
70  entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, type );
71  entry.insert( KIO::UDSEntry::UDS_ACCESS, access );
72  entry.insert( KIO::UDSEntry::UDS_SIZE, 0LL );
73  goto notype;
74 
75  }
76 */
77  }
78  int type = S_IFREG;
79  int access = 0;
80  if( fileInfo.isDir() )
81  type = S_IFDIR;
82  else if( fileInfo.isSymLink() )
83  type = S_IFLNK;
84  if( fileInfo.isReadable() )
85  access |= S_IRUSR;
86  if( fileInfo.isWritable() )
87  access |= S_IWUSR;
88  if( fileInfo.isExecutable() )
89  access |= S_IXUSR;
90 
91  entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, type );
92  entry.insert( KIO::UDSEntry::UDS_ACCESS, access );
93  entry.insert( KIO::UDSEntry::UDS_SIZE, fileInfo.size() );
94  if( fileInfo.isHidden() )
95  entry.insert( KIO::UDSEntry::UDS_HIDDEN, true );
96 
97  entry.insert( KIO::UDSEntry::UDS_MODIFICATION_TIME, fileInfo.lastModified().toTime_t() );
98  entry.insert( KIO::UDSEntry::UDS_USER, fileInfo.owner() );
99  entry.insert( KIO::UDSEntry::UDS_GROUP, fileInfo.group() );
100  entry.insert( KIO::UDSEntry::UDS_ACCESS_TIME, fileInfo.lastRead().toTime_t() );
101 
102  return entry;
103 }
104 
105 void FileProtocol::copy( const KUrl &src, const KUrl &dest,
106  int _mode, JobFlags _flags )
107 {
108  kDebug(7101) << "copy(): " << src << " -> " << dest << ", mode=" << _mode;
109 
110  QFileInfo _src(src.toLocalFile());
111  QFileInfo _dest(dest.toLocalFile());
112  DWORD dwFlags = COPY_FILE_FAIL_IF_EXISTS;
113 
114  if( _src == _dest ) {
115  error( KIO::ERR_IDENTICAL_FILES, _dest.filePath() );
116  return;
117  }
118 
119  if( !_src.exists() ) {
120  error( KIO::ERR_DOES_NOT_EXIST, _src.filePath() );
121  return;
122  }
123 
124  if ( _src.isDir() ) {
125  error( KIO::ERR_IS_DIRECTORY, _src.filePath() );
126  return;
127  }
128 
129  if( _dest.exists() ) {
130  if( _dest.isDir() ) {
131  error( KIO::ERR_DIR_ALREADY_EXIST, _dest.filePath() );
132  return;
133  }
134 
135  if (!(_flags & KIO::Overwrite))
136  {
137  error( KIO::ERR_FILE_ALREADY_EXIST, _dest.filePath() );
138  return;
139  }
140 
141  dwFlags = 0;
142  }
143 
144  if( !QFileInfo(_dest.dir().absolutePath()).exists() )
145  {
146  _dest.dir().mkdir(_dest.dir().absolutePath());
147  }
148 
149  if ( CopyFileExW( ( LPCWSTR ) _src.filePath().utf16(),
150  ( LPCWSTR ) _dest.filePath().utf16(),
151  CopyProgressRoutine,
152  ( LPVOID ) this,
153  FALSE,
154  dwFlags) == 0 )
155  {
156  DWORD dwLastErr = GetLastError();
157  if ( dwLastErr == ERROR_FILE_NOT_FOUND )
158  error( KIO::ERR_DOES_NOT_EXIST, _src.filePath() );
159  else if ( dwLastErr == ERROR_ACCESS_DENIED )
160  error( KIO::ERR_ACCESS_DENIED, _dest.filePath() );
161  else {
162 #if 0
163  LPVOID lpMsgBuf;
164 
165  FormatMessage(
166  FORMAT_MESSAGE_ALLOCATE_BUFFER |
167  FORMAT_MESSAGE_FROM_SYSTEM |
168  FORMAT_MESSAGE_IGNORE_INSERTS,
169  NULL,
170  dwLastErr,
171  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
172  (LPTSTR) &lpMsgBuf,
173  0, NULL );
174  OutputDebugString((WCHAR*)lpMsgBuf);
175 #endif
176  error( KIO::ERR_CANNOT_RENAME, _src.filePath() );
177  kDebug( 7101 ) << "Copying file "
178  << _src.filePath()
179  << " failed ("
180  << dwLastErr << ")";
181  }
182  return;
183  }
184 
185  finished();
186 }
187 
188 void FileProtocol::listDir( const KUrl& url )
189 {
190  kDebug(7101) << "========= LIST " << url.url() << " =========";
191 
192  if (!url.isLocalFile()) {
193  KUrl redir(url);
194  redir.setProtocol(config()->readEntry("DefaultRemoteProtocol", "smb"));
195  redirection(redir);
196  kDebug(7101) << "redirecting to " << redir.url();
197  finished();
198  return;
199  }
200 
201  QDir dir( url.toLocalFile() );
202  dir.setFilter( QDir::AllEntries|QDir::Hidden );
203 
204  if ( !dir.exists() ) {
205  kDebug(7101) << "========= ERR_DOES_NOT_EXIST =========";
206  error( KIO::ERR_DOES_NOT_EXIST, url.toLocalFile() );
207  return;
208  }
209 
210  if ( !dir.isReadable() ) {
211  kDebug(7101) << "========= ERR_CANNOT_ENTER_DIRECTORY =========";
212  error( KIO::ERR_CANNOT_ENTER_DIRECTORY, url.toLocalFile() );
213  return;
214  }
215  QDirIterator it( dir );
216  UDSEntry entry;
217  while( it.hasNext() ) {
218  it.next();
219  UDSEntry entry = createUDSEntryWin( it.fileInfo() );
220 
221  listEntry( entry, false );
222  entry.clear();
223  }
224 
225  listEntry( entry, true ); // ready
226 
227  kDebug(7101) << "============= COMPLETED LIST ============";
228 
229  finished();
230 }
231 
232 void FileProtocol::rename( const KUrl &src, const KUrl &dest,
233  KIO::JobFlags _flags )
234 {
235  kDebug(7101) << "rename(): " << src << " -> " << dest;
236 
237  QFileInfo _src(src.toLocalFile());
238  QFileInfo _dest(dest.toLocalFile());
239  DWORD dwFlags = 0;
240 
241  if( _src == _dest ) {
242  error( KIO::ERR_IDENTICAL_FILES, _dest.filePath() );
243  return;
244  }
245 
246  if( !_src.exists() ) {
247  error( KIO::ERR_DOES_NOT_EXIST, _src.filePath() );
248  return;
249  }
250 
251  if( _dest.exists() ) {
252  if( _dest.isDir() ) {
253  error( KIO::ERR_DIR_ALREADY_EXIST, _dest.filePath() );
254  return;
255  }
256 
257  if (!(_flags & KIO::Overwrite))
258  {
259  error( KIO::ERR_FILE_ALREADY_EXIST, _dest.filePath() );
260  return;
261  }
262 
263 #ifndef _WIN32_WCE
264  dwFlags = MOVEFILE_REPLACE_EXISTING;
265 #endif
266  }
267  // To avoid error 17 - The system cannot move the file to a different disk drive.
268 #ifndef _WIN32_WCE
269  dwFlags |= MOVEFILE_COPY_ALLOWED;
270 
271 
272  if ( MoveFileExW( ( LPCWSTR ) _src.filePath().utf16(),
273  ( LPCWSTR ) _dest.filePath().utf16(), dwFlags) == 0 )
274 #else
275  if ( MoveFileW( ( LPCWSTR ) _src.filePath().utf16(),
276  ( LPCWSTR ) _dest.filePath().utf16()) == 0 )
277 #endif
278  {
279  DWORD dwLastErr = GetLastError();
280  if ( dwLastErr == ERROR_FILE_NOT_FOUND )
281  error( KIO::ERR_DOES_NOT_EXIST, _src.filePath() );
282  else if ( dwLastErr == ERROR_ACCESS_DENIED )
283  error( KIO::ERR_ACCESS_DENIED, _dest.filePath() );
284  else {
285  error( KIO::ERR_CANNOT_RENAME, _src.filePath() );
286  kDebug( 7101 ) << "Renaming file "
287  << _src.filePath()
288  << " failed ("
289  << dwLastErr << ")";
290  }
291  return;
292  }
293 
294  finished();
295 }
296 
297 void FileProtocol::symlink( const QString &target, const KUrl &dest, KIO::JobFlags flags )
298 {
299  // no symlink on windows for now
300  // vista provides a CreateSymbolicLink() function. for now use ::copy
301  FileProtocol::copy( target, dest, 0, flags );
302 }
303 
304 void FileProtocol::del( const KUrl& url, bool isfile )
305 {
306  QString _path( url.toLocalFile() );
307  /*****
308  * Delete files
309  *****/
310 
311  if (isfile) {
312  kDebug( 7101 ) << "Deleting file " << _path;
313 
314  if( DeleteFileW( ( LPCWSTR ) _path.utf16() ) == 0 ) {
315  DWORD dwLastErr = GetLastError();
316  if ( dwLastErr == ERROR_PATH_NOT_FOUND )
317  error( KIO::ERR_DOES_NOT_EXIST, _path );
318  else if( dwLastErr == ERROR_ACCESS_DENIED )
319  error( KIO::ERR_ACCESS_DENIED, _path );
320  else {
321  error( KIO::ERR_CANNOT_DELETE, _path );
322  kDebug( 7101 ) << "Deleting file "
323  << _path
324  << " failed ("
325  << dwLastErr << ")";
326  }
327  }
328  } else {
329  kDebug( 7101 ) << "Deleting directory " << _path;
330  if (!deleteRecursive(_path))
331  return;
332  if( RemoveDirectoryW( ( LPCWSTR ) _path.utf16() ) == 0 ) {
333  DWORD dwLastErr = GetLastError();
334  if ( dwLastErr == ERROR_FILE_NOT_FOUND )
335  error( KIO::ERR_DOES_NOT_EXIST, _path );
336  else if( dwLastErr == ERROR_ACCESS_DENIED )
337  error( KIO::ERR_ACCESS_DENIED, _path );
338  else {
339  error( KIO::ERR_CANNOT_DELETE, _path );
340  kDebug( 7101 ) << "Deleting directory "
341  << _path
342  << " failed ("
343  << dwLastErr << ")";
344  }
345  }
346  }
347  finished();
348 }
349 
350 void FileProtocol::chown( const KUrl& url, const QString&, const QString& )
351 {
352  error( KIO::ERR_CANNOT_CHOWN, url.toLocalFile() );
353 }
354 
355 void FileProtocol::stat( const KUrl & url )
356 {
357  if (!url.isLocalFile()) {
358  KUrl redir(url);
359  redir.setProtocol(config()->readEntry("DefaultRemoteProtocol", "smb"));
360  redirection(redir);
361  kDebug(7101) << "redirecting to " << redir.url();
362  finished();
363  return;
364  }
365 
366  const QString sDetails = metaData(QLatin1String("details"));
367  int details = sDetails.isEmpty() ? 2 : sDetails.toInt();
368  kDebug(7101) << "FileProtocol::stat details=" << details;
369 
370  UDSEntry entry = createUDSEntryWin( QFileInfo(url.toLocalFile()) );
371 
372  statEntry( entry );
373 
374  finished();
375 }
KIO::Overwrite
readEntry
KAutostart::StartPhase readEntry(const KConfigGroup &group, const char *key, const KAutostart::StartPhase &aDefault)
KIO::UDSEntry::UDS_TARGET_URL
kdebug.h
FileProtocol::listDir
virtual void listDir(const KUrl &url)
Definition: file_unix.cpp:309
KIO::UDSEntry::clear
void clear()
KIO::UDSEntry
FileProtocol::symlink
virtual void symlink(const QString &target, const KUrl &dest, KIO::JobFlags flags)
Definition: file_unix.cpp:481
KIO::UDSEntry::insert
void insert(uint field, const QString &value)
KIO::ERR_CANNOT_DELETE
KIO::UDSEntry::UDS_FILE_TYPE
KIO::ERR_FILE_ALREADY_EXIST
createUDSEntryWin
static UDSEntry createUDSEntryWin(const QFileInfo &fileInfo)
Definition: file_win.cpp:57
KUrl::toLocalFile
QString toLocalFile(AdjustPathOption trailing=LeaveTrailingSlash) const
FileProtocol::chown
virtual void chown(const KUrl &url, const QString &owner, const QString &group)
Definition: file_unix.cpp:566
QString
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
file.h
KUrl
KIO::UDSEntry::UDS_HIDDEN
config
KSharedConfigPtr config()
FileProtocol
Definition: file.h:46
FileProtocol::copy
virtual void copy(const KUrl &src, const KUrl &dest, int mode, KIO::JobFlags flags)
Definition: file_unix.cpp:78
FileProtocol::rename
virtual void rename(const KUrl &src, const KUrl &dest, KIO::JobFlags flags)
Definition: file_unix.cpp:419
KIO::UDSEntry::UDS_USER
KIO::ERR_IS_DIRECTORY
KIO::ERR_CANNOT_RENAME
KIO::UDSEntry::UDS_ACCESS_TIME
KIO::ERR_IDENTICAL_FILES
KIO::ERR_DIR_ALREADY_EXIST
KIO::UDSEntry::UDS_MODIFICATION_TIME
KIO::ERR_ACCESS_DENIED
CopyProgressRoutine
static DWORD CALLBACK CopyProgressRoutine(LARGE_INTEGER TotalFileSize, LARGE_INTEGER TotalBytesTransferred, LARGE_INTEGER StreamSize, LARGE_INTEGER StreamBytesTransferred, DWORD dwStreamNumber, DWORD dwCallbackReason, HANDLE hSourceFile, HANDLE hDestinationFile, LPVOID lpData)
Definition: file_win.cpp:41
KIO::ERR_CANNOT_CHOWN
FileProtocol::stat
virtual void stat(const KUrl &url)
Definition: file_unix.cpp:615
dir
QString dir(const QString &fileClass)
KIO::UDSEntry::UDS_ACCESS
access
int access(const QString &path, int mode)
KIO::UDSEntry::UDS_NAME
KIO::UDSEntry::UDS_GROUP
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
FileProtocol::del
virtual void del(const KUrl &url, bool isfile)
Definition: file_unix.cpp:521
KIO::ERR_DOES_NOT_EXIST
KIO::ERR_CANNOT_ENTER_DIRECTORY
KUrl::isLocalFile
bool isLocalFile() const
KIO::UDSEntry::UDS_SIZE
kconfiggroup.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIOSlave

Skip menu "KIOSlave"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal