FileNative.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel */
40 /* */
41 /* Stream processing (stream oriented file systems) */
42 /* */
43 /******************************************************************************/
44 
45 #include "oorexxapi.h"
46 #include <fcntl.h>
47 #include <sys/stat.h>
48 #include <string.h>
49 #include <errno.h>
50 #include "SysFileSystem.hpp"
51 
52 /**
53  * Return the file name separator used by the file system.
54  */
55 RexxMethod0(CSTRING, file_separator)
56 {
58 }
59 
60 /**
61  * Return the separator used for file search paths
62  */
63 RexxMethod0(CSTRING, file_path_separator)
64 {
66 }
67 
68 
69 /**
70  * Return the file system case sensitivity section
71  */
72 RexxMethod0(logical_t, file_case_sensitive)
73 {
75 }
76 
77 
78 /**
79  * Return the file system case sensitivity section
80  */
81 RexxMethod1(logical_t, file_can_read, CSTRING, name)
82 {
84 }
85 
86 
87 /**
88  * Return the file system case sensitivity section
89  */
90 RexxMethod1(logical_t, file_can_write, CSTRING, name)
91 {
92  return SysFileSystem::exists(name) && !SysFileSystem::isReadOnly(name);
93 }
94 
95 
96 /**
97  * Return the list of file system root elements.
98  */
99 RexxMethod0(RexxArrayObject, file_list_roots)
100 {
101  char rootBuffer[SysFileSystem::MaximumPathLength];
102  int count = SysFileSystem::getRoots(rootBuffer);
103 
104  const char *roots = rootBuffer;
105 
106  RexxArrayObject result = context->NewArray(count);
107  for (int i = 0; i < count; i++)
108  {
109  context->ArrayAppendString(result, roots, strlen(roots));
110  roots += strlen(roots) + 1;
111  }
112 
113  return result;
114 }
115 
116 
117 /**
118  * Create a fully-qualified path name for a file.
119  */
120 RexxMethod1(RexxStringObject, file_qualify, CSTRING, name)
121 {
122  char qualified_name[SysFileSystem::MaximumFileNameLength];
123  // qualifyStreamName will not expand if not a null string on entry.
124  qualified_name[0] = '\0';
125  SysFileSystem::qualifyStreamName(name, qualified_name, sizeof(qualified_name));
126  return context->String(qualified_name);
127 }
128 
129 
130 /**
131  * Test if the file exists
132  */
133 RexxMethod1(logical_t, file_exists, CSTRING, name)
134 {
135  return SysFileSystem::exists(name);
136 }
137 
138 
139 /**
140  * Delete a file
141  */
142 RexxMethod1(logical_t, file_delete_file, CSTRING, name)
143 {
144  return SysFileSystem::deleteFile(name);
145 }
146 
147 
148 /**
149  * Delete a directory
150  */
151 RexxMethod1(logical_t, file_delete_directory, CSTRING, name)
152 {
153  return SysFileSystem::deleteDirectory(name);
154 }
155 
156 
157 /**
158  * Tests if the file is a directory
159  */
160 RexxMethod1(logical_t, file_isDirectory, CSTRING, name)
161 {
162  return SysFileSystem::isDirectory(name);
163 }
164 
165 
166 /**
167  * Tests if the file is a file
168  */
169 RexxMethod1(logical_t, file_isFile, CSTRING, name)
170 {
171  return SysFileSystem::isFile(name);
172 }
173 
174 
175 /**
176  * Tests if the file hidden
177  */
178 RexxMethod1(logical_t, file_isHidden, CSTRING, name)
179 {
180  return SysFileSystem::isHidden(name);
181 }
182 
183 
184 /**
185  * Return the last modified date as a Ticks time value.
186  */
187 RexxMethod1(int64_t, file_get_last_modified, CSTRING, name)
188 {
190 }
191 
192 
193 /**
194  * Return the last modified date as a Ticks time value.
195  */
196 RexxMethod2(logical_t, file_set_last_modified, CSTRING, name, int64_t, time)
197 {
198  return SysFileSystem::setLastModifiedDate(name, time);
199 }
200 
201 
202 /**
203  * Set the read-only flag for the target file
204  */
205 RexxMethod1(logical_t, file_set_read_only, CSTRING, name)
206 {
207  return SysFileSystem::setFileReadOnly(name);
208 }
209 
210 
211 /**
212  * Return the last modified date as a Ticks time value.
213  */
214 RexxMethod1(uint64_t, file_length, CSTRING, name)
215 {
216  return SysFileSystem::getFileLength(name);
217 }
218 
219 
220 /**
221  * Get a list of the file children for a directory.
222  */
224 {
225  if (!SysFileSystem::isDirectory(name))
226  {
227  return context->Nil();
228  }
229 
230  // create an empty array to start
231  RexxArrayObject result = context->NewArray(0);
232 
233  SysFileIterator iterator(name);
234  while (iterator.hasNext())
235  {
237  iterator.next(buffer);
238  // don't include the "." and ".." directories in this list
239  if (strcmp(buffer, ".") != 0 && strcmp(buffer, "..") != 0)
240  {
241  context->ArrayAppendString(result, buffer, strlen(buffer));
242  }
243  }
244 
245  return result;
246 }
247 
248 
249 /**
250  * Make a directory instance
251  */
252 RexxMethod1(logical_t, file_make_dir, CSTRING, name)
253 {
254  return SysFileSystem::makeDirectory(name);
255 }
256 
257 
258 /**
259  * Rename a file.
260  */
261 RexxMethod2(logical_t, file_rename, CSTRING, fromName, CSTRING, toName)
262 {
263  return SysFileSystem::moveFile(fromName, toName);
264 }
265 
266 
RexxMethod2(logical_t, file_set_last_modified, CSTRING, name, int64_t, time)
Definition: FileNative.cpp:196
RexxMethod0(CSTRING, file_separator)
Definition: FileNative.cpp:55
RexxMethod1(logical_t, file_can_read, CSTRING, name)
Definition: FileNative.cpp:81
void next(char *buffer)
static bool moveFile(const char *oldName, const char *newName)
static bool setLastModifiedDate(const char *name, int64_t time)
static const char * getSeparator()
static bool deleteFile(const char *name)
static bool deleteDirectory(const char *name)
static bool setFileReadOnly(const char *name)
static int64_t getLastModifiedDate(const char *name)
static const char * getPathSeparator()
static int getRoots(char *roots)
static bool makeDirectory(const char *name)
static bool isDirectory(const char *name)
static bool isCaseSensitive()
static uint64_t getFileLength(const char *name)
static bool isWriteOnly(const char *name)
static void qualifyStreamName(const char *unqualifiedName, char *qualifiedName, size_t bufferSize)
static bool isHidden(const char *name)
static bool isReadOnly(const char *name)
static bool exists(const char *name)
static bool isFile(const char *name)
const char * CSTRING
Definition: rexx.h:78
size_t logical_t
Definition: rexx.h:231
struct _RexxStringObject * RexxStringObject
Definition: rexx.h:128
struct _RexxArrayObject * RexxArrayObject
Definition: rexx.h:130
struct _RexxObjectPtr * RexxObjectPtr
Definition: rexx.h:127
signed __int64 int64_t
unsigned __int64 uint64_t