aboutsummaryrefslogtreecommitdiff
path: root/tests/nbody-java/xmlvm-file.c
blob: 5085d6d2d325af7a3d52aef23ec11c5f10ae0e08 (plain)
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
/*
 * Copyright (c) 2002-2011 by XMLVM.org
 *
 * Project Info:  http://www.xmlvm.org
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>

#include "xmlvm-hy.h"
#include "xmlvm-file.h"
#include "hycomp.h"


/**
 * This will convert all separators to the proper platform separator
 * and remove duplicates on non POSIX platforms.
 */
void ioh_convertToPlatform (char *path)
{
    char *pathIndex;
#if !(DIR_SEPARATOR == '/')
    size_t length = strlen (path);
#endif
    
    /* Convert all separators to the same type */
    pathIndex = path;
    while (*pathIndex != '\0')
    {
        if ((*pathIndex == '\\' || *pathIndex == '/')
            && (*pathIndex != DIR_SEPARATOR))
            *pathIndex = DIR_SEPARATOR;
        pathIndex++;
    }

#if !(DIR_SEPARATOR == '/')
    /* Remove duplicate initial separators */
    pathIndex = path;
    while ((*pathIndex != '\0') && (*pathIndex == DIR_SEPARATOR))
    {
        pathIndex++;
    }
    if ((pathIndex > path) && ((int)length > (pathIndex - path))
        && (*(pathIndex + 1) == ':'))
    {
        /* For Example '////c:/!*' (! added to avoid gcc warning) */
        size_t newlen = length - (pathIndex - path);
        memmove (path, pathIndex, newlen);
        path[newlen] = '\0';
    }
    else
    {
        if ((pathIndex - path > 3) && ((int)length > (pathIndex - path)))
        {
            /* For Example '////serverName/!*' (! added to avoid gcc warning) */
            size_t newlen = length - (pathIndex - path) + 2;
            memmove (path, pathIndex - 2, newlen);
            path[newlen] = '\0';
        }
    }
    /* This will have to handle extra \'s but currently doesn't */
#endif
    
}


static I_32
EsTranslateOpenFlags (I_32 flags)
{
    I_32 realFlags = 0;
    
    if (flags & HyOpenAppend)
    {
        realFlags |= O_APPEND;
    }
    if (flags & HyOpenTruncate)
    {
        realFlags |= O_TRUNC;
    }
    if (flags & HyOpenCreate)
    {
        realFlags |= O_CREAT;
    }
    if (flags & HyOpenCreateNew)
    {
        realFlags |= O_EXCL | O_CREAT;
    }
#ifdef O_SYNC
	if (flags & HyOpenSync) {
		realFlags |= O_SYNC;
	}
#endif    
    if (flags & HyOpenRead)
    {
        if (flags & HyOpenWrite)
        {
            return (O_RDWR | realFlags);
        }
        return (O_RDONLY | realFlags);
    }
    if (flags & HyOpenWrite)
    {
        return (O_WRONLY | realFlags);
    }
    return -1;
}


static I_32 findError (I_32 errorCode)
{
    switch (errorCode)
    {
        case EACCES:
        case EPERM:
            return HYPORT_ERROR_FILE_NOPERMISSION;
        case ENAMETOOLONG:
            return HYPORT_ERROR_FILE_NAMETOOLONG;
        case ENOENT:
            return HYPORT_ERROR_FILE_NOENT;
        case ENOTDIR:
            return HYPORT_ERROR_FILE_NOTDIR;
        case ELOOP:
            return HYPORT_ERROR_FILE_LOOP;
            
        case EBADF:
            return HYPORT_ERROR_FILE_BADF;
        case EEXIST:
            return HYPORT_ERROR_FILE_EXIST;
        case ENOSPC:
        case EFBIG:
            return HYPORT_ERROR_FILE_DISKFULL;
        default:
            return HYPORT_ERROR_FILE_OPFAILED;
    }
}



I_32 hyfile_close (IDATA fd)
{
    
#if (FD_BIAS != 0)
    if (fd < FD_BIAS) {
        /* Cannot close STD streams, and no other FD's should exist <FD_BIAS */
	    return -1;
    }
#endif
    
    return close ((int) (fd - FD_BIAS));
}



