1/*
2 This file is part of the KDE libraries
3 Copyright (C) 2001 Waldo Bastian <bastian@kde.org>
4 Copyright (C) 2004 Jarosław Staniek <staniek@kde.org>
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#ifndef _KDE_FILE_H_
22#define _KDE_FILE_H_
23
24/**
25 * \file kde_file.h
26 * \brief This file provides portable defines for file support.
27 *
28 * Use the KDE_xxx defines instead of the normal C
29 * functions and structures.
30 * \since 3.3
31 */
32
33#include <fcntl.h>
34#include <sys/stat.h>
35#include <unistd.h>
36#include <utime.h>
37#if defined _WIN32 || defined _WIN64
38#include <kde_file_win.h>
39#endif
40#include <kdecore_export.h>
41
42/* added not for Solaris and OpenSolaris platforms */
43
44#if (defined _LFS64_LARGEFILE) && (defined _LARGEFILE64_SOURCE) && (!defined _GNU_SOURCE) && (!defined __sun)
45/*
46 * This section provides portable defines for large file support.
47 * To use this you must compile your code with _LARGEFILE64_SOURCE
48 * defined and use the KDE_xxx defines instead of the normal
49 * C functions and structures.
50 *
51 * Please note that not every platform supports 64 bit file structures,
52 * in that case the normal 32 bit functions will be used.
53 *
54 * @see http://www.suse.de/~aj/linux_lfs.html
55 * @see http://ftp.sas.com/standards/large.file/xopen/x_open.05Mar96.html
56 *
57 * KDE makes use of the "Transitional Extensions" since we can not ensure
58 * that all modules and libraries used by KDE will be compiled with
59 * 64-bit support.
60 * (A.3.2.3 Mixed API and Compile Environments within a Single Process)
61 */
62#define KDE_stat ::stat64
63#define KDE_lstat ::lstat64
64#define KDE_fstat ::fstat64
65#define KDE_open ::open64
66#define KDE_lseek ::lseek64
67#define KDE_fseek ::fseek64
68#define KDE_ftell ::ftell64
69#define KDE_fgetpos ::fgetpos64
70#define KDE_fsetpos ::fsetpos64
71#define KDE_readdir ::readdir64
72#define KDE_sendfile ::sendfile64
73#define KDE_struct_stat struct stat64
74#define KDE_struct_dirent struct dirent64
75#define KDE_rename ::rename
76#define KDE_mkdir ::mkdir
77/* TODO: define for win32 */
78
79#else /* !_LFS64_LARGEFILE */
80
81/*
82 * This section defines portable defines for standard file support.
83 */
84
85/*
86 Platform specific definitions for Solaris and OpenSolaris tested with gcc 4.3.2
87*/
88#if defined __sun__
89#define KDE_stat ::stat
90#define KDE_lstat ::lstat
91#define KDE_fstat ::fstat
92#define KDE_open ::open
93#define KDE_lseek ::lseek
94#define KDE_fseek ::fseek
95#define KDE_ftell ::ftell
96#define KDE_fgetpos ::fgetpos
97#define KDE_fsetpos ::fsetpos
98#define KDE_readdir ::readdir
99#define KDE_sendfile ::sendfile
100#define KDE_struct_stat struct stat
101#define KDE_struct_dirent struct dirent
102#define KDE_rename ::rename
103#define KDE_mkdir ::mkdir
104
105#else
106
107#if defined _WIN32 || defined _WIN64
108#define KDE_stat kdewin32_stat
109#define KDE_lstat kdewin32_lstat
110#define KDE_open kdewin32_open
111#define KDE_rename kdewin32_rename
112#define KDE_mkdir kdewin32_mkdir
113#else /* unix */
114#define KDE_stat ::stat
115#define KDE_lstat ::lstat
116#define KDE_open ::open
117#define KDE_rename ::rename
118#define KDE_mkdir ::mkdir
119#endif
120
121#define KDE_fstat ::fstat
122#define KDE_lseek ::lseek
123#define KDE_fseek ::fseek
124#define KDE_ftell ::ftell
125#define KDE_fgetpos ::fgetpos
126#define KDE_fsetpos ::fsetpos
127#define KDE_readdir ::readdir
128#define KDE_sendfile ::sendfile
129#define KDE_struct_stat struct stat
130#define KDE_struct_dirent struct dirent
131#endif
132
133#ifdef _LFS64_STDIO
134#define KDE_fopen ::fopen64
135#define KDE_freopen ::freopen64
136/* TODO: define for win32 */
137#else
138#if defined _WIN32 || defined _WIN64
139#define KDE_fopen kdewin32_fopen
140#define KDE_freopen kdewin32_freopen
141#else /* unix */
142#define KDE_fopen ::fopen
143#endif
144#endif
145#endif
146
147/* functions without 64-bit version but wrapped for compatibility reasons */
148#if defined _WIN32 || defined _WIN64
149#define KDE_fdopen kdewin32_fdopen
150#define KDE_signal kdewin32_signal
151#else /* unix */
152#define KDE_fdopen ::fdopen
153#define KDE_signal ::signal
154#endif
155
156#include <QtCore/QFile>
157class QString;
158namespace KDE
159{
160 /** replacement for ::access() to handle filenames in a platform independent way */
161 KDECORE_EXPORT int access(const QString &path, int mode);
162 /** replacement for ::chmod() to handle filenames in a platform independent way */
163 KDECORE_EXPORT int chmod(const QString &path, mode_t mode);
164 /** replacement for ::fopen()/::fopen64() to handle filenames in a platform independent way */
165 KDECORE_EXPORT FILE *fopen(const QString &pathname, const char *mode);
166 /** replacement for ::lstat()/::lstat64() to handle filenames in a platform independent way */
167 KDECORE_EXPORT int lstat(const QString &path, KDE_struct_stat *buf);
168 /** replacement for ::mkdir() to handle pathnames in a platform independent way */
169 KDECORE_EXPORT int mkdir(const QString &pathname, mode_t mode);
170 /** replacement for ::open()/::open64() to handle filenames in a platform independent way */
171 KDECORE_EXPORT int open(const QString &pathname, int flags, mode_t mode = 0);
172 /** replacement for ::rename() to handle pathnames in a platform independent way */
173 KDECORE_EXPORT int rename(const QString &in, const QString &out);
174 /** replacement for ::stat()/::stat64() to handle filenames in a platform independent way */
175 KDECORE_EXPORT int stat(const QString &path, KDE_struct_stat *buf);
176 /** replacement for ::utime() to handle filenames in a platform independent way */
177 KDECORE_EXPORT int utime(const QString &filename, struct utimbuf *buf);
178#ifndef Q_WS_WIN
179 inline int access(const QString &path, int mode)
180 {
181 return ::access( QFile::encodeName(path).constData(), mode );
182 }
183 inline int chmod(const QString &path, mode_t mode)
184 {
185 return ::chmod( QFile::encodeName(path).constData(), mode );
186 }
187 inline FILE *fopen(const QString &pathname, const char *mode)
188 {
189 return KDE_fopen( QFile::encodeName(pathname).constData(), mode );
190 }
191 inline int lstat(const QString &path, KDE_struct_stat *buf)
192 {
193 return KDE_lstat( QFile::encodeName(path).constData(), buf );
194 }
195 inline int mkdir(const QString &pathname, mode_t mode)
196 {
197 return KDE_mkdir( QFile::encodeName(pathname).constData(), mode );
198 }
199 inline int open(const QString &pathname, int flags, mode_t mode)
200 {
201 return KDE_open( QFile::encodeName(pathname).constData(), flags, mode );
202 }
203 inline int rename(const QString &in, const QString &out)
204 {
205 return KDE_rename( QFile::encodeName(in).constData(), QFile::encodeName(out).constData() );
206 }
207 inline int stat(const QString &path, KDE_struct_stat *buf)
208 {
209 return KDE_stat( QFile::encodeName(path).constData(), buf );
210 }
211 inline int utime(const QString &filename, struct utimbuf *buf)
212 {
213 return ::utime( QFile::encodeName(filename).constData(), buf );
214 }
215#endif
216}
217
218#if defined _WIN32 || defined _WIN64
219#define KPATH_SEPARATOR ';'
220#define KDIR_SEPARATOR '\\' /* faster than QDir::separator() */
221#else
222#ifndef O_BINARY
223#define O_BINARY 0 /* for open() */
224#endif
225#define KPATH_SEPARATOR ':'
226#define KDIR_SEPARATOR '/' /* faster than QDir::separator() */
227#endif
228
229#endif /* _KDE_FILE_H_ */
230