KDECore
kde_file_win.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
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
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
00110
00111
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
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
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
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
00184 const QString newPath = path.left( len - 1 );
00185 result = _wstat64( CONV(newPath), &s64 );
00186 } else {
00187
00188 result = _wstat64( CONV(path), &s64 );
00189 }
00190 if( result != 0 )
00191 return result;
00192
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;
00198 buf->st_gid = -2;
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 };