IDATA hyfile_open (const char *path, I_32 flags, I_32 mode)
{
    struct stat buffer;
    I_32 fd;
    I_32 realFlags = EsTranslateOpenFlags (flags);
    I_32 fdflags;
    
    // Trc_PRT_file_open_Entry (path, flags, mode);
    
    if (realFlags == -1)
    {
        // Trc_PRT_file_open_Exit1 (flags);
        hyerror_set_last_error(EINVAL, findError(EINVAL));
        return -1;
    }
    
    if ( ( flags&HyOpenRead && !(flags&HyOpenWrite) )  && !stat (path, &buffer))
    {
        if (S_ISDIR (buffer.st_mode))
        {
            hyerror_set_last_error_with_message(findError(EEXIST), "Is a directory");
            // Trc_PRT_file_open_Exit4 ();
            return -1;
        }
    }
    
    fd = open (path, realFlags, mode);
    
    if (-1 == fd)
    {
        // Trc_PRT_file_open_Exit2 (errno, findError (errno));
        int rc = errno;
        hyerror_set_last_error(rc, findError(rc));
        return -1;
    }
    
    /* Tag this descriptor as being non-inheritable */
    fdflags = fcntl (fd, F_GETFD, 0);
    fcntl (fd, F_SETFD, fdflags | FD_CLOEXEC);
    
    fd += FD_BIAS;
    // Trc_PRT_file_open_Exit (fd);
    return (IDATA) fd;
}



IDATA hyfile_read (IDATA fd, void *buf, IDATA nbytes)
{
    IDATA result;
    if (nbytes == 0)
    {
        return 0;
    }
    
#ifdef ZOS
    if (fd == HYPORT_TTY_IN) {
        result = fread(buf, sizeof(char), nbytes, stdin);
    }  else	if (fd < FD_BIAS) {
        /* Cannot read from STDOUT/ERR, and no other FD's should exist <FD_BIAS */
        return -1;
    } else
#endif /* ZOS */
    {
        result = read ((int) (fd - FD_BIAS), buf, nbytes);
    }
    
    if (result == 0)
    {
        return -1;
    }
    else
    {
        return result;
    }
}



I_64 hyfile_seek (IDATA inFD, I_64 offset, I_32 whence)
{
  int fd = (int)inFD;
  off_t localOffset = (off_t) offset;

  if ((whence < HySeekSet) || (whence > HySeekEnd))
    {
      return -1;
    }

  /* If file offsets are 32 bit, truncate the seek to that range */
  if (sizeof (off_t) < sizeof (I_64))
    {
      if (offset > 0x7FFFFFFF)
        {
          localOffset = 0x7FFFFFFF;
        }
      else if (offset < -0x7FFFFFFF)
        {
          localOffset = -0x7FFFFFFF;
        }
    }

#if (FD_BIAS != 0)
	if (fd < FD_BIAS) {
		/* Cannot seek on STD streams, and no other FD's should exist <FD_BIAS */
		return -1;
	}
#endif

  return (I_64) lseek ((int) (fd - FD_BIAS), localOffset, whence);
}



IDATA hyfile_write (IDATA fd, const void *buf, IDATA nbytes)
{
    IDATA rc = 0;
    
#ifdef ZOS
    if (fd == HYPORT_TTY_OUT) {
        rc = fwrite(buf, sizeof(char), nbytes, stdout);
    } else if (fd == HYPORT_TTY_ERR) {
        rc = fwrite(buf, sizeof(char), nbytes, stderr);
    } else if (fd < FD_BIAS) {
        /* Cannot fsync STDIN, and no other FD's should exist <FD_BIAS */
        return -1;
    } else 
#endif /* ZOS */
    {
        /* write will just do the right thing for HYPORT_TTY_OUT and HYPORT_TTY_ERR */
        rc = write ((int) (fd - FD_BIAS), buf, nbytes);
    }
    
    if (rc == -1)
    {
        int rc = errno;
        return hyerror_set_last_error(rc, findError(rc));
    }
    
    return rc;
}