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

KDECore

kde_file_win.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE libraries
00003    Copyright (C) 2004 Jarosław Staniek <staniek@kde.org>
00004    Copyright (C) 2009 Christian Ehrlicher <ch.ehrlicher@gmx.de>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // needed for _wstat64
00022 #define __MSVCRT_VERSION__ 0x601
00023 
00024 #include "kde_file.h"
00025 
00026 #include <QtCore/QFile>
00027 #include <errno.h>
00028 
00029 #include <sys/utime.h>
00030 #include <sys/stat.h>
00031 #include <wchar.h>
00032 #define CONV(x) ((wchar_t*)x.utf16())
00033 
00035 static int kdewin_fix_mode_string(char *fixed_mode, const char *mode)
00036 {
00037     if (strlen(mode)<1 || strlen(mode)>3) {
00038         errno = EINVAL;
00039         return 1;
00040     }
00041 
00042     strncpy(fixed_mode, mode, 3);
00043     if (fixed_mode[0]=='b' || fixed_mode[1]=='b' || fixed_mode[0]=='t' || fixed_mode[1]=='t')
00044         return 0;
00045     /* no 't' or 'b': append 'b' */
00046     if (fixed_mode[1]=='+') {
00047         fixed_mode[1]='b';
00048         fixed_mode[2]='+';
00049         fixed_mode[3]=0;
00050     }
00051     else {
00052         fixed_mode[1]='b';
00053         fixed_mode[2]=0;
00054     }
00055     return 0;
00056 }
00057 
00059 static int kdewin_fix_flags(int flags)
00060 {
00061     if ((flags & O_TEXT) == 0 && (flags & O_BINARY) == 0)
00062         return flags | O_BINARY;
00063     return flags;
00064 }
00065 
00066 /* from kdefakes library
00067    Generate a unique temporary directory name from TEMPLATE.
00068 
00069    TEMPLATE has the form:
00070 
00071    <path>/ccXXXXXX
00072 
00073 
00074    The last six characters of TEMPLATE must be "XXXXXX";
00075    they are replaced with a string that makes the filename unique.
00076 
00077    Returns a file descriptor open on the file for reading and writing.  */
00078 
00079 QString mkdtemp_QString (const QString &_template)
00080 {
00081   static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
00082   char XXXXXX[7];
00083   int value;
00084 
00085   if ( !_template.endsWith(QLatin1String("XXXXXX")) )
00086       return 0;
00087 
00088   strcpy(XXXXXX, "XXXXXX");
00089   const QString tmpl = _template.left(_template.length() - 6);
00090 
00091   value = rand();
00092   for (int count = 0; count < 256; ++count)
00093   {
00094       int v = value;
00095 
00096       /* Fill in the random bits.  */
00097       XXXXXX[0] = letters[v % 62];
00098       v /= 62;
00099       XXXXXX[1] = letters[v % 62];
00100       v /= 62;
00101       XXXXXX[2] = letters[v % 62];
00102       v /= 62;
00103       XXXXXX[3] = letters[v % 62];
00104       v /= 62;
00105       XXXXXX[4] = letters[v % 62];
00106       v /= 62;
00107       XXXXXX[5] = letters[v % 62];
00108 
00109       /* This is a random value.  It is only necessary that the next
00110          TMP_MAX values generated by adding 7777 to VALUE are different
00111          with (module 2^32).  */
00112       value += 7777;
00113 
00114       const QString tmp = tmpl + QString::fromAscii( XXXXXX );
00115       if (!KDE::mkdir(tmp,0700))
00116           return tmp;
00117   }
00118   return QString();
00119 }
00120 
00121 namespace KDE
00122 {
00123   int access(const QString &path, int mode)
00124   {
00125     int x_mode = 0;
00126     // X_OK gives an assert on msvc2005 and up - use stat() instead
00127     if( ( mode & X_OK ) == X_OK ) {
00128         KDE_struct_stat st;
00129         if( KDE::stat( path, &st ) != 0 )
00130           return 1;
00131         if( ( st.st_mode & S_IXUSR ) != S_IXUSR )
00132           return 1;
00133     }
00134     mode &= ~X_OK;
00135     return _waccess( CONV(path), mode );
00136   }
00137 
00138   int chmod(const QString &path, mode_t mode)
00139   {
00140     return _wchmod( CONV(path), mode );
00141   }
00142 
00143   int lstat(const QString &path, KDE_struct_stat *buf)
00144   {
00145     return KDE::stat( path, buf );
00146   }
00147 
00148   int mkdir(const QString &pathname, mode_t)
00149   {
00150     return _wmkdir( CONV(pathname) );
00151   }
00152 
00153   int open(const QString &pathname, int flags, mode_t mode)
00154   {
00155     return _wopen( CONV(pathname), kdewin_fix_flags(flags), mode );
00156   }
00157 
00158   int rename(const QString &in, const QString &out)
00159   {
00160     // better than :waccess/_wunlink/_wrename
00161     bool ok = ( MoveFileExW( CONV(in), CONV(out),
00162                              MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED ) != 0 );
00163     return ok ? 0 : -1;
00164   }
00165 
00166   int stat(const QString &path, KDE_struct_stat *buf)
00167   {
00168     int result;
00169 #ifdef Q_CC_MSVC
00170     struct _stat64 s64;
00171 #else
00172     struct __stat64 s64;
00173 #endif
00174     const int len = path.length();
00175     if ( (len==2 || len==3) && path[1]==':' && path[0].isLetter() ) {
00176         /* 1) */
00177         QString newPath(path);
00178         if (len==2)
00179             newPath += QLatin1Char('\\');
00180         result = _wstat64( CONV(newPath), &s64 );
00181     } else
00182     if ( len > 1 && (path.endsWith(QLatin1Char('\\')) || path.endsWith(QLatin1Char('/'))) ) {
00183         /* 2) */
00184         const QString newPath = path.left( len - 1 );
00185         result = _wstat64( CONV(newPath), &s64 );
00186     } else {
00187         //TODO: is stat("/") ok?
00188         result = _wstat64( CONV(path), &s64 );
00189     }
00190     if( result != 0 )
00191         return result;
00192     // KDE5: fixme!
00193     buf->st_dev   = s64.st_dev;
00194     buf->st_ino   = s64.st_ino;
00195     buf->st_mode  = s64.st_mode;
00196     buf->st_nlink = s64.st_nlink;
00197     buf->st_uid   = -2; // be in sync with Qt4
00198     buf->st_gid   = -2; // be in sync with Qt4
00199     buf->st_rdev  = s64.st_rdev;
00200     buf->st_size  = s64.st_size;
00201     buf->st_atime = s64.st_atime;
00202     buf->st_mtime = s64.st_mtime;
00203     buf->st_ctime = s64.st_ctime;
00204     return result;
00205   }
00206   int utime(const QString &filename, struct utimbuf *buf)
00207   {
00208     return _wutime( CONV(filename), (struct _utimbuf*)buf );
00209   }
00210 };

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal