File: vfs_file_win.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 (274 lines) | stat: -rw-r--r-- 5,428 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
/*
   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_win.h>
#include <falcon/dir_sys_win.h>
#include <falcon/sys.h>
#include <falcon/autocstring.h>
#include <falcon/autowstring.h>
#include <windows.h>
#include <falcon/streambuffer.h>

namespace Falcon 
{

static DWORD win_paramsToMode( VFSFile::OParams p )
{
   DWORD omode;

   if ( p.isRdwr() )
      omode = GENERIC_READ | GENERIC_WRITE;
   else if ( p.isRdOnly() )
      omode = GENERIC_READ;
   else
      omode = GENERIC_WRITE;

   return omode;
}


static DWORD win_paramsToShare( VFSFile::OParams p )
{
   DWORD shmode;
   if ( p.isShNone() )
      shmode = 0;
   else if( p.isShNoRead() )
      shmode = FILE_SHARE_WRITE;
   else if ( p.isShNoWrite() )
		shmode = FILE_SHARE_READ;
	else
		shmode = FILE_SHARE_READ | FILE_SHARE_WRITE;
   
   return shmode;
}


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


VFSFile::~VFSFile()
{
}


Stream *VFSFile::open( const URI& uri, const OParams &p )
{
   DWORD omode = win_paramsToMode( p );
   DWORD oshare = win_paramsToShare( p );

   String path = uri.path();
   Path::uriToWin( path );
   AutoWString wstr( path );

   HANDLE handle = CreateFileW( wstr.w_str(),
      omode,
      oshare,
      NULL,
      OPEN_EXISTING,
      0,
      NULL );

	DWORD dwError = GetLastError();
   if ( handle == 0 || handle == INVALID_HANDLE_VALUE )
   {
      if ( dwError  == ERROR_CALL_NOT_IMPLEMENTED )
      {
         AutoCString cstr( path );
         handle = CreateFile( cstr.c_str(),
               omode,
               oshare,
               NULL,
               OPEN_EXISTING,
               0,
               NULL );
      }
   }

   if ( handle == 0 || handle == INVALID_HANDLE_VALUE )
   {
      return 0;
   }

   FileStream *fs = new FileStream( new WinFileSysData( handle, 0 ) );
   return new StreamBuffer( fs );
}


Stream *VFSFile::create( const URI& uri, const CParams &p, bool &bSuccess )
{
   DWORD omode = win_paramsToMode( p );
   DWORD oshare = win_paramsToShare( p );
   DWORD ocreate = p.isNoOvr() ? 0 : CREATE_ALWAYS;
   
   // turn the xxx bytes 
   DWORD oattribs = FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE;
   if ( p.createMode()  )
   {
      // use the owner bits
      int obits = p.createMode() & 0700;

      // set read only if write bit is not set 
      if ( (obits & 0200) == 0 )
      {
         oattribs |= FILE_ATTRIBUTE_READONLY;
      }
      
      // set hidden if read bit is not set
      if ( (obits & 0400) == 0 )
      {
         oattribs |= FILE_ATTRIBUTE_HIDDEN; 
      }
   }

   String path = uri.path();
   Path::uriToWin( path );
   AutoWString wstr( path );

   HANDLE handle = CreateFileW( wstr.w_str(),
      omode,
      oshare,
      NULL,
      ocreate,
      oattribs,
      NULL );

	DWORD dwError = GetLastError();
   if ( handle == 0 || handle == INVALID_HANDLE_VALUE )
   {
      if ( dwError  == ERROR_CALL_NOT_IMPLEMENTED )
      {
         AutoCString cstr( path );
         handle = CreateFile( cstr.c_str(),
               omode,
               oshare,
               NULL,
               ocreate,
               oattribs,
               NULL );
      }
   }

   if ( handle == 0 || handle == INVALID_HANDLE_VALUE )
   {
      bSuccess = false;
      return 0;
   }

   bSuccess = true;
   // the caller may not really want to open the stream.
   if( p.isNoStream() )
   {
      CloseHandle( handle );
      return 0;
   }
      
   FileStream *fs = new FileStream( new WinFileSysData( handle, 0 ) );
   return new StreamBuffer( fs );
}


DirEntry* VFSFile::openDir( const URI& uri )
{
   int32 error = 0;
   return Sys::fal_openDir( uri.path(), error );
}


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 )
{
   return false;
}


bool VFSFile::chmod( const URI &uri, int mode )
{
   return false;
}

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


bool VFSFile::unlink( const URI &uri )
{
   int32 err = 0;
   return Sys::fal_unlink( uri.path(), err );
}

bool VFSFile::move( const URI &suri, const URI &duri )
{
   int32 err = 0;
   return Sys::fal_move( suri.path(), duri.path(), err );
}


bool VFSFile::mkdir( const URI &uri, uint32 mode )
{
   int32 err = 0;
   return Sys::fal_mkdir( uri.path(), err );
}


bool VFSFile::rmdir( const URI &uri )
{
   int32 err = 0;
   return Sys::fal_rmdir( uri.path(), err );
}


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


Error *VFSFile::getLastError()
{
   DWORD ew = GetLastError();

   if( ew != 0 )
   {
      IoError *e = new IoError( e_io_error );
      e->systemError( ew );
      return e;
   }

   return 0;
}

}

/* end of vsf_file_unix.cpp */