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
|
/*
* Copyright (c) 2001-2002 The Trustees of Indiana University.
* All rights reserved.
* Copyright (c) 1998-2001 University of Notre Dame.
* All rights reserved.
* Copyright (c) 1994-1998 The Ohio State University.
* All rights reserved.
*
* This file is part of the LAM/MPI software package. For license
* information, see the LICENSE file in the top level directory of the
* LAM/MPI source distribution.
*
* $HEADER$
*
* $Id: fface.c,v 6.10 2003/06/30 05:19:30 brbarret Exp $
*
* Function: - abstracts native filesystem interface
* - Trollius remote file access follows the UNIX I/O
* standard assuming a 32 bit interface (int4).
* - In case the standard UNIX calls are not available,
* they may be emulated in this module.
* - UNIX version
*/
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include "filed.h"
#include <freq.h>
#include <net.h>
#include <typical.h>
/*
* local functions
*/
static void conv_stat();
static int4 conv_flags();
/*
* UNIX version - boring, so just blurt them out.
* Here we know that int4 is int.
* In fmyopen(), convert LAM flags to their UNIX equivalent.
*/
int4
fmyopen(path, flags, mode)
char *path;
int4 flags;
int4 mode;
{
int4 fd;
fd = open(path, conv_flags(flags) | O_NOCTTY, mode);
return(fd);
}
int4 fmyclose(fd) int4 fd;
{ return(close(fd)); }
int4 fmyread(fd, buf, nbytes) int4 fd; char *buf; int4 nbytes;
{ return(read(fd, buf, nbytes)); }
int4 fmywrite(fd, buf, nbytes) int4 fd; char *buf; int4 nbytes;
{ return(write(fd, buf, nbytes)); }
int4 fmylseek(fd, offset, whence) int4 fd; int4 offset; int4 whence;
{ return(lseek(fd, (off_t) offset, whence)); }
int4 fmychdir(newdir) char *newdir;
{ return(chdir(newdir)); }
char *fmygetwd(dir) char *dir;
#if LAM_HAVE_GETCWD
{ return(getcwd(dir, MAXNMSGLEN)); }
#else
{ return(getwd(dir)); }
#endif
int4 fmyunlink(name) char *name;
{ return(unlink(name)); }
int4 fmyrmdir(dir) char *dir;
{ return(rmdir(dir)); }
int4 fmysystem(cmd) char *cmd;
{ return(system(cmd)); }
int4 fmymkdir(path, mode) char *path; int4 mode;
{ return(mkdir(path, mode)); }
int4 fmyaccess(path, mode) char *path; int4 mode;
{ return(access(path, mode)); }
int4
fmyrename(CONST char *from, CONST char *to)
{
return rename(from, to);
}
#ifndef SCO
int4 fmytrunc(path, len) char *path; int4 len;
{ return(truncate(path, (off_t) len)); }
int4 fmyftrunc(fd, len) int4 fd; int4 len;
{ return(ftruncate(fd, (off_t) len)); }
#else /* SCO */
/*
* Simulate truncate() by opening the file.
*/
int4 fmytrunc(path, len) char *path; int4 len;
{
int4 fd;
fd = open(path, O_WRONLY, 0);
if (fd < 0) {
return((int4) ERROR);
}
if (chsize(fd, (off_t) len)) {
return((int4) ERROR);
}
return((int4) close(fd));
}
int4 fmyftrunc(fd, len) int4 fd; int4 len;
{ return(chsize(fd, (off_t) len)); }
#endif /* SCO */
/*
* fmyinit
*
* Function: - machine dependent initialization
* - in UNIX case, take care of process group/
* controlling terminal headaches
* Returns: - 0 or ERROR
*/
int
fmyinit()
{
/*
* Create a new session. This detaches our controlling terminal
* so we don't get TTIN and TTOU signals when reading/writing the tty.
* We also get out of the tty's process group so that we don't
* catch special character signals from it.
*
* In the future, we will have worker processes to deal with ttys
* (and all weird and/or slow devices) so we won't have this hassle.
*/
/*
* I thought I vaguely understood this subject. Current testing reveals
* that neither of the above two problems exists if I fail to call setsid().
* It is clearly documented that I cannot make this call if I am already
* a session leader and I want to be able to run lamd from the shell.
*
* if (setsid() < 0) {
* return(ERROR);
* }
*/
return(0);
}
/*
* fmystat
*/
int4
fmystat(path, buf)
char *path;
struct stat *buf;
{
struct stat status;
int4 ret;
ret = stat(path, &status);
conv_stat(&status, (unsigned int *) buf);
return(ret);
}
/*
* fmyfstat
*/
int4
fmyfstat(fd, buf)
int4 fd;
struct stat *buf;
{
struct stat status;
int4 ret;
ret = fstat(fd, &status);
conv_stat(&status, (unsigned int *) buf);
return(ret);
}
#if !LAM_STAT_MEMBERS_INT4
/*
* stuff_buff
*
* Function: - stuff a buffer with a quantity
* - zero-pads dest if dest is longer than src
* Accepts: - two buffers and corresponding sizes
*/
static void
stuff_buff(char *dest, int dest_size, char *src, int src_size)
{
if (src_size >= dest_size)
memcpy(dest, src, dest_size);
else {
memcpy(dest, src, src_size);
memset(dest + src_size, 0, dest_size - src_size);
}
}
#endif
/*
* conv_stat
*
* Function: - stores status structure in an int4 array
* Accepts: - ptr to stat structure
* - ptr to int4 array
*/
static void
conv_stat(status, buf)
struct stat *status;
uint4 *buf;
{
#if LAM_STAT_MEMBERS_INT4
buf[0] = (uint4) status->st_dev;
buf[1] = (uint4) status->st_ino;
buf[2] = (uint4) status->st_mode;
buf[3] = (uint4) status->st_nlink;
buf[4] = (uint4) status->st_uid;
buf[5] = (uint4) status->st_gid;
buf[6] = (uint4) status->st_size;
buf[7] = (uint4) status->st_atime;
buf[8] = (uint4) status->st_mtime;
buf[9] = (uint4) status->st_ctime;
#else
stuff_buff((char*) &buf[0], sizeof(uint4),
(char*) &status->st_dev, sizeof(status->st_dev));
stuff_buff((char*) &buf[1], sizeof(uint4),
(char*) &status->st_ino, sizeof(status->st_ino));
stuff_buff((char*) &buf[2], sizeof(uint4),
(char*) &status->st_mode, sizeof(status->st_mode));
stuff_buff((char*) &buf[3], sizeof(uint4),
(char*) &status->st_nlink, sizeof(status->st_nlink));
stuff_buff((char*) &buf[4], sizeof(uint4),
(char*) &status->st_uid, sizeof(status->st_uid));
stuff_buff((char*) &buf[5], sizeof(uint4),
(char*) &status->st_gid, sizeof(status->st_gid));
stuff_buff((char*) &buf[6], sizeof(uint4),
(char*) &status->st_size, sizeof(status->st_size));
stuff_buff((char*) &buf[7], sizeof(uint4),
(char*) &status->st_atime, sizeof(status->st_atime));
stuff_buff((char*) &buf[8], sizeof(uint4),
(char*) &status->st_mtime, sizeof(status->st_mtime));
stuff_buff((char*) &buf[9], sizeof(uint4),
(char*) &status->st_ctime, sizeof(status->st_ctime));
#endif
}
/*
* conv_flags
*
* Function: - convert LAM flags to UNIX open() flags
* Accepts: - LAM flags
* Returns: - UNIX flags
*/
static int4
conv_flags(lflags)
int4 lflags;
{
int4 uflags;
uflags = 0;
if (lflags & LAM_O_RDONLY) { uflags |= O_RDONLY; }
if (lflags & LAM_O_WRONLY) { uflags |= O_WRONLY; }
if (lflags & LAM_O_RDWR) { uflags |= O_RDWR; }
if (lflags & LAM_O_APPEND) { uflags |= O_APPEND; }
if (lflags & LAM_O_CREAT) { uflags |= O_CREAT; }
if (lflags & LAM_O_EXCL) { uflags |= O_EXCL; }
if (lflags & LAM_O_TRUNC) { uflags |= O_TRUNC; }
return(uflags);
}
|