doxygen  1.8.18
About: Doxygen is a source code documentation generator tool for C++, C, Objective-C, C#, PHP, Java, Python, IDL (diverse flavors), Fortran, VHDL, Tcl, and to some extent D. Different output formats are supported.
  Fossies Dox: doxygen-1.8.18.src.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

qfile_unix.cpp
Go to the documentation of this file.
1 
38 #include "qglobal.h"
39 
40 #include "qfile.h"
41 #include "qfiledefs_p.h"
42 
43 #if (defined(_OS_MAC_) && (!defined(_OS_UNIX_))) || defined(_OS_MSDOS_) || defined(_OS_WIN32_) || defined(_OS_OS2_) || defined(_OS_CYGWIN_)
44 # define HAS_TEXT_FILEMODE // has translate/text filemode
45 #endif
46 #if defined(O_NONBLOCK)
47 # define HAS_ASYNC_FILEMODE
48 # define OPEN_ASYNC O_NONBLOCK
49 #elif defined(O_NDELAY)
50 # define HAS_ASYNC_FILEMODE
51 # define OPEN_ASYNC O_NDELAY
52 #endif
53 
54 bool qt_file_access( const QString& fn, int t )
55 {
56  if ( fn.isEmpty() )
57  return FALSE;
58  return ACCESS( QFile::encodeName(fn), t ) == 0;
59 }
60 
66 bool QFile::remove( const QString &fileName )
67 {
68  if ( fileName.isEmpty() ) {
69 #if defined(CHECK_NULL)
70  qWarning( "QFile::remove: Empty or null file name" );
71 #endif
72  return FALSE;
73  }
74  return unlink( QFile::encodeName(fileName) ) == 0;
75  // unlink more common in UNIX
76 }
77 
78 #if defined(O_NONBLOCK)
79 # define HAS_ASYNC_FILEMODE
80 # define OPEN_ASYNC O_NONBLOCK
81 #elif defined(O_NDELAY)
82 # define HAS_ASYNC_FILEMODE
83 # define OPEN_ASYNC O_NDELAY
84 #endif
85 
134 bool QFile::open( int m )
135 {
136  if ( isOpen() ) { // file already open
137 #if defined(CHECK_STATE)
138  qWarning( "QFile::open: File already open" );
139 #endif
140  return FALSE;
141  }
142  if ( fn.isNull() ) { // no file name defined
143 #if defined(CHECK_NULL)
144  qWarning( "QFile::open: No file name specified" );
145 #endif
146  return FALSE;
147  }
148  init(); // reset params
149  setMode( m );
150  if ( !(isReadable() || isWritable()) ) {
151 #if defined(CHECK_RANGE)
152  qWarning( "QFile::open: File access not specified" );
153 #endif
154  return FALSE;
155  }
156  bool ok = TRUE;
157  STATBUF st;
158  if ( isRaw() ) { // raw file I/O
159  int oflags = OPEN_RDONLY;
160  if ( isReadable() && isWritable() )
161  oflags = OPEN_RDWR;
162  else if ( isWritable() )
163  oflags = OPEN_WRONLY;
164  if ( flags() & IO_Append ) { // append to end of file?
165  if ( flags() & IO_Truncate )
166  oflags |= (OPEN_CREAT | OPEN_TRUNC);
167  else
168  oflags |= (OPEN_APPEND | OPEN_CREAT);
169  setFlags( flags() | IO_WriteOnly ); // append implies write
170  } else if ( isWritable() ) { // create/trunc if writable
171  if ( flags() & IO_Truncate )
172  oflags |= (OPEN_CREAT | OPEN_TRUNC);
173  else
174  oflags |= OPEN_CREAT;
175  }
176 #if defined(HAS_TEXT_FILEMODE)
177  if ( isTranslated() )
178 #ifdef __CYGWIN__
179  /* Do nothing, allowing the Cygwin mount mode to take effect. */;
180 #else
181  oflags |= OPEN_TEXT;
182 #endif
183  else
184  oflags |= OPEN_BINARY;
185 #endif
186 #if defined(HAS_ASYNC_FILEMODE)
187  if ( isAsynchronous() )
188  oflags |= OPEN_ASYNC;
189 #endif
190  fd = OPEN( QFile::encodeName(fn), oflags, 0666 );
191 
192  if ( fd != -1 ) { // open successful
193  FSTAT( fd, &st ); // get the stat for later usage
194  } else {
195  ok = FALSE;
196  }
197  } else { // buffered file I/O
198  QCString perm;
199  char perm2[4];
200  bool try_create = FALSE;
201  if ( flags() & IO_Append ) { // append to end of file?
202  setFlags( flags() | IO_WriteOnly ); // append implies write
203  perm = isReadable() ? "a+" : "a";
204  } else {
205  if ( isReadWrite() ) {
206  if ( flags() & IO_Truncate ) {
207  perm = "w+";
208  } else {
209  perm = "r+";
210  try_create = TRUE; // try to create if not exists
211  }
212  } else if ( isReadable() ) {
213  perm = "r";
214  } else if ( isWritable() ) {
215  perm = "w";
216  }
217  }
218  qstrcpy( perm2, perm );
219 #if defined(HAS_TEXT_FILEMODE)
220  if ( isTranslated() )
221 #ifdef __CYGWIN__
222  /* Do nothing, allowing the Cygwin mount mode to take effect. */;
223 #else
224  strcat( perm2, "t" );
225 #endif
226  else
227  strcat( perm2, "b" );
228 #endif
229  while (1) { // At most twice
230 
231  fh = fopen( QFile::encodeName(fn), perm2 );
232 
233  if ( !fh && try_create ) {
234  perm2[0] = 'w'; // try "w+" instead of "r+"
235  try_create = FALSE;
236  } else {
237  break;
238  }
239  }
240  if ( fh ) {
241  FSTAT( FILENO(fh), &st ); // get the stat for later usage
242  } else {
243  ok = FALSE;
244  }
245  }
246  if ( ok ) {
247  setState( IO_Open );
248  // on successful open the file stat was got; now test what type
249  // of file we have
250  if ( (st.st_mode & STAT_MASK) != STAT_REG ) {
251  // non-seekable
253  length = INT_MAX;
254  ioIndex = (flags() & IO_Append) == 0 ? 0 : length;
255  } else {
256  length = (int)st.st_size;
257  ioIndex = (flags() & IO_Append) == 0 ? 0 : length;
258  if ( !(flags()&IO_Truncate) && length == 0 && isReadable() ) {
259  // try if you can read from it (if you can, it's a sequential
260  // device; e.g. a file in the /proc filesystem)
261  int c = getch();
262  if ( c != -1 ) {
263  ungetch(c);
265  length = INT_MAX;
266  }
267  }
268  }
269  } else {
270  init();
271  if ( errno == EMFILE ) // no more file handles/descrs
273  else
275  }
276  return ok;
277 }
278 
306 bool QFile::open( int m, FILE *f )
307 {
308  if ( isOpen() ) {
309 #if defined(CHECK_RANGE)
310  qWarning( "QFile::open: File already open" );
311 #endif
312  return FALSE;
313  }
314  init();
315  setMode( m &~IO_Raw );
316  setState( IO_Open );
317  fh = f;
318  ext_f = TRUE;
319  STATBUF st;
320  FSTAT( FILENO(fh), &st );
321  ioIndex = (int)ftell( fh );
322  if ( (st.st_mode & STAT_MASK) != STAT_REG || f == stdin ) { //stdin is non seekable
323  // non-seekable
325  length = INT_MAX;
326  } else {
327  length = (int)st.st_size;
328  if ( !(flags()&IO_Truncate) && length == 0 && isReadable() ) {
329  // try if you can read from it (if you can, it's a sequential
330  // device; e.g. a file in the /proc filesystem)
331  int c = getch();
332  if ( c != -1 ) {
333  ungetch(c);
335  length = INT_MAX;
336  }
337  }
338  }
339  return TRUE;
340 }
341 
356 bool QFile::open( int m, int f )
357 {
358  if ( isOpen() ) {
359 #if defined(CHECK_RANGE)
360  qWarning( "QFile::open: File already open" );
361 #endif
362  return FALSE;
363  }
364  init();
365  setMode( m |IO_Raw );
366  setState( IO_Open );
367  fd = f;
368  ext_f = TRUE;
369  STATBUF st;
370  FSTAT( fd, &st );
371  ioIndex = (int)LSEEK(fd, 0, SEEK_CUR);
372  if ( (st.st_mode & STAT_MASK) != STAT_REG || f == 0 ) { // stdin is not seekable...
373  // non-seekable
375  length = INT_MAX;
376  } else {
377  length = (int)st.st_size;
378  if ( length == 0 && isReadable() ) {
379  // try if you can read from it (if you can, it's a sequential
380  // device; e.g. a file in the /proc filesystem)
381  int c = getch();
382  if ( c != -1 ) {
383  ungetch(c);
385  length = INT_MAX;
386  }
387  resetStatus();
388  }
389  }
390  return TRUE;
391 }
392 
399 {
400  STATBUF st;
401  if ( isOpen() ) {
402  FSTAT( fh ? FILENO(fh) : fd, &st );
403  } else {
404  STAT( QFile::encodeName(fn), &st );
405  }
406  return (uint)st.st_size;
407 }
408 
434 bool QFile::at( int pos )
435 {
436  if ( !isOpen() ) {
437 #if defined(CHECK_STATE)
438  qWarning( "QFile::at: File is not open" );
439 #endif
440  return FALSE;
441  }
442  bool ok;
443  if ( isRaw() ) { // raw file
444  pos = (int)LSEEK(fd, pos, SEEK_SET);
445  ok = pos != -1;
446  } else { // buffered file
447  ok = fseek(fh, pos, SEEK_SET) == 0;
448  }
449  if ( ok )
450  ioIndex = pos;
451 #if defined(CHECK_RANGE)
452  else
453  qWarning( "QFile::at: Cannot set file position %d", pos );
454 #endif
455  return ok;
456 }
457 
473 int QFile::readBlock( char *p, uint len )
474 {
475 #if defined(CHECK_NULL)
476  if ( !p )
477  qWarning( "QFile::readBlock: Null pointer error" );
478 #endif
479 #if defined(CHECK_STATE)
480  if ( !isOpen() ) { // file not open
481  qWarning( "QFile::readBlock: File not open" );
482  return -1;
483  }
484  if ( !isReadable() ) { // reading not permitted
485  qWarning( "QFile::readBlock: Read operation not permitted" );
486  return -1;
487  }
488 #endif
489  int nread = 0; // number of bytes read
490  if ( !ungetchBuffer.isEmpty() ) {
491  // need to add these to the returned string.
492  int l = ungetchBuffer.length();
493  while( nread < l ) {
494  *p = ungetchBuffer[ l - nread - 1 ];
495  p++;
496  nread++;
497  }
498  ungetchBuffer.truncate( l - nread );
499  }
500 
501  if ( nread < (int)len ) {
502  if ( isRaw() ) { // raw file
503  nread += (int)READ( fd, p, len-nread );
504  if ( len && nread <= 0 ) {
505  nread = 0;
507  }
508  } else { // buffered file
509  nread += (int)fread( p, 1, len-nread, fh );
510  if ( (uint)nread != len ) {
511  if ( ferror( fh ) || nread==0 )
513  }
514  }
515  }
516  ioIndex += nread;
517  return nread;
518 }
519 
537 int QFile::writeBlock( const char *p, uint len )
538 {
539 #if defined(CHECK_NULL)
540  if ( p == 0 && len != 0 )
541  qWarning( "QFile::writeBlock: Null pointer error" );
542 #endif
543 #if defined(CHECK_STATE)
544  if ( !isOpen() ) { // file not open
545  qWarning( "QFile::writeBlock: File not open" );
546  return -1;
547  }
548  if ( !isWritable() ) { // writing not permitted
549  qWarning( "QFile::writeBlock: Write operation not permitted" );
550  return -1;
551  }
552 #endif
553  if (p==0) return 0;
554  int nwritten; // number of bytes written
555  if ( isRaw() ) // raw file
556  nwritten = (int)WRITE( fd, p, len );
557  else // buffered file
558  nwritten = (int)fwrite( p, 1, len, fh );
559  if ( nwritten != (int)len ) { // write error
560  if ( errno == ENOSPC ) // disk is full
562  else
564  if ( isRaw() ) // recalc file position
565  ioIndex = (int)LSEEK( fd, 0, SEEK_CUR );
566  else
567  ioIndex = fseek( fh, 0, SEEK_CUR );
568  } else {
569  ioIndex += nwritten;
570  }
571  if ( ioIndex > length ) // update file length
572  length = ioIndex;
573  return nwritten;
574 }
575 
587 int QFile::handle() const
588 {
589  if ( !isOpen() )
590  return -1;
591  else if ( fh )
592  return FILENO( fh );
593  else
594  return fd;
595 }
596 
615 {
616  bool ok = FALSE;
617  if ( isOpen() ) { // file is not open
618  if ( fh ) { // buffered file
619  if ( ext_f )
620  ok = fflush( fh ) != -1; // flush instead of closing
621  else
622  ok = fclose( fh ) != -1;
623  } else { // raw file
624  if ( ext_f )
625  ok = TRUE; // cannot close
626  else
627  ok = CLOSE( fd ) != -1;
628  }
629  init(); // restore internal state
630  }
631  if (!ok)
633 
634  return;
635 }
636 
638 {
639  if (isOpen())
640  {
641  // TODO: support 64 bit size
642  return ftell( fh );
643  }
644  return -1;
645 }
646 
648 {
649  if (isOpen())
650  {
651  // TODO: support 64 bit size
652  if (fseek( fh, 0, SEEK_END )!=-1)
653  {
654  return ftell( fh );
655  }
656  }
657  return -1;
658 }
659 
660 bool QFile::seek( int64 pos )
661 {
662  if (isOpen())
663  {
664  // TODO: support 64 bit size
665  return fseek( fh, (long)pos, SEEK_SET )!=-1;
666  }
667  return FALSE;
668 }
669 
qfile.h
Portable::fseek
portable_off_t fseek(FILE *f, portable_off_t offset, int whence)
Definition: portable.cpp:292
QFile::at
int at() const
Definition: qfile.h:124
OPEN
#define OPEN
Definition: qfiledefs_p.h:211
QIODevice::setStatus
void setStatus(int)
Definition: qiodevice.cpp:408
OPEN_RDWR
#define OPEN_RDWR
Definition: qfiledefs_p.h:228
QCString::isEmpty
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition: qcstring.h:191
uint
unsigned uint
Definition: qglobal.h:351
qWarning
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
IO_ResourceError
#define IO_ResourceError
Definition: qiodevice.h:81
IO_ReadError
#define IO_ReadError
Definition: qiodevice.h:78
IO_Append
#define IO_Append
Definition: qiodevice.h:64
QIODevice::isRaw
bool isRaw() const
Definition: qiodevice.h:102
CLOSE
#define CLOSE
Definition: qfiledefs_p.h:212
LSEEK
#define LSEEK
Definition: qfiledefs_p.h:213
QIODevice::isReadable
bool isReadable() const
Definition: qiodevice.h:106
QFile::length
int length
Definition: qfile.h:105
IO_Sequential
#define IO_Sequential
Definition: qiodevice.h:50
qstrcpy
char * qstrcpy(char *dst, const char *src)
Definition: qcstring.h:89
qglobal.h
STATBUF
#define STATBUF
Definition: qfiledefs_p.h:199
QIODevice::setType
void setType(int)
Definition: qiodevice.cpp:363
IO_Open
#define IO_Open
Definition: qiodevice.h:71
QCString::length
uint length() const
Returns the length of the string, excluding the 0-terminator.
Definition: qcstring.h:197
QFile::writeBlock
int writeBlock(const char *data, uint len)
Definition: qfile_unix.cpp:537
languages.l
l
Definition: languages.py:75
IO_WriteOnly
#define IO_WriteOnly
Definition: qiodevice.h:62
QFile::ungetchBuffer
QCString ungetchBuffer
Definition: qfile.h:111
OPEN_RDONLY
#define OPEN_RDONLY
Definition: qfiledefs_p.h:226
QFile::readBlock
int readBlock(char *data, uint len)
Definition: qfile_unix.cpp:473
IO_Truncate
#define IO_Truncate
Definition: qiodevice.h:65
QString::isNull
bool isNull() const
Definition: qstring.h:411
FILENO
#define FILENO
Definition: qfiledefs_p.h:210
QCString
This is an alternative implementation of QCString.
Definition: qcstring.h:134
QIODevice::isTranslated
bool isTranslated() const
Definition: qiodevice.h:105
QFile::seek
bool seek(int64 pos)
Definition: qfile_unix.cpp:660
QFile::getch
int getch()
Definition: qfile.cpp:337
FSTAT
#define FSTAT
Definition: qfiledefs_p.h:202
WRITE
#define WRITE
Definition: qfiledefs_p.h:215
IO_OpenError
#define IO_OpenError
Definition: qiodevice.h:82
TRUE
@ TRUE
Definition: mscgen_bool.h:29
FALSE
@ FALSE
Definition: mscgen_bool.h:28
IO_WriteError
#define IO_WriteError
Definition: qiodevice.h:79
qfiledefs_p.h
QFile::init
void init()
Definition: qfile.cpp:124
Portable::fopen
FILE * fopen(const char *fileName, const char *mode)
Definition: portable.cpp:314
STAT_MASK
#define STAT_MASK
Definition: qfiledefs_p.h:206
QIODevice::setState
void setState(int)
Definition: qiodevice.cpp:393
QIODevice::isWritable
bool isWritable() const
Definition: qiodevice.h:107
QFile::fd
int fd
Definition: qfile.h:104
QFile::handle
int handle() const
Definition: qfile_unix.cpp:587
STAT_REG
#define STAT_REG
Definition: qfiledefs_p.h:204
QFile::open
bool open(int)
Definition: qfile_unix.cpp:134
QString::isEmpty
bool isEmpty() const
Definition: qstring.h:715
IO_UnspecifiedError
#define IO_UnspecifiedError
Definition: qiodevice.h:86
QIODevice::resetStatus
void resetStatus()
Definition: qiodevice.h:113
OPEN_CREAT
#define OPEN_CREAT
Definition: qfiledefs_p.h:229
QIODevice::setMode
void setMode(int)
Definition: qiodevice.cpp:378
QIODevice::isReadWrite
bool isReadWrite() const
Definition: qiodevice.h:108
IO_Raw
#define IO_Raw
Definition: qiodevice.h:56
STAT
#define STAT
Definition: qfiledefs_p.h:201
QFile::toEnd
int64 toEnd()
Definition: qfile_unix.cpp:647
ACCESS
#define ACCESS
Definition: qfiledefs_p.h:216
QFile::fh
FILE * fh
Definition: qfile.h:103
QCString::truncate
bool truncate(uint pos)
Truncates the string at position pos.
Definition: qcstring.h:234
QIODevice::flags
int flags() const
Definition: qiodevice.h:94
QFile::encodeName
static QCString encodeName(const QString &fileName)
Definition: qfile.cpp:494
QIODevice::setFlags
void setFlags(int f)
Definition: qiodevice.h:136
READ
#define READ
Definition: qfiledefs_p.h:214
QIODevice::isAsynchronous
bool isAsynchronous() const
Definition: qiodevice.h:104
QFile::ungetch
int ungetch(int)
Definition: qfile.cpp:425
qt_file_access
bool qt_file_access(const QString &fn, int t)
Implementation of QFileInfo class.
Definition: qfile_unix.cpp:54
Portable::ftell
portable_off_t ftell(FILE *f)
Definition: portable.cpp:303
QFile::fn
QString fn
Definition: qfile.h:102
QFile::size
uint size() const
Definition: qfile_unix.cpp:398
int64
long long int64
Definition: qglobal.h:361
QFile::remove
bool remove()
Definition: qfile.cpp:205
OPEN_TRUNC
#define OPEN_TRUNC
Definition: qfiledefs_p.h:230
QIODevice::isOpen
bool isOpen() const
Definition: qiodevice.h:110
QFile::ext_f
bool ext_f
Definition: qfile.h:106
Portable::unlink
void unlink(const char *fileName)
Definition: portable.cpp:505
QIODevice::ioIndex
int ioIndex
Definition: qiodevice.h:141
QFile::pos
int64 pos() const
Definition: qfile_unix.cpp:637
OPEN_WRONLY
#define OPEN_WRONLY
Definition: qfiledefs_p.h:227
QFile::close
void close()
Definition: qfile_unix.cpp:614
QString
QString member functions.
Definition: qstring.h:351
OPEN_APPEND
#define OPEN_APPEND
Definition: qfiledefs_p.h:231