/*------------------------------------------------------------------------ * rstrunc - handle a truncate request *------------------------------------------------------------------------ */ void rstrunc ( struct rf_msg_treq *reqptr, /* ptr to read request */ struct rf_msg_tres *resptr /* ptr to read response */ ) { int fd; /* file descriptor */ /* DEBUG */ printf("DEBUG: reached rstrunc\n"); /* if file is open, close it */ if (findex >= 0) { close(fptr->desc); fptr->desc = -1; } fd = open(reqptr->rf_name, O_TRUNC); if (fd < 0) { snderr( (struct rf_msg_hdr *)reqptr, (struct rf_msg_hdr *)resptr, sizeof(struct rf_msg_tres) ); } else { close(fd); } /* Return OK status */ sndok ( (struct rf_msg_hdr *)reqptr, (struct rf_msg_hdr *)resptr, sizeof(struct rf_msg_tres) ); return; }
/*------------------------------------------------------------------------ * rsdelete - handle a delete request *------------------------------------------------------------------------ */ void rsdelete ( struct rf_msg_dreq *reqptr, /* ptr to read request */ struct rf_msg_dres *resptr /* ptr to read response */ ) { int retval; /* return value */ /*DEBUG*/ printf("DEBUG: reached rsdelete\n"); /* if file is open, close it */ if (findex >= 0) { close(fptr->desc); fptr->desc = -1; } retval = unlink(reqptr->rf_name); if (retval < 0) { snderr( (struct rf_msg_hdr *)reqptr, (struct rf_msg_hdr *)resptr, sizeof(struct rf_msg_dres) ); return; } /* Return OK status */ sndok ( (struct rf_msg_hdr *)reqptr, (struct rf_msg_hdr *)resptr, sizeof(struct rf_msg_sres) ); return; }
/*------------------------------------------------------------------------ * rsstat - handle a stat request *------------------------------------------------------------------------ */ void rsstat ( struct rf_msg_sreq *reqptr, /* ptr to read request */ struct rf_msg_sres *resptr /* ptr to read response */ ) { struct stat stbuf; /* buffer for file status */ int sreturn; /* stat return value */ sreturn = stat(reqptr->rf_name, &stbuf); if (sreturn < 0) { /* file does not exist */ resptr->rf_size = 0; snderr( (struct rf_msg_hdr *)reqptr, (struct rf_msg_hdr *)resptr, sizeof(struct rf_msg_sres) ); return; } /* Place the file size in the response message */ resptr->rf_size = htonl(stbuf.st_size); /* Copy the header and send the response */ sndok ( (struct rf_msg_hdr *)reqptr, (struct rf_msg_hdr *)resptr, sizeof(struct rf_msg_sres) ); return; }
/*------------------------------------------------------------------------ * rsread - handle a read request *------------------------------------------------------------------------ */ void rsread ( struct rf_msg_rreq *reqptr, /* ptr to read request */ struct rf_msg_rres *resptr /* ptr to read response */ ) { int fd; /* file descriptor */ int i, j; /* general loop indexes */ struct stat buf; /* buffer for file status */ int retval; /* function return value */ char *to, *from; /* used during copy */ int nbytes; /* num. of bytes read from file */ retval = stat(reqptr->rf_name, &buf); if(retval >= 0) { if(buf.st_mode & S_IFDIR) { rsdirread(reqptr, resptr); return; } } /* if file is not open, try to open it */ if (findex < 0) { if ( (fd = rsofile(reqptr->rf_name,O_RDWR)) < 0 ) { resptr->rf_pos = reqptr->rf_pos; resptr->rf_len = 0; /* set length to zero */ snderr( (struct rf_msg_hdr *)reqptr, (struct rf_msg_hdr *)resptr, sizeof(struct rf_msg_rreq) ); return; } for (i=0; i<MAXFILES; i++) { /* If entry is free, stop searching */ if (ofiles[i].desc < 0) { break; } } if (i >= MAXFILES) { /* table is full */ i = fnext; /* choose entry to close */ close(ofiles[fnext].desc); /* move to next slot for the future , wrap */ /* around the table, if necessary */ fnext++; if (fnext >= MAXFILES) { fnext = 0; } } ofiles[i].desc = fd; from = reqptr->rf_name; to =ofiles[i].name; /* copy name to open file table */ while ( (*to++ = *from++) ) { ; } } else { fd = fptr->desc; } /* stat file to get its current size */ retval = fstat(fd, &buf); /* if requested offset is beyond EOF, return error */ if ( (retval<0) || (ntohl(reqptr->rf_pos) > buf.st_size) ) { resptr->rf_pos = reqptr->rf_pos; resptr->rf_len = 0; /* set length to zero */ snderr( (struct rf_msg_hdr *)reqptr, (struct rf_msg_hdr *)resptr, sizeof(struct rf_msg_rreq) ); return; } /* seek to specified offset and read data */ lseek(fd, ntohl(reqptr->rf_pos), SEEK_SET); memset(resptr->rf_data, NULLCH, RF_DATALEN); nbytes = read(fd, resptr->rf_data, ntohl(reqptr->rf_len)); if (nbytes < 0) { resptr->rf_pos = reqptr->rf_pos; resptr->rf_len = 0; /* set length to zero */ snderr( (struct rf_msg_hdr *)reqptr, (struct rf_msg_hdr *)resptr, sizeof(struct rf_msg_rreq) ); return; } /* set bytes read */ resptr->rf_pos = reqptr->rf_pos; resptr->rf_len = htonl(nbytes); sndok( (struct rf_msg_hdr *)reqptr, (struct rf_msg_hdr *)resptr, sizeof(struct rf_msg_rres) ); return; }