File: vfs_file_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 (190 lines) | stat: -rw-r--r-- 3,745 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
/*
   FALCON - The Falcon Programming Language.
   FILE: vfs_file.cpp

   VSF provider for physical file system on the host system.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Fri, 12 Sep 2008 21:47:10 +0200

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

   See LICENSE file for licensing details.
*/

#include <falcon/vfs_file.h>
#include <falcon/error.h>
#include <falcon/fstream_sys_unix.h>
#include <falcon/dir_sys_unix.h>
#include <falcon/sys.h>
#include <falcon/autocstring.h>
#include <falcon/streambuffer.h>

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

namespace Falcon {

// we don't use filesystem data.
VFSFile::VFSFile():
   VFSProvider( "file" ),
   m_fsdata(0)
{}


VFSFile::~VFSFile()
{
}


Stream *VFSFile::open( const URI& uri, const OParams &p )
{
   int omode = paramsToMode( p );

   // todo: do something about share mode
   AutoCString cfilename( uri.path() );

   errno = 0;
   int handle = ::open( cfilename.c_str(), omode );
   if ( handle >= 0 )
   {
      UnixFileSysData *ufd = new UnixFileSysData( handle, 0 );
      return new StreamBuffer( new FileStream( ufd ) );
   }

   return 0;
}


Stream *VFSFile::create( const URI& uri, const CParams &p, bool &bSuccess )
{
   int omode = paramsToMode( p );

   if ( p.isNoOvr() )
      omode |= O_EXCL;

   //TODO: something about sharing
   AutoCString cfilename( uri.path() );
   errno=0;

   umask( 0000 );
   int handle = ::open( cfilename.c_str(), O_CREAT | omode, p.createMode() );

   if ( handle >= 0 ) {
      bSuccess = true;
      if ( ! p.isNoStream() )
      {
         UnixFileSysData *ufd = new UnixFileSysData( handle, 0 );
         return new StreamBuffer( new FileStream( ufd ) );
      }
      else
         return 0;
   }

   bSuccess = false;
   return 0;
}


DirEntry* VFSFile::openDir( const URI& uri )
{
   AutoCString filename( uri.path() );

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

   return new DirEntry_unix( uri.path(), dir );
}


bool VFSFile::readStats( const URI& uri, FileStat &s )
{
   return Sys::fal_stats( uri.path(), s );
}


bool VFSFile::writeStats( const URI& uri, const FileStat &s )
{
   // TODO: write contents
   return false;
}

bool VFSFile::chown( const URI &uri, int uid, int gid )
{
   AutoCString filename( uri.path() );
   return ::chown( filename.c_str(), uid, gid ) == 0;
}


bool VFSFile::chmod( const URI &uri, int mode )
{
   AutoCString filename( uri.path() );
   return ::chmod( filename.c_str(), mode ) == 0;
}

bool VFSFile::link( const URI &uri1, const URI &uri2, bool bSymbolic )
{
   // TODO
   return false;
}


bool VFSFile::unlink( const URI &uri )
{
   AutoCString filename( uri.path() );
   return ::unlink( filename.c_str() ) == 0;
}

bool VFSFile::move( const URI &suri, const URI &duri )
{
   AutoCString filename( suri.path() );
   AutoCString dest( duri.path() );
   return ::rename( filename.c_str(), dest.c_str() ) == 0;
}


bool VFSFile::mkdir( const URI &uri, uint32 mode )
{
   AutoCString filename( uri.path() );
   return ::mkdir( filename.c_str(), mode ) == 0;
}


bool VFSFile::rmdir( const URI &uri )
{
   AutoCString filename( uri.path() );
   return ::rmdir( filename.c_str() ) == 0;
}


int64 VFSFile::getLastFsError()
{
   return (int64) errno;
}


Error *VFSFile::getLastError()
{
   if( errno != 0 )
   {
      IoError *e = new IoError( e_io_error );
      e->systemError( errno );
      return e;
   }

   return 0;
}


}

/* end of vsf_file.cpp */