GLAST / LAT > DAQ and FSW > FSW > Doxygen Index> FBS / V0-3-1 > fbs_rtos / rad750
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <PBI/Endianness.h>
#include <PBS/BSWP.ih>
#include <PBS/FFS.ih>
#include <PBS/INT.ih>
#include <ZLIB/zlib.h>
#include <MDB/MDB_pubdefs.h>
#include <FBS/FBS_msgs.h>
#include <FBS/FBS_pubrtos.h>
#include <FBS_mtx.ih>
#include <FBS_prvrtos.h>
Classes | |
struct | _FILE_Stat |
Block of information associated with a file opened with FILE_open(). More... | |
Defines | |
#define | INT_FAKE_OK |
Allow unix to fake an interrupt lock. | |
#define | MKDIR(_a, _b) mkdir( _a, _b ) |
Gloss over differences in the unix and VxWorks mkdir calls. | |
#define | UMASK(_a, _b) _a = umask( _b ) |
#define | FILE_K_BITALLOC (2) |
Number of unsigned ints used in bit allocation scheme. | |
#define | FILE_L_TEMPNAME (256) |
Maximum length of a temporary filename. | |
#define | FILE_ACS_SIZE (2048) |
Size of buffer to use during adler32 checksum verification. | |
Typedefs | |
typedef struct _FILE_Stat | FILE_Stat |
Typedef for struct _FILE_Stat. | |
Functions | |
static unsigned int | FILE_inflate (int in, int out, unsigned int bufSize) |
File inflater. | |
static int | FILE_findSlot (int fd) |
Find the slot describing fd. | |
static void * | FILE_inflateAlloc (void *opaque, unsigned int numItems, unsigned int size) |
Memory allocation callback for ZLIB. | |
static unsigned int | FILE_inflateError (int errCode, const char *func, const char *msg) |
This function translates ZLIB native error codes into MSG codes. | |
static void | FILE_inflateFree (void *opaque, void *address) |
Memory free callback for ZLIB. | |
static char * | FILE_tempname (int idx, char *buf, unsigned int len) |
Generate a temporary file name. | |
unsigned int | FILE_check (const char *fil, unsigned int *key, unsigned short *typ) |
Check the integrity of a file. | |
int | FILE_close (int fd) |
Close a FSW file (regular, headed/not-headed, compressed/not-compressed). | |
unsigned int | FILE_decodePrimary (unsigned char *buf, unsigned int cnt, unsigned int *ver, unsigned int *cmp, unsigned short *typ, unsigned int *key, unsigned int *fcs, unsigned int *len) |
Assuming buf points to a file header, decode it. | |
unsigned int | FILE_getCompression (int fd) |
Get the compression flag associated with the file descriptor. | |
unsigned int | FILE_getKey (int fd) |
Get the value of the key associated with the file descriptor. | |
int | FILE_getName (int fd, char *nam, unsigned int len) |
Get the original file name associated with the file descriptor. | |
unsigned int | FILE_getSize (int fd) |
Get the file header size associated with the file descriptor. | |
unsigned short | FILE_getType (int fd) |
Get the value of the type associated with the file descriptor. | |
unsigned int | FILE_getVersion (int fd) |
Get the file header version associated with the file descriptor. | |
int | FILE_open (const char *fil, int flg, mode_t mod) |
Open a FSW file (regular, headed/not-headed, compressed/not-compressed). | |
Variables | |
static FBS_mti | mti |
Instance of (storage for) mutex (only used in Unix). | |
static volatile FBS_mtx | mtx |
Mutex to enforce serial open/close/delete operations on a single file. | |
static unsigned int | bookbits [FILE_K_BITALLOC] |
unsigned ints used in a bit allocation scheme to indicate booked slots | |
static FILE_Stat | bookslot [FILE_K_BITALLOC<< 5] |
Slots in which to remember file descriptors for "delete-on-close". | |
char | dir [] = { "/tmp/d000/" } |
Directory prefix for a temporary inflation file. |
CVS $Id: FBS_open.c,v 1.9 2011/03/24 21:24:42 apw Exp $
unsigned int FILE_check | ( | const char * | fil, | |
unsigned int * | key, | |||
unsigned short * | typ | |||
) |
Check the integrity of a file.
fil | (in) File name | |
key | (out) File key (from header) (can be NULL) | |
typ | (out) File type (from header) (can be NULL) |
FBS_SUCCESS | File integrity check passed | |
FBS_CHKFAIL | File interrity check failed |
References FILE_ACS_SIZE, FILE_decodePrimary(), FILE_E_CORRUPT, FILE_E_MMAPHEAD, and FILE_K_HDR_SIZ.
int FILE_close | ( | int | fd | ) |
Close a FSW file (regular, headed/not-headed, compressed/not-compressed).
fd | (in) File descriptor (conventional meaning) |
0 | Success | |
-1 | Failure (use errno to retrieve further details) |
References FBS_mtx__unlock(), _FILE_Stat::fd, FILE_findSlot(), FILE_L_TEMPNAME, and FILE_tempname().
unsigned int FILE_decodePrimary | ( | unsigned char * | buf, | |
unsigned int | cnt, | |||
unsigned int * | ver, | |||
unsigned int * | cmp, | |||
unsigned short * | typ, | |||
unsigned int * | key, | |||
unsigned int * | fcs, | |||
unsigned int * | len | |||
) |
Assuming buf points to a file header, decode it.
buf | (in) Pointer to buffer containing supposed file header | |
cnt | (in) Byte count in buffer pointed to by buf | |
ver | (out) Header version number (0 if no file header) | |
cmp | (out) Header compression flag (0 if no file header) | |
typ | (out) File type (0xffff if no file header) | |
key | (out) File key (0xffffffff if no file header) | |
fcs | (out) File body checksum (0xffffffff if no file header) | |
len | (out) File body length (0xffffffff if no file header) |
0 | The buffer does not contain a file header | |
x | Offset in buf to where the file body begins |
References _FILE_PrimaryHeader::ctl, _FILE_PrimaryHeader::fcs, FILE_M_HDR_CMP, FILE_M_HDR_SIZ, FILE_M_HDR_VER, FILE_V_HDR_CMP, FILE_V_HDR_SIZ, FILE_V_HDR_VER, _FILE_PrimaryHeader::hcs, _FILE_PrimaryHeader::key, _FILE_PrimaryHeader::len, and _FILE_PrimaryHeader::typ.
Referenced by FILE_check(), FILE_loadModule(), and FILE_open().
static int FILE_findSlot | ( | int | fd | ) | [static] |
Find the slot describing fd.
fd | (in) File descriptor (conventional meaning) |
x | Slot index of fd (and mutex is set). | |
-1 | Failure (and mutex is released) |
References FBS_mtx__create(), FBS_mtx__destroy(), FBS_mtx__lock(), FBS_mtx__unlock(), and FILE_K_BITALLOC.
Referenced by FILE_close(), FILE_getCompression(), FILE_getKey(), FILE_getName(), FILE_getSize(), FILE_getType(), and FILE_getVersion().
unsigned int FILE_getCompression | ( | int | fd | ) |
Get the compression flag associated with the file descriptor.
fd | (in) File descriptor (conventional meaning) |
1 | File compressed | |
0 | File not compressed |
If these conditions are not met, the guard value 0 is returned. Note that this is the compression bit from the original file open. If the original file is compressed, FILE_open() automatically runs a decompression step and points the caller to a decompressed version of the file.
References _FILE_Stat::ctl, FBS_mtx__unlock(), FILE_findSlot(), FILE_K_HDR_CMP, and FILE_M_HDR_CMP.
unsigned int FILE_getKey | ( | int | fd | ) |
Get the value of the key associated with the file descriptor.
fd | (in) File descriptor (conventional meaning) |
x | Key value | |
-1 | Failure (well OK, it's really 0xffffffff) |
If these conditions are not met, the guard value 0xffffffff is returned.
References FBS_mtx__unlock(), FILE_findSlot(), and _FILE_Stat::key.
int FILE_getName | ( | int | fd, | |
char * | nam, | |||
unsigned int | len | |||
) |
Get the original file name associated with the file descriptor.
fd | (in) File descriptor (conventional meaning) | |
nam | (out) Buffer into which to place name | |
len | (out) Maximum length of name buffer |
x | True length of file name | |
-1 | Failure (see below) |
If these conditions are not met, the guard value -1 is returned.
Assuming the above conditions are met, this routine will always return a zero terminated string in nam, even if nam is not sufficiently long to accomodate the full name. To check for this case, the caller should compare the string length of nam against the true name length, which is returned by this function.
References FBS_mtx__unlock(), FILE_findSlot(), and _FILE_Stat::nam.
unsigned int FILE_getSize | ( | int | fd | ) |
Get the file header size associated with the file descriptor.
fd | (in) File descriptor (conventional meaning) |
x | File header size | |
0 | Guard value |
If these conditions are not met, the guard value 0 is returned.
Note that the size returned is literally the number stored in the file header, and does not include the four bytes at the start of the header used to store the file header checksum.
References _FILE_Stat::ctl, FBS_mtx__unlock(), FILE_findSlot(), FILE_K_HDR_SIZ, and FILE_M_HDR_SIZ.
unsigned short FILE_getType | ( | int | fd | ) |
Get the value of the type associated with the file descriptor.
fd | (in) File descriptor (conventional meaning) |
x | Type value | |
-1 | Failure (well OK, it's really 0xffff) |
If these conditions are not met, the guard value 0xffff is returned.
References FBS_mtx__unlock(), FILE_findSlot(), and _FILE_Stat::typ.
unsigned int FILE_getVersion | ( | int | fd | ) |
Get the file header version associated with the file descriptor.
fd | (in) File descriptor (conventional meaning) |
x | Type value | |
0 | Failure |
If these conditions are not met, the guard value 0 is returned.
References _FILE_Stat::ctl, FBS_mtx__unlock(), FILE_findSlot(), FILE_K_HDR_VER, and FILE_M_HDR_VER.
static unsigned int FILE_inflate | ( | int | in, | |
int | out, | |||
unsigned int | bufSize | |||
) | [static] |
File inflater.
in | (in) Input file descriptor | |
out | (in) Output file descriptor | |
bufSize | (in) Buffer length |
References FILE_inflateAlloc(), FILE_inflateError(), and FILE_inflateFree().
Referenced by FILE_open().
static void * FILE_inflateAlloc | ( | void * | prm, | |
unsigned int | nitem, | |||
unsigned int | size | |||
) | [inline, static] |
Memory allocation callback for ZLIB.
prm | (in) Context parameter (unused) | |
nitem | (in) Number of items to allocate. | |
size | (in) Size (bytes) of each item. |
Referenced by FILE_inflate().
static unsigned int FILE_inflateError | ( | int | zcode, | |
const char * | func, | |||
const char * | msg | |||
) | [static] |
This function translates ZLIB native error codes into MSG codes.
zcode | (in) The ZLIB error code. | |
func | (in) The name of the function in which the error occurred. | |
msg | (in) A message to print out in the MSG output. |
Referenced by FILE_inflate().
static void FILE_inflateFree | ( | void * | prm, | |
void * | add | |||
) | [inline, static] |
Memory free callback for ZLIB.
prm | (in) Context parameter (unused) | |
add | (in) Address of memory to free |
Referenced by FILE_inflate().
int FILE_open | ( | const char * | fil, | |
int | flg, | |||
mode_t | mod | |||
) |
Open a FSW file (regular, headed/not-headed, compressed/not-compressed).
fil | (in) File name | |
flg | (in) Open flags (O_RDONLY, O_WRONLY, O_RDWR, O_CREAT) | |
mod | (in) Access mode (only used for NFS file creation) |
x | File descriptor | |
-1 | Failure |
Flight software files appear in three different formats:
When FILE_open() is asked to open any of these formats, it will first inspect the file to classify it. The adler32 checksum in the header is very powerful here. Subsequent processing depends on the file format:
/tmp/d000
on unix, in /ram/d000
in VxWorks). The original file is closed, the inflated file is opened O_RDONLY, and a file descriptor for the inflated file is returned to the caller. The temporary file is deleted when the file is closed (though the caller must use FILE_close() to close the file, not a simple close() call).In keeping with the idea that FILE_open() is modeled on open(), the errno method is used to return extra information. One unusual feature of this is that errno can contain helpful information, even when the file open is successful.
If the return value is not -1 (i.e. the open succeeded), errno can contain:
If the return value is -1 (i.e. the open failed), errno, in addition to any of the regular values that open() can return, can contain:
If you wish to use these values for additional checking, note that errno is a four byte word encoded as two two-byte units. The most significant unit is a module number and the lower unit is the "error" number. The additional error codes defined by FILE_open() are module 0x200 (to avoid collisions with unix and VxWorks error codes), and the error numbers obey MSG conventions, i.e. if the least significant bit is clear, the operation was a success.
References _FILE_PrimaryHeader::ctl, _FILE_Stat::ctl, _FILE_Stat::fd, FILE_ACS_SIZE, FILE_decodePrimary(), FILE_E_CORRUPT, FILE_E_NOINFLAT, FILE_E_NOMKDIR, FILE_E_NOREOPEN, FILE_E_NOSLOT, FILE_E_NOTMPOPN, FILE_inflate(), FILE_K_BITALLOC, FILE_K_HDR_SIZ, FILE_L_NAME, FILE_L_TEMPNAME, FILE_M_HDR_CMP, FILE_S_HEDCMP, FILE_S_HEDNOCMP, FILE_S_NOHED, FILE_S_SUCCESS, FILE_tempname(), FILE_V_HDR_CMP, FILE_V_HDR_SIZ, FILE_V_HDR_VER, _FILE_Stat::key, MKDIR, _FILE_Stat::nam, and _FILE_Stat::typ.
static char * FILE_tempname | ( | int | idx, | |
char * | buf, | |||
unsigned int | len | |||
) | [inline, static] |
Generate a temporary file name.
idx | (in) Slot index of temporary file | |
buf | (in) Buffer to write file name to | |
len | (in) Maximum length of file name buffer |
/ram
disk). The unix file name is a little more elaborate (it includes host name and process number) to try to avoid collisions.
Referenced by FILE_close(), and FILE_open().