File: dir_sys_unix.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (360 lines) | stat: -rw-r--r-- 7,582 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
   FALCON - The Falcon Programming Language.
   FILE: dir_unix.cpp

   Implementation of directory system support for unix.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: dom nov 7 2004

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

/** \file
   Implementation of directory system support for unix.
*/

#include <falcon/item.h>
#include <falcon/mempool.h>
#include <falcon/string.h>
#include <falcon/memory.h>
#include <falcon/autocstring.h>

#include <falcon/dir_sys_unix.h>
#include <falcon/time_sys_unix.h>
#include <falcon/timestamp.h>
#include <falcon/filestat.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <cstring>
#include <stdio.h>


namespace Falcon {
namespace Sys {

bool fal_fileType( const String &fname, FileStat::e_fileType &st )
{
   AutoCString filename(fname );
   struct stat fs;

   if ( lstat( filename.c_str(), &fs ) != 0 ) {
      st = FileStat::t_notFound;
      return false;
   }

   if( S_ISREG( fs.st_mode ) )
      st = FileStat::t_normal;
   else if( S_ISDIR( fs.st_mode ) )
      st = FileStat::t_dir;
   else if( S_ISFIFO( fs.st_mode ) )
      st = FileStat::t_pipe;
   else if( S_ISLNK( fs.st_mode ) )
      st = FileStat::t_link;
   else if( S_ISBLK( fs.st_mode ) || S_ISCHR( fs.st_mode ) )
      st = FileStat::t_device;
   else if( S_ISSOCK( fs.st_mode ) )
      st = FileStat::t_socket;
   else
      st = FileStat::t_unknown;

   return true;
}

bool fal_stats( const String &f, FileStat &sts )
{
   AutoCString filename( f );

   struct stat fs;

   if ( lstat( filename.c_str(), &fs ) != 0 )
   {
      return false;
   }

   sts.m_size = fs.st_size;
   if( S_ISREG( fs.st_mode ) )
      sts.m_type = FileStat::t_normal;
   else if( S_ISDIR( fs.st_mode ) )
      sts.m_type = FileStat::t_dir;
   else if( S_ISFIFO( fs.st_mode ) )
      sts.m_type = FileStat::t_pipe;
   else if( S_ISLNK( fs.st_mode ) )
      sts.m_type = FileStat::t_link;
   else if( S_ISBLK( fs.st_mode ) || S_ISCHR( fs.st_mode ) )
      sts.m_type = FileStat::t_device;
   else if( S_ISSOCK( fs.st_mode ) )
      sts.m_type = FileStat::t_socket;
   else
      sts.m_type = FileStat::t_unknown;

   sts.m_access = fs.st_mode;
   sts.m_owner = fs.st_uid;      /* user ID of owner */
   sts.m_group = fs.st_gid;      /* group ID of owner */


   UnixSystemTime mtime( fs.st_mtime );

   if (sts.m_atime == 0 )
      sts.m_atime = new TimeStamp();
   mtime.m_time_t = fs.st_atime;
   sts.m_atime->fromSystemTime( mtime );    /* time of last access */

   if (sts.m_ctime == 0 )
      sts.m_ctime = new TimeStamp();
   mtime.m_time_t = fs.st_ctime;
   sts.m_ctime->fromSystemTime( mtime );     /* time of last change */

   // copy last change time to last modify time
   if (sts.m_mtime == 0 )
      sts.m_mtime = new TimeStamp();
   sts.m_mtime->fromSystemTime( mtime );

   return true;
}

bool fal_mkdir( const String &f, int32 &fsStatus )
{
   AutoCString filename( f );

   if ( ::mkdir( filename.c_str(), 0744 ) == 0 ) {
      fsStatus = 0;
      return true;
   }
   fsStatus = errno;
   return false;
}

bool fal_mkdir( const String &strName, int32 &fsError, bool descend )
{
   if ( descend )
   {
      // find /.. sequences
      uint32 pos = strName.find( "/" );
      if(pos == 0) pos = strName.find( "/", 1 ); // an absolute path
      while( true )
      {
         String strPath( strName, 0, pos );

         // stat the file
         FileStat fstats;
         // if the file exists...
         if ( (! Sys::fal_stats( strPath, fstats )) ||
              fstats.m_type != FileStat::t_dir )
         {
            // if it's not a directory, try to create the directory.
            if ( ! Sys::fal_mkdir( strPath, fsError ) )
               return false;
         }

         // last loop?
         if ( pos == String::npos )
            break;

         pos = strName.find( "/", pos + 1 );
       }

   }
   else
   {
      // Just one try; succeed or fail
      return Sys::fal_mkdir( strName, fsError );
   }

   return true;
}

bool fal_rmdir( const String &f, int32 &fsStatus )
{
   AutoCString filename( f );

   if ( ::rmdir( filename.c_str() ) == 0 ) {
      fsStatus = 0;
      return true;
   }
   fsStatus = errno;
   return false;
}

bool fal_unlink( const String &f, int32 &fsStatus )
{
   AutoCString filename( f );

   if ( ::unlink( filename.c_str() ) == 0 ) {
      fsStatus = 0;
      return true;
   }

   fsStatus = errno;
   return false;
}

bool fal_move( const String &f, const String &d, int32 &fsStatus )
{
   AutoCString filename( f );
   AutoCString dest( d );

   if ( ::rename( filename.c_str(), dest.c_str() ) == 0 )
   {
      fsStatus = 0;
      return true;
   }
   fsStatus = errno;
   return false;
}

bool fal_chdir( const String &f, int32 &fsStatus )
{
   AutoCString filename( f );

   if ( ::chdir( filename.c_str() ) == 0 ) {
      fsStatus = 0;
      return true;
   }
   fsStatus = errno;
   return false;
}

bool fal_getcwd( String &fname, int32 &fsError )
{

   char buf[256];
   uint32 pwdSize = 256;
   char *buffer = buf;
   char *bufret;

   while ( (bufret = ::getcwd( buffer, pwdSize )) == 0 && errno == ERANGE ) {
      pwdSize += 256;
      if ( buffer != buf )
         memFree( buffer );
      buffer = ( char * ) memAlloc( pwdSize );
   }

   fsError = errno;
   bool val;
   if ( bufret != 0 )
   {
      val = true;
      fname.fromUTF8( bufret );
   }
   else
      val = false;

   if ( buffer != buf )
      memFree( buffer );

   return val;
}

bool fal_chmod( const String &fname, uint32 mode )
{
   AutoCString filename( fname );
   bool ret = ::chmod( filename.c_str(), mode ) == 0;
   return ret;
}

bool fal_chown( const String &fname, int32 owner )
{
   AutoCString filename( fname );
   bool ret = ::chown( filename.c_str(), owner , (gid_t) -1 ) == 0;
   return ret;
}

bool fal_readlink( const String &fname, String &link )
{
   char buf[1024];
   int len;
   AutoCString filename( fname );

   if ( ( len = readlink( filename.c_str(), buf, sizeof(buf) - 1 ) ) != -1) {
      buf[len] = '\0';
      link.fromUTF8( buf );
      return true;
   }
   return false;
}

bool fal_writelink( const String &fname, const String &link )
{
   AutoCString filename( fname );
   AutoCString linkname( link );

   if ( ! symlink( filename.c_str(), linkname.c_str() ) )
   {
      return false;
   }

   return true;
}

bool fal_chgrp( const String &fname, int32 owner )
{
   AutoCString filename( fname );
   bool ret = ::chown( filename.c_str(), (uid_t) -1, owner ) == 0;
   return ret;
}

::Falcon::DirEntry *fal_openDir( const String &p, int32 &fsStatus )
{
   AutoCString filename( p );

   DIR *dir = ::opendir( filename.c_str() );
   if ( dir == 0 ) {
      fsStatus = errno;
      return 0;
   }

   return new DirEntry_unix( p, dir );
}

void fal_closeDir( ::Falcon::DirEntry *entry )
{
	delete entry;
}

} // SYS namespace



bool DirEntry_unix::read( String &res )
{
   // Glibc doesn't perform that check
   if( m_raw_dir == 0)
   {
      return false;
   }
   struct dirent *d;

   errno = 0;
   d = readdir( m_raw_dir );
   m_lastError = errno;

   if ( d == 0 ) {
      return false;
   }

   res.fromUTF8( d->d_name );
   return true;
}

void DirEntry_unix::close()
{
   if ( m_raw_dir != 0 ) {
      errno = 0;
      closedir( m_raw_dir );
      m_lastError = errno;
   }
   m_raw_dir = 0;
}


}


/* end of dir_unix.cpp */