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

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • kdecore
  • util
kde_file_win.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the KDE libraries
3  Copyright (C) 2004 JarosÅ‚aw Staniek <staniek@kde.org>
4  Copyright (C) 2009 Christian Ehrlicher <ch.ehrlicher@gmx.de>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 // needed for _wstat64
22 #define __MSVCRT_VERSION__ 0x601
23 
24 #include "kde_file.h"
25 
26 #include <QtCore/QFile>
27 #include <errno.h>
28 
29 #include <sys/utime.h>
30 #include <sys/stat.h>
31 #include <wchar.h>
32 #define CONV(x) ((wchar_t*)x.utf16())
33 
35 static int kdewin_fix_mode_string(char *fixed_mode, const char *mode)
36 {
37  if (strlen(mode)<1 || strlen(mode)>3) {
38  errno = EINVAL;
39  return 1;
40  }
41 
42  strncpy(fixed_mode, mode, 3);
43  if (fixed_mode[0]=='b' || fixed_mode[1]=='b' || fixed_mode[0]=='t' || fixed_mode[1]=='t')
44  return 0;
45  /* no 't' or 'b': append 'b' */
46  if (fixed_mode[1]=='+') {
47  fixed_mode[1]='b';
48  fixed_mode[2]='+';
49  fixed_mode[3]=0;
50  }
51  else {
52  fixed_mode[1]='b';
53  fixed_mode[2]=0;
54  }
55  return 0;
56 }
57 
59 static int kdewin_fix_flags(int flags)
60 {
61  if ((flags & O_TEXT) == 0 && (flags & O_BINARY) == 0)
62  return flags | O_BINARY;
63  return flags;
64 }
65 
66 /* from kdefakes library
67  Generate a unique temporary directory name from TEMPLATE.
68 
69  TEMPLATE has the form:
70 
71  <path>/ccXXXXXX
72 
73 
74  The last six characters of TEMPLATE must be "XXXXXX";
75  they are replaced with a string that makes the filename unique.
76 
77  Returns a file descriptor open on the file for reading and writing. */
78 
79 QString mkdtemp_QString (const QString &_template)
80 {
81  static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
82  char XXXXXX[7];
83  int value;
84 
85  if ( !_template.endsWith(QLatin1String("XXXXXX")) )
86  return QString();
87 
88  strcpy(XXXXXX, "XXXXXX");
89  const QString tmpl = _template.left(_template.length() - 6);
90 
91  value = rand();
92  for (int count = 0; count < 256; ++count)
93  {
94  int v = value;
95 
96  /* Fill in the random bits. */
97  XXXXXX[0] = letters[v % 62];
98  v /= 62;
99  XXXXXX[1] = letters[v % 62];
100  v /= 62;
101  XXXXXX[2] = letters[v % 62];
102  v /= 62;
103  XXXXXX[3] = letters[v % 62];
104  v /= 62;
105  XXXXXX[4] = letters[v % 62];
106  v /= 62;
107  XXXXXX[5] = letters[v % 62];
108 
109  /* This is a random value. It is only necessary that the next
110  TMP_MAX values generated by adding 7777 to VALUE are different
111  with (module 2^32). */
112  value += 7777;
113 
114  const QString tmp = tmpl + QString::fromLatin1( XXXXXX );
115  if (!KDE::mkdir(tmp,0700))
116  return tmp;
117  }
118  return QString();
119 }
120 
121 namespace KDE
122 {
123  int access(const QString &path, int mode)
124  {
125  int x_mode = 0;
126  // X_OK gives an assert on msvc2005 and up - use stat() instead
127  if( ( mode & X_OK ) == X_OK ) {
128  KDE_struct_stat st;
129  if( KDE::stat( path, &st ) != 0 )
130  return 1;
131  if( ( st.st_mode & S_IXUSR ) != S_IXUSR )
132  return 1;
133  }
134  mode &= ~X_OK;
135  return _waccess( CONV(path), mode );
136  }
137 
138  int chmod(const QString &path, mode_t mode)
139  {
140  return _wchmod( CONV(path), mode );
141  }
142 
143  FILE *fopen(const QString &pathname, const char *mode)
144  {
145  return _wfopen( CONV(pathname), CONV(QString::fromLatin1( mode )) );
146  }
147 
148  int lstat(const QString &path, KDE_struct_stat *buf)
149  {
150  return KDE::stat( path, buf );
151  }
152 
153  int mkdir(const QString &pathname, mode_t)
154  {
155  return _wmkdir( CONV(pathname) );
156  }
157 
158  int open(const QString &pathname, int flags, mode_t mode)
159  {
160  return _wopen( CONV(pathname), kdewin_fix_flags(flags), mode );
161  }
162 
163  int rename(const QString &in, const QString &out)
164  {
165  // better than :waccess/_wunlink/_wrename
166 #ifndef _WIN32_WCE
167  bool ok = ( MoveFileExW( CONV(in), CONV(out),
168  MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED ) != 0 );
169 #else
170  bool ok = ( MoveFileW( CONV(in), CONV(out)) != 0 );
171 #endif
172  return ok ? 0 : -1;
173  }
174 
175  int stat(const QString &path, KDE_struct_stat *buf)
176  {
177  int result;
178 #ifdef Q_CC_MSVC
179 #ifndef _WIN32_WCE
180  struct _stat64 s64;
181 #else
182  struct stat st;
183 #endif
184 #else
185  struct __stat64 s64;
186 #endif
187  const int len = path.length();
188  if ( (len==2 || len==3) && path[1]==QLatin1Char(':') && path[0].isLetter() ) {
189  /* 1) */
190  QString newPath(path);
191  if (len==2)
192  newPath += QLatin1Char('\\');
193 #ifndef _WIN32_WCE
194  result = _wstat64( CONV(newPath), &s64 );
195 #else
196  result = wstat( CONV(newPath), &st );
197 #endif
198  } else
199  if ( len > 1 && (path.endsWith(QLatin1Char('\\')) || path.endsWith(QLatin1Char('/'))) ) {
200  /* 2) */
201  const QString newPath = path.left( len - 1 );
202 #ifndef _WIN32_WCE
203  result = _wstat64( CONV(newPath), &s64 );
204 #else
205  result = wstat( CONV(newPath), &st );
206 #endif
207  } else {
208  //TODO: is stat("/") ok?
209 #ifndef _WIN32_WCE
210  result = _wstat64( CONV(path), &s64 );
211 #else
212  result = wstat( CONV(path), &st );
213 #endif
214  }
215  if( result != 0 )
216  return result;
217  // KDE5: fixme!
218 #ifndef _WIN32_WCE
219  buf->st_dev = s64.st_dev;
220  buf->st_ino = s64.st_ino;
221  buf->st_mode = s64.st_mode;
222  buf->st_nlink = s64.st_nlink;
223 #else
224  buf->st_dev = st.st_dev;
225  buf->st_ino = st.st_ino;
226  buf->st_mode = st.st_mode;
227  buf->st_nlink = st.st_nlink;
228 #endif
229  buf->st_uid = -2; // be in sync with Qt4
230  buf->st_gid = -2; // be in sync with Qt4
231 #ifndef _WIN32_WCE
232  buf->st_rdev = s64.st_rdev;
233  buf->st_size = s64.st_size;
234  buf->st_atime = s64.st_atime;
235  buf->st_mtime = s64.st_mtime;
236  buf->st_ctime = s64.st_ctime;
237 #else
238  buf->st_rdev = st.st_rdev;
239  buf->st_size = st.st_size;
240  buf->st_atime = st.st_atime;
241  buf->st_mtime = st.st_mtime;
242  buf->st_ctime = st.st_ctime;
243 #endif
244  return result;
245  }
246  int utime(const QString &filename, struct utimbuf *buf)
247  {
248 #ifndef _WIN32_WCE
249  return _wutime( CONV(filename), (struct _utimbuf*)buf );
250 #else
251  return _wutime( CONV(filename), (struct utimbuf*)buf );
252 #endif
253  }
254 };
kdewin_fix_mode_string
static int kdewin_fix_mode_string(char *fixed_mode, const char *mode)
Definition: kde_file_win.cpp:35
CONV
#define CONV(x)
Definition: kde_file_win.cpp:32
KDE::mkdir
int mkdir(const QString &pathname, mode_t)
Definition: kde_file_win.cpp:153
KDE::stat
int stat(const QString &path, KDE_struct_stat *buf)
Definition: kde_file_win.cpp:175
KDE::rename
int rename(const QString &in, const QString &out)
Definition: kde_file_win.cpp:163
KDE::chmod
int chmod(const QString &path, mode_t mode)
Definition: kde_file_win.cpp:138
mkdtemp_QString
QString mkdtemp_QString(const QString &_template)
Definition: kde_file_win.cpp:79
KDE::open
int open(const QString &pathname, int flags, mode_t mode)
Definition: kde_file_win.cpp:158
kdewin_fix_flags
static int kdewin_fix_flags(int flags)
Definition: kde_file_win.cpp:59
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QString
QLatin1Char
KDE::lstat
int lstat(const QString &path, KDE_struct_stat *buf)
Definition: kde_file_win.cpp:148
QLatin1String
KDE::access
int access(const QString &path, int mode)
Definition: kde_file_win.cpp:123
QString::length
int length() const
QString::left
QString left(int n) const
QString::fromLatin1
QString fromLatin1(const char *str, int size)
KDE::fopen
FILE * fopen(const QString &pathname, const char *mode)
Definition: kde_file_win.cpp:143
KDE::utime
int utime(const QString &filename, struct utimbuf *buf)
Definition: kde_file_win.cpp:246
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • 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