[Libsysio-commit] libsysio_tests: libsysio/tests README help.c helper.pm sysio_stubs.c sysio_tests.c
Brought to you by:
lward
Update of /cvsroot/libsysio/libsysio/tests In directory sc8-pr-cvs1:/tmp/cvs-serv17460/tests Added Files: Tag: libsysio_tests README help.c helper.pm sysio_stubs.c sysio_tests.c test_all.pl test_copy.pl test_driver.c test_driver.h test_getcwd.pl test_list.pl test_path.pl test_stats.pl Log Message: added new tests --- NEW FILE --- To run the tests, just do ./test_all.pl (at some point I will integrate that into the build environment). There are 6 basic commands for the test driver, CALL, PRINT, ALLOC, FREE, HELP, and exit (EXIT, quit, or QUIT will also work). CALL is the main command for running libsysio calls. The format will depend on the particular libsysio command being ran. Basically, the format is CALL cmd args. The available commands used with CALL are (in no particular order): fstat iwrite read chdir fstatvfs iwritev readv chmod fsync list rmdir chown ftruncate lseek sizeof close getcwd lstat stat cmpstr getdirentries mkdir statvfs creat init mknod symlink debug ioctl mount truncate dup iodone open umask dup2 iowait umount endian ipread printline unlink ipreadv pread write fcntl ipwrite preadv writev fdatasync ipwritev pwritev fill iread pwrite ireadv The specifics of the commands are explained later. The return value from a command can be saved and referenced later by using a syntax similar to $foo = x. Commands can be combined, such as: CALL fstat ( $fd = CALL open foo ) ( $buf = ALLOC 128 ), with some cautionary notes. First, everything needs to be seperated by a space. File names with spaces in them need to be quoted, as in: $fd = CALL open "file with spaces" O_RDONLY Second, any value that is used needs to be identified with an identifier. In other words, the command: $buf = ALLOC ( CALL sizeof stat ) will not work, but the command $buf = ALLOC ( $size = CALL sizeof stat ) will. All commands return a 4 digit status code. The codes are: 0000 : Success. This does NOT necessarily mean that the libsysio : command returned success, only that there were no errors : in issuing the command to libsysio. To get the result of : the libsysio command, use PRINT $$ . PRINT $errno will return : the last error code. 0x001 : Invalid arguments given to command 0x002 : Invalid command issued 0x004 : Invalid variable identifier given ALLOC takes a size argument and an optional alignment argument. FREE takes the variable to free as an argument. HELP without any arguments displays the list of commands. HELP <cmd> will give information on the specific command PRINT take several forms. To just print out a variable, type PRINT $var-name. If the variable is an integer, it will return the integer. If it is a string, it will print out the string. If it is a buffer, it will print out the buffer as a series of hex digits. Note for most buffers, the test driver will not know what it contains--just because it should contain a string does not mean that the driver will know that. The other form of PRINT is: PRINT $var_name <offset> <length> <type> which will print out length units of the given type starting at the given offset. The length is the total length in bytes, so for an integer, a length of 4 would only print out one integer. The length argument is ignored for strings. Allowable types are INT SHORT CHAR and LONG. For most of the CALL commands, their format is similar to the related sysio call. The ones that do not have a corresponding sysio call are listed below: init: This MUST be called prior to any sysio calls. It initilizes : libsysio printline: If debugging is turned on, this will print a line number : with any debug lines fill <val> <type> <size> <offset> <buf>: Fills buffer buf with size : bytes of val starting at : buf+offset. The type of val : can be UINT. STR, or PTR and : is given by the type arg list <dir>: Lists contents of dir. If no dir is given, uses cwd debug <num>: Sets debug level to num sizeof <obj>: Gives the size of the obj. Valid objs are char, int, : long, flock, stat, and statvfs endian: returns 0 if the machine is little endian, one otherwise cmpstr <buf1> <buf2>: Issues a strcmp call on the two buffers to : see if they are the same. Returns 0 for a : match --- NEW FILE --- #include <stdio.h> #include "test_driver.h" void do_help() { int i, d, count = 0; fprintf(outfp, "libsysio test harness\n"); fprintf(outfp, "To get help on a specific command, use help <cmd>\n"); fprintf(outfp, "To exit, type exit or quit\n"); fprintf(outfp, "\nTo save the result from a function, use \"$res = command\"\n"); fprintf(outfp, "To later use that res, do \"comm $res\"\n"); fprintf(outfp, "\n\nAvailable commands are:\n\n"); /* Get total number of commands */ while (cmd_list[count].cmd) count++; d = count/4; if (count %4) d++; for (i=0; i < d; i++) { if (cmd_list[i+d].cmd) { if (cmd_list[i+2*d].cmd) { if (cmd_list[i+3*d].cmd) fprintf(outfp, "%-15s %-15s %-15s %-15s\n", cmd_list[i].cmd, cmd_list[i+d].cmd, cmd_list[i+2*d].cmd, cmd_list[i+3*d].cmd); else fprintf(outfp, "%-15s %-15s %-15s\n", cmd_list[i].cmd, cmd_list[i+d].cmd, cmd_list[i+2*d].cmd); } else fprintf(outfp, "%-15s %-15s\n", cmd_list[i].cmd, cmd_list[i+d].cmd); } else fprintf(outfp, "%-15s\n", cmd_list[i].cmd); } fprintf(outfp, "\n"); } void usage_setdebug() { fprintf(outfp, "setdebug [level]: Set debugging level to level\n"); } void usage_printline() { fprintf(outfp, "printline [0|1]: Turn off (0) or on (1) the printing of line number\n"); fprintf(outfp, " : and file name with debug output\n"); } void usage_endian() { fprintf(outfp, "endian: returns 1 for bigendian machines and 0 for little endian machines\n"); } void usage_sizeof() { fprintf(outfp, "sizeof [type]: returns the size of the data type. Currently \n"); fprintf(outfp, " : supported types are char, int, long, flock, stat and \n"); fprintf(outfp, " : statvfs\n"); } void usage_get_buffer() { fprintf(outfp, "alloc [size] <align>: allocates a buffer of size bytes aligned to align\n"); fprintf(outfp, " : align is optional. If not there, buffer will be aligned on\n"); fprintf(outfp, " : a one-byte boundary. returns an index into an array that \n"); fprintf(outfp, " : holds the buffer\n"); } void usage_free_buffer() { fprintf(outfp, "free [bufidx]: frees buffer at bufidx. Returns 0 on success, -1 on failure\n"); } void usage_do_fillbuff() { fprintf(outfp, "fill [val] [type] [size] [offset] [buf] : Fills the buffer buf with size \n"); fprintf(outfp, " : bytes of val starting at buf+offset\n"); fprintf(outfp, " : The type of val is specified by type,\n"); fprintf(outfp, " : which can be UINT, STR, or PTR\n"); } void usage_do_printbuf() { fprintf(outfp, "printbuf [buf] : print out contents of the buffer stored in buf\n"); fprintf(outfp, " : Always returns 0\n"); } void usage_cmpbufs() { fprintf(outfp, "cmpstr [buf1] [buf2]: Compare the contents of buf1 with buf2 by issuing a \n"); fprintf(outfp, " strcmp call. Returns 0 if the buffers match\n"); } void usage_init() { fprintf(outfp, "init: initilizes libsysio to default values for root directory and \n"); fprintf(outfp, " : current directory. Must be called before any other libsysio calls\n"); fprintf(outfp, " : Returns 0 on success, -1 on failure\n"); } void usage_list() { fprintf(outfp, "list <dir>: lists contents of dir. If dir is ommitted, will list contents\n"); fprintf(outfp, " : of the current working directory\n"); fprintf(outfp, " : Returns 0 on success, -1 on failure\n"); } void usage_chdir() { fprintf(outfp, "chdir [dir]: change the current working directory to dir\n"); fprintf(outfp, " : Returns 0 on success, -1 on failure\n"); } void usage_chmod() { fprintf(outfp, "chmod [newmode] [file]: change mode of file to newmode. newmode can be \n"); fprintf(outfp, " : specifed symbolically (eg, a+x), numerically \n"); fprintf(outfp, " : (eg, 0777), or using system defines \n"); fprintf(outfp, " : (eg S_IRUSR|S_IWUSR|S_IRGRP)\n"); fprintf(outfp, " : Returns 0 on success, -1 on failure\n"); } void usage_chown() { fprintf(outfp, "chown [newown[:newgrp]] [file]: change the owner of file to newown, the group\n"); fprintf(outfp, " : of file to newgrp, or both\n"); fprintf(outfp, " : Returns 0 on success, -1 on failure\n"); } void usage_open() { fprintf(outfp, "open [file] [flags] <mode>: open file with given flags. The mode is optional\n"); fprintf(outfp, " : can use defines for open, (eg, open foo O_RDONLY)\n"); fprintf(outfp, " : If flags are 0, file will be opened with O_RDWR\n"); fprintf(outfp, " : Returns the file descriptor for the opened file\n"); } void usage_close() { fprintf(outfp, "close [file]: closes the file. Returns 0 on success, -1 on failure\n"); } void usage_mount() { fprintf(outfp, "mount [fstype:source] [target]: mount source (which has fstype as its file\n"); fprintf(outfp, " : system type) onto target.\n"); fprintf(outfp, " : Returns 0 on success, -1 on failure\n"); } void usage_dup() { fprintf(outfp, "dup [oldfd]: Duplicate oldfd. Returns the duplicated file descriptor\n"); fprintf(outfp, " : Returns -1 on failure\n"); } void usage_dup2() { fprintf(outfp, "dup2 [oldfd] [newfd]: Make newfd be a copy of oldfd. Returns newfd on \n"); fprintf(outfp, " : success and -1 on failure\n"); } void usage_fcntl() { fprintf(outfp, "fcntl [fd] [cmd] <args> : execute fcntl cmd on file with file descriptor fd\n"); fprintf(outfp, " : using (optional) args. Accepted (but not \n"); fprintf(outfp, " : necesarily working) commands are F_DUPFD, \n"); fprintf(outfp, " : F_GETFD, F_GETFL, F_GETOWN, F_SETFD, F_SETFL,\n"); fprintf(outfp, " : F_SETOWN, F_SETLK, F_SETLKW, and F_GETLK. \n"); } void usage_fstat() { fprintf(outfp, "fstat [fd] [buf]: Get the stat structure for file descriptor fd and place it\n"); fprintf(outfp, " : in buf. Returns 0 on success, -1 on failure\n"); } void usage_fsync() { fprintf(outfp, "fsync [fd]: ensure all parts of file with file descriptor fd are output to\n"); fprintf(outfp, " : stable storage. Returns 0 on success, -1 on failure\n"); } void usage_fdatasync() { fprintf(outfp, "fdatasync [fd]: ensure all parts of file with file descriptor fd except the \n"); fprintf(outfp, " : metadata are output to stable storage. Returns 0 on \n"); fprintf(outfp, " : success, -1 on failure\n"); } void usage_ftruncate() { fprintf(outfp, "ftruncate [fd] [len]: truncate file with file descriptor fd to have be \n"); fprintf(outfp, " : len bytes in length. Returns 0 on success, -1 on \n"); fprintf(outfp, " : failure\n"); } void usage_getcwd() { fprintf(outfp, "getcwd [buf] [size]: get the current working directory and store it in buf\n"); fprintf(outfp, " : buf is size bytes in length. If buf is too short, an \n"); fprintf(outfp, " : error of ERANGE is returned. Returns 0 on success, -1\n"); fprintf(outfp, " : on failure\n"); } void usage_lseek() { fprintf(outfp, "lseek [fd] [offset] [whence]: Sets the offset of the file descriptor fd to\n"); fprintf(outfp, " : either offset if whence is SEEK_SET or offset\n"); fprintf(outfp, " : plus the current location if whence is SEEK_CUR\n"); fprintf(outfp, " : or offset plus the size of the file if whence\n"); fprintf(outfp, " : is SEEK_END. Returns 0 on success and -1 on \n"); fprintf(outfp, " : failure\n"); } void usage_lstat() { fprintf(outfp, "lstat [filename] [buf]: Get the stat structure for filename and return it in\n"); fprintf(outfp, " : buf. Returns 0 on success and -1 on failure\n"); } void usage_getdirentries() { fprintf(outfp, "getdirentries [fd] [buf] [nbytes] [basep]: Read dir entries from directory\n"); fprintf(outfp, " : with file descriptor fd into buf\n"); fprintf(outfp, " : At most nbytes are read. Reading\n"); fprintf(outfp, " : starts at basep, and basep is set\n"); fprintf(outfp, " : to new pos. Returns the number of \n"); fprintf(outfp, " : bytes read on success or 0 on\n"); fprintf(outfp, " : failure\n"); fprintf(outfp, "Note that basep does not have to be pre-allocated. Executing cmd: \n"); fprintf(outfp, "\"getdirentries $fd $buf 4096 $basep\", where $fd is the result of an open\n"); fprintf(outfp, "and $buf is the result of an alloc (but $basep is totally new) will work\n"); fprintf(outfp, "After the execution of the command, $basep holds the new offset and can be\n"); fprintf(outfp, "used again for any further getdirentries calls\n"); } void usage_mkdir() { fprintf(outfp, "mkdir [newdir] [mode]: make a new directory, newdir, with the permissions \n"); fprintf(outfp, " : specified in mode. Permissions can be symbolic \n"); fprintf(outfp, " : (eg, a+x), numeric (eg, 0777), or can use defines\n"); fprintf(outfp, " : (eg S_IRUSR|S_IWUSR|S_IRGRP). Returns 0 on success \n"); fprintf(outfp, " : -1 on failure.\n"); } void usage_creat() { fprintf(outfp, "creat [newfile] [mode]: create a new file, newfile, with the permissions \n"); fprintf(outfp, " : specified in mode. Permissions can be symbolic \n"); fprintf(outfp, " : (eg, a+x), numeric (eg, 0777), or can use defines\n"); fprintf(outfp, " : (eg S_IRUSR|S_IWUSR|S_IRGRP). Returns 0 on success \n"); fprintf(outfp, " : -1 on failure.\n"); } void usage_stat() { fprintf(outfp, "stat [filename] [buf]: Get the stat structure for filename and return it in\n"); fprintf(outfp, " : buf. Returns 0 on success and -1 on failure\n"); } void usage_statvfs() { fprintf(outfp, "statvfs [filename] [buf]: Get the statvfs structure for filename and return\n"); fprintf(outfp, " : it in buf. Returns 0 on success and -1 on failure\n"); } void usage_fstatvfs() { fprintf(outfp, "fstatvfs [fd] [buf]: Get the stat structure for file with file descriptor fd\n"); fprintf(outfp, " : and return it in buf. Returns 0 on success and -1 on\n"); fprintf(outfp, " : failure\n"); } void usage_truncate() { fprintf(outfp, "truncate [fname] [len]: truncate file with name fname to be exactly \n"); fprintf(outfp, " : len bytes in length. Returns 0 on success, -1 on \n"); fprintf(outfp, " : failure\n"); } void usage_rmdir() { fprintf(outfp, "rmdir [dirname]: Remove directory at dirname. Returns 0 on success, -1 on\n"); fprintf(outfp, " : failure.\n"); } void usage_symlink() { fprintf(outfp, "symlink [path1] [path2]: Make a symbolic link from path1 to path2. Returns\n"); fprintf(outfp, " : 0 on success, -1 on failure\n"); } void usage_unlink() { fprintf(outfp, "unlink [path]: Unlink path. If path is the last name to a file, the file is \n"); fprintf(outfp, " : is removed. If it was a symbolic link, the link is removed. \n"); fprintf(outfp, " : Returns 0 on success, -1 on failure\n"); } void usage_ioctl() { fprintf(outfp, "ioctl [fd] [cmd] <args> : Issue the ioctl command cmd on the file with file\n"); fprintf(outfp, " : descriptor fd. Any arguments are placed in args\n"); fprintf(outfp, " : At the moment, the only commands understand are the \n"); fprintf(outfp, " : ioctl commands found in /usr/include/linux/fs.h\n"); } void usage_umask() { fprintf(outfp, "ioctl [mask] : Sets the umask used by open to set initial file permissions on\n"); fprintf(outfp, " : a newly created file. Returnds the previous value of the mask\n"); } void usage_iodone() { fprintf(outfp, "iodone [ioid] : Poll for completion of the asynchronous request identifed by\n"); fprintf(outfp, " : ioid. Returns 1 if request finished\n"); } void usage_iowait() { fprintf(outfp, "iowait [ioid] : Wait for completion of the asynchronous request identifed by\n"); fprintf(outfp, " : ioid. Returns result of asynchronous request \n"); } void usage_ipreadv() { fprintf(outfp, "ipreadv [fd] [buf] [count] [off]: Reads data asynchrously to file descriptor fd \n"); fprintf(outfp, " : starting at offset off. Data comes from \n"); fprintf(outfp, " : buffer described by buf, which is a pointer to\n"); fprintf(outfp, " : an iovec strucutre. Number of buffers is \n"); fprintf(outfp, " : specified by count. Returns an iod_t on \n"); fprintf(outfp, " : success and -1 on failure\n"); } void usage_ipread() { fprintf(outfp, "ipread [fd] [buf] [count] [off]: Read asynchrously up to count bytes from file\n"); fprintf(outfp, " : with file descriptor fd starting at offset off\n"); fprintf(outfp, " : Read into buffer pointed at by buf. Returns\n"); fprintf(outfp, " : an iod_t on success and -1 on failure\n"); } void usage_preadv() { fprintf(outfp, "preadv [fd] [buf] [count] [off]: Reads data from file descriptor fd starting at\n"); fprintf(outfp, " : offset off. Data goes into buffer described\n"); fprintf(outfp, " : by buf, which is a pointer to an iovec \n"); fprintf(outfp, " : structure. Number of buffers is specified by\n"); fprintf(outfp, " : count. Returns the number of bytes read\n"); } void usage_pread() { fprintf(outfp, "preadv [fd] [buf] [count] [off]: Reads count bytes of data from file descriptor\n"); fprintf(outfp, " : fd starting at offset off. Data goes into buf.\n"); fprintf(outfp, " : Returns number of bytes read or -1 on failure\n"); } void usage_ireadv() { fprintf(outfp, "ireadv [fd] [buf] [count] : Reads data asynchrously to file descriptor fd \n"); fprintf(outfp, " : Data comes from buffer described by buf, which is \n"); fprintf(outfp, " : an pointer to an iovec structure. Number of\n"); fprintf(outfp, " : buffers is specified by count. Returns an iod_t\n"); fprintf(outfp, " : on success and -1 on failure\n"); } void usage_iread() { fprintf(outfp, "iread [fd] [buf] [count]: Read asynchrously up to count bytes from file with\n"); fprintf(outfp, " : file descriptor fd into buffer pointed at by buf\n"); fprintf(outfp, " : Returns an iod_t on success and -1 on failure\n"); } void usage_readv() { fprintf(outfp, "readv [fd] [buf] [count] : Reads data from file descriptor fd. Data comes from\n"); fprintf(outfp, " : the buffer described by buf, which is a pointer to an\n"); fprintf(outfp, " : an iovec structure. Number of buffers is specified\n"); fprintf(outfp, " : by count. Returns the number of bytes read on \n"); fprintf(outfp, " : on success and -1 on failure\n"); } void usage_read() { fprintf(outfp, "read [fd] [buf] [count]: Read up to count bytes from file with file \n"); fprintf(outfp, " : descriptor fd into buffer pointed at by buf\n"); fprintf(outfp, " : Returns number of bytes read on success or 0 on \n"); fprintf(outfp, " : on failure\n"); } void usage_ipwritev() { fprintf(outfp, "ipwritev [fd] [buf] [count] [off]: writes data asynchronously to file with file\n"); fprintf(outfp, " : descriptor fd starting at offset off. Data \n"); fprintf(outfp, " : comes from buffers described by buf, which\n"); fprintf(outfp, " : is a pointer to an iovec structure. Number \n"); fprintf(outfp, " : of buffers is specified by count. Returns\n"); fprintf(outfp, " : an iod_t on success and -1 on failure\n"); } void usage_ipwrite() { fprintf(outfp, "ipwrite [fd] [buf] [count] [off]: writes count bytes of data asynchronously to\n"); fprintf(outfp, " : file with file descriptor fd starting at \n"); fprintf(outfp, " : offset off. Data comes from buf. Returns an\n"); fprintf(outfp, " : iod_t on success and -1 on failure\n"); } void usage_pwritev() { fprintf(outfp, "pwritev [fd] [buf] [count] [off]: writes data to file with file descriptor fd\n"); fprintf(outfp, " : starting at offset off. Data comes from \n"); fprintf(outfp, " : buffers described by buf, which is a pointer\n"); fprintf(outfp, " : to an iovec structure. Number of buffers is\n"); fprintf(outfp, " : by count. Returns number of bytes read on \n"); fprintf(outfp, " : success and -1 on failure\n"); } void usage_pwrite() { fprintf(outfp, "pwrite [fd] [buf] [count] [off]: writes count bytes of data to file with file \n"); fprintf(outfp, " : descriptor fd starting at offset off. Data\n"); fprintf(outfp, " : Data comes from buf. Returns number of bytes\n"); fprintf(outfp, " : written on success and -1 on failure\n"); } void usage_iwritev() { fprintf(outfp, "iwritev [fd] [buf] [count] : writes data asynchronously to file with file\n"); fprintf(outfp, " : descriptor fd. Data comes from buffers described\n"); fprintf(outfp, " : by buf, which is a pointer to an iovec structure.\n"); fprintf(outfp, " : Number of buffers is specified by count. Returns\n"); fprintf(outfp, " : an iod_t on success and -1 on failure\n"); } void usage_iwrite() { fprintf(outfp, "iwrite [fd] [buf] [count] : writes count bytes of data asynchronously to\n"); fprintf(outfp, " : file with file descriptor fd. Data comes from buf.\n"); fprintf(outfp, " : Returns an iod_t on success and -1 on failure.\n"); } void usage_writev() { fprintf(outfp, "writev [fd] [buf] [count]: writes data to file descriptor fd. Data comes from\n"); fprintf(outfp, " : buffers described by buf, which is a pointer to a \n"); fprintf(outfp, " : iovec strucutre. Number of buffers is specified by \n"); fprintf(outfp, " : count \n"); } void usage_write() { fprintf(outfp, "write [fd] [buf] [count] : writes count bytes of data to file with file \n"); fprintf(outfp, " : descriptor fd. Data comes from buf. Returns number\n"); fprintf(outfp, " : of bytes written on success and -1 on failure.\n"); } void usage_mknod() { fprintf(outfp, "mknod [path] [mode] [dev] : creates a filesystem node named path with \n"); fprintf(outfp, " : specified mode using device special file dev\n"); fprintf(outfp, " : Returns 0 on sucess and -1 on failure\n"); } void usage_umount() { fprintf(outfp, "umount [path] : Umount file at path. Returns 0 on success and -1 on failure\n"); } void usage_exit() { } --- NEW FILE --- #!/usr/bin/perl -w # # Provides a set of helper routines for use in the Perl # test scripts # package helper; use strict; BEGIN{} # Print out a given error message, close the command file # and exit sub print_and_exit { my ($cmdfh, $outfh, $exit_num, $exit_str) = @_; print STDOUT "$exit_str"; # Clean up my $cmdstr = 'FREE $buf'; $cmdstr = $cmdstr."\n"; send_cmd($cmdfh, $outfh, "FREE", $cmdstr); print $cmdfh "exit\n"; close $outfh; # Give test_driver time to finish sleep 0.000001; exit $exit_num; } # Output the given command and make sure that the exit # code for the command was valid sub send_cmd { my ($cmdfh, $outfh, $cmd, $cmdstr) = @_; print $cmdfh $cmdstr; my $res = <$outfh>; chop($res); if ($res ne "0000 ") { print_and_exit($cmdfh, $outfh, 1, "ERROR! Command $cmd failed with code $res\n"); } } # Check the return value from the last libsysio call sub verify_cmd { my ($cmdfh, $outfh, $cmd) = @_; # Verify the system call's output my $cmdstr = 'PRINT $$'; $cmdstr .= "\n"; send_cmd($cmdfh, $outfh, "PRINT", $cmdstr); my $res = <$outfh>; chop($res); if ($res eq "0xffffffff") { # Get the errno $cmdstr = 'PRINT $errno'; $cmdstr .= "\n"; send_cmd($cmdfh, $outfh, "PRINT", $cmdstr); my $err = <$outfh>; chop($err); print_and_exit($cmdfh, $outfh, 1, "ERROR! $cmd returned $res\n"); } return $res; } # Compares two numbers. Output error message and exit if # they differ sub cmp_nums { my ($cmdfh, $outfh, $ionum, $pnum, $desc) = @_; my $str; if ($ionum != $pnum) { my $str = sprintf("ERROR! Sysio's number %x does not match Perl's (%x)\n", $ionum, $pnum); $str = sprintf("%s Numbers were %s\n", $str, $desc); print_and_exit(1, $str); } } END{} 1; --- NEW FILE --- #define _BSD_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> #include <sys/uio.h> #include <linux/fs.h> #include <sys/stat.h> #include <sys/statvfs.h> #include "test_driver.h" #include "sysio.h" /* * ################################################ * # Function stubs # [...2110 lines suppressed...] int do_umount(int argc, char **argv) { int err; if (argc != 1) { DBG(2, fprintf(outfp, "Invalid number (%d) of args for umount\n", argc)); return INVALID_ARGS; } err = umount(argv[0]); if (err) my_perror("umount"); my_errno = errno; last_ret_val = err; last_type = SINT; return SUCCESS; } --- NEW FILE --- #define _BSD_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <getopt.h> #include <errno.h> #include <ctype.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/statvfs.h> #include <fcntl.h> #include <sys/queue.h> #include <dirent.h> #include <pwd.h> #include <grp.h> #include "sysio.h" #include "mount.h" #include "test.h" #include "test_driver.h" /* * ################################################### * # Test functions # * # These functions are used to test libsysio. # * # Eventually, there should be one of these for # * # every function document in sysio.h # * ################################################### */ int initilize_sysio(char *root_driver, char *root_path, int mntflgs) { int err; char *wd; /* * Init sysio lib. */ _sysio_init(); /* * Init native file system driver and request mount of specified * source directory. */ err = drv_init_all(); if (err) { my_errno = err; my_perror("drv_init_all"); last_ret_val = errno; return SUCCESS; } err = _sysio_mount_root(root_path, root_driver, mntflgs, NULL); if (err) { my_errno = err; my_perror("_sysio_mount_root"); last_ret_val = errno; return SUCCESS; } /* * Attempt to set the cwd by getting it out of the * user's environment. If that does not work, set * it to / */ wd = getenv("PWD"); if (wd == NULL) { wd = malloc(5); strcpy(wd, "/"); } if (chdir(wd) != 0) { my_perror(wd); my_errno = errno; last_ret_val = errno; return SUCCESS; } DBG(3, sprintf(output, "Your current working directory is %s\n", wd)); last_ret_val = 0; return SUCCESS; } int sysio_list(char *path) { int fd; size_t n; struct dirent *buf, *dp; __off_t base; ssize_t cc; int numfiles = 0; fd = open(path, O_RDONLY); if (fd < 0) { my_errno = errno; last_ret_val = fd; my_perror(path); return SUCCESS; } n = 16 * 1024; buf = malloc(n); if (!buf) { my_perror(path); cc = -1; goto out; } base = 0; DBG(5, sprintf(output, "About to call getdirentries\n")); while ((cc = getdirentries(fd, (char *)buf, n, &base)) > 0) { dp = buf; while (cc > 0) { DBG(4, fprintf(outfp, "\t%s: ino %#08x off %#08x type %#08x\n", dp->d_name, (unsigned int)dp->d_ino, (unsigned int)dp->d_off, (int )dp->d_type)); sprintf(output, "%s\n", dp->d_name); cc -= dp->d_reclen; dp = (void *)dp + dp->d_reclen; numfiles++; } printf("Out of inner loop\n"); if (!base) break; } out: if (cc < 0) { DBG(2, sprintf(output, "cc barfed\n")); my_perror(path); } free(buf); { int oerrno = errno; if (close(fd) != 0) { DBG(2,sprintf(output, "close barfed\n")); my_perror(path); if (cc < 0) errno = oerrno; else cc = -1; } } last_ret_val = numfiles; my_errno = errno; return SUCCESS; } int sysio_mount(char *from, char *to) { int err; char *s; char *buf; char *cp; char *fstype, *source, *opts, *target; err = 0; /* * Copy everything to a buffer we can modify. */ s = buf = malloc(strlen(from) + 1); if (!buf) { my_perror(from); last_ret_val = -1; my_errno = errno; return SUCCESS; } (void )strcpy(s, from); /* * Eat leading white. */ while (*s && *s == ' ' && *s == '\t') s++; /* * Get fstype. */ fstype = cp = s; while (*cp && *cp != ':' && *cp != ' ' && *cp != '\t') cp++; if (fstype == cp || *cp != ':') { DBG(1, sprintf(output, "%s: Missing FS type\n", from)); err = -1; goto out; } *cp++ = '\0'; s = cp; /* * Eat leading white. */ while (*s && *s == ' ' && *s == '\t') s++; /* * Get source. */ source = cp = s; while (*cp && *cp != ' ' && *cp != '\t') cp++; if (source == cp) { DBG(1, sprintf(output, "%s: Missing source\n", from)); err = -1; goto out; } if (*cp) *cp++ = '\0'; s = to; /* * Eat leading white. */ while (*s && *s == ' ' && *s == '\t') s++; /* * Get opts. */ opts = cp = s; while (*cp && *cp != ' ' && *cp != '\t') cp++; if (opts == cp) { DBG(1,sprintf(output, "%s: Missing target\n", to)); err = -1; goto out; } if (*cp) *cp++ = '\0'; s = cp; /* * Eat leading white. */ while (*s && *s == ' ' && *s == '\t') s++; /* * Get target */ target = cp = s; while (*cp && *cp != ' ' && *cp != '\t') cp++; if (target == cp) { target = opts; opts = NULL; } if (*cp) *cp++ = '\0'; err = mount(source, target, fstype, 0, opts); if (err) my_perror(from); out: free(buf); last_ret_val = err; my_errno = errno; return SUCCESS; } int sysio_chdir(char *newdir) { char *buf; if (chdir(newdir) != 0) { my_perror(newdir); return -1; } buf = getcwd(NULL, 0); if (!buf) { my_perror(newdir); last_ret_val = -1; my_errno = errno; return SUCCESS; } DBG(4, sprintf(output, "New dir is %s\n", buf)); free(buf); return SUCCESS; } static mode_t get_mode(char *arg, int type, int start_mode); #define SYMBOLIC 0 #define DEFINED 1 #define NUMERIC 2 /* * Change the permissions on a given file * * sysio_chmod <filename> <permissions> * */ int sysio_chmod(char *mode_arg, const char *path) { int err; mode_t mode; struct stat st; /* Get the current mode */ err = stat(path, &st); /* Is the new mode symbolic? */ if (isalpha(mode_arg[0])) { /* Could be specifying defines */ if (mode_arg[0] == 'S') mode = get_mode(mode_arg, DEFINED, st.st_mode); else mode = get_mode(mode_arg, SYMBOLIC, st.st_mode); } else mode = get_mode(mode_arg, NUMERIC, st.st_mode); DBG(3,sprintf(output, "Using a mode of %o and a file of %s\n", mode, path)); if (mode == 0) { DBG(2,sprintf(output, "Invalid mode\n")); return INVALID_ARGS; } last_ret_val = chmod(path, mode); my_errno = errno; return SUCCESS; } #define USER_STATE 0 /* Specifies that the users are still being listed */ #define MODE_STATE_ADD 1 #define MODE_STATE_REMOVE 2 #define READ 00444 #define WRITE 00222 #define EXECUTE 00111 #define OWNER 00700 #define GROUP 00070 #define OTHER 00007 mode_t get_mode(char *arg, int type, int start_mode) { int i, j,digit, total; char c; int state = USER_STATE; int len = strlen(arg); unsigned int users = 0; unsigned int modes = 0; if (type == DEFINED) { char curr_word[10]; total = digit = 0; j = 0; DBG(4, sprintf(output, "len is %d\n", len)); for (i=0; i < len; i++) { if (arg[i] == '|') { curr_word[j] = '\0'; DBG(3, sprintf(output, "Got mode word %s\n", curr_word)); digit = get_obj(curr_word); if (digit < 0 ) { DBG(2, sprintf(output, "Unable to understand mode arg %s\n", curr_word)); return -1; } total |= digit; j = 0; } else curr_word[j++] = arg[i]; } curr_word[j] = '\0'; DBG(3, sprintf(output, "Got mode word %s\n", curr_word)); digit = get_obj(curr_word); if (digit < 0 ) { DBG(3, sprintf(output, "Unable to understand mode arg %s\n", curr_word)); return -1; } total |= digit; return total; } if (type == SYMBOLIC) { for (i=0; i < len; i++) { c = arg[i]; if (state == USER_STATE) { switch(c){ case 'u': users |= OWNER; break; case 'g': users |= GROUP; break; case 'o': users |= OTHER; break; case 'a': users |= (OWNER|GROUP|OTHER); break; case '+': state = MODE_STATE_ADD; break; case '-': state = MODE_STATE_REMOVE; break; default: return 0; } } else { switch(c){ case 'r': modes |= READ; break; case 'w': modes |= WRITE; break; case 'x': modes |= EXECUTE; break; default: return 0; } } } if (state == MODE_STATE_ADD) { return (start_mode | (users & modes)); } else { return (start_mode & ~(users & modes)); } } else { /* Digits should be octal digits, so should convert */ total = 0; for (i=0; i < len; i++) { c = arg[i]; digit = atoi(&c); if (digit > 7) return 0; for (j=len-i-1; j >0; j--) digit *= 8; total += digit; } return total; } } /* * Changes the ownership of the file. The new_id * is of the format owner:group. Either the owner * or the group may be omitted, but, in order to * change the group, the : must preced the group. */ int sysio_chown(char *new_id, char *file) { char *owner = NULL; char *group = NULL; struct group *g_ent = NULL; struct passwd *o_ent = NULL; uid_t o_id=-1, g_id=-1; int len, j, i=0; int state = 0; /* Correspond to getting owner name */ len = strlen(new_id); for (i=0; i < len; i++) { if (new_id[i] == ':') { /* Group name */ if (!group) group = malloc(strlen(new_id) -i +2); state = 1; /* Now getting group name */ j = 0; if (owner) owner[i] = '\0'; } if (!state) { /* Getting owner name */ if (!owner) owner = malloc(strlen(new_id) +1 ); owner[i] = new_id[i]; } else { /* Group name */ group[j] = new_id[i]; j++; } } if (group) group[i] = '\0'; else owner[i] = '\0'; /* Are the owner and/or group symbolic or numeric? */ if (owner) { if (isdigit(owner[0])) { /* Numeric -- just convert */ o_id = (uid_t) atoi(owner); /* Make sure it is valid */ if ((o_ent = getpwuid(o_id)) == NULL) { DBG(2, sprintf(output, "Error: uid %d not found \n", o_id)); return INVALID_ARGS; } } else { /* Get the id from the passwd file */ if ((o_ent = getpwnam(owner)) == NULL) { DBG(2, sprintf(output, "Error: name %s not found\n", owner)); return INVALID_ARGS; } o_id = o_ent->pw_uid; } } if (group) { if (isdigit(group[0])) { /* Numeric -- just convert */ g_id = (uid_t) atoi(group); /* Make sure it is valid */ if ((g_ent = getgrgid(g_id)) == NULL) { DBG(2, sprintf(output, "Error: gid %d not found \n", g_id)); return INVALID_ARGS; } } else { /* Find group in group file */ if ((g_ent = getgrnam(group)) == NULL) { DBG(2, sprintf(output, "Error: group %s not found \n", group)); return INVALID_ARGS; } g_id = g_ent->gr_gid; } } /* Now issue the syscall */ DBG(4, sprintf(output, "Changing owner of file %s to %d (group %d)\n", file, o_id, g_id)); last_ret_val = chown(file, o_id, g_id); my_errno = errno; return SUCCESS; } int sysio_open(char *path, int flags) { DBG(4, sprintf(output, "Opening file %s with flags %x\n", path, flags)); last_ret_val = open(path, flags); my_errno = errno; return SUCCESS; } int sysio_open3(char *path, int flags, char *mode_arg) { mode_t mode; /* Is the new mode symbolic? */ if (isalpha(mode_arg[0])) { /* Could be specifying defines */ if (mode_arg[0] == 'S') mode = get_mode(mode_arg, DEFINED, 0); else mode = get_mode(mode_arg, SYMBOLIC, 0); } else mode = get_mode(mode_arg, NUMERIC, 0); last_ret_val = open(path, flags, mode); my_errno = errno; return SUCCESS; } int sysio_close(int fd) { last_ret_val = close(fd); my_errno = errno; return SUCCESS; } int sysio_fcntl(int fd, struct cmd_map* cmdptr, char *arg) { int fd_new, index, cmd, flag; char *cmdname; void *buf; cmd = cmdptr->cmd; cmdname = cmdptr->cmd_name; switch(cmd) { case F_DUPFD: fd_new = get_obj(arg); last_ret_val = fcntl(fd, F_DUPFD, fd_new); my_errno = errno; return SUCCESS; break; case F_GETFD: case F_GETFL: case F_GETOWN: /* case F_GETSIG: case F_GETLEASE: */ last_ret_val= fcntl(fd, cmd); my_errno = errno; return SUCCESS; break; case F_SETFD: case F_SETFL: case F_SETOWN: /*case F_SETSIG: case F_SETLEASE: case F_NOTIFY: */ flag = atoi(arg); last_ret_val = fcntl(fd, cmd, flag); my_errno = errno; return SUCCESS; break; case F_SETLK: case F_SETLKW: case F_GETLK: /* Get the buffer to hold the lock structure */ index = get_obj(arg); if (index < 0) { sprintf(output, "Unable to find buffer %s\n", arg+1); return INVALID_VAR; } buf = buflist[index]; if (!buf) { sprintf(output, "Buffer at index %d (mapped by %s) is null\n", index, arg); return INVALID_VAR; } last_ret_val = fcntl(fd, cmd, (struct flock *)buf); my_errno = errno; return SUCCESS; default: /* THis should be impossible */ return INVALID_ARGS; } return INVALID_ARGS; } void print_stat(struct stat *st) { DBG(3, sprintf(output, "%sstruct stat: \n", output)); DBG(3, sprintf(output, "%s st_dev: %#10x\n", output, (unsigned int)st->st_dev)); DBG(3, sprintf(output, "%s st_ino: %#10x\n", output, (unsigned int) st->st_ino)); DBG(3, sprintf(output, "%s st_mode: %o\n", output, st->st_mode)); DBG(3, sprintf(output, "%s st_nlink: %d\n", output, st->st_nlink)); DBG(3, sprintf(output, "%s st_uid: %d\n", output, st->st_uid)); DBG(3, sprintf(output, "%s st_gid: %d\n", output, st->st_gid)); DBG(3, sprintf(output, "%s st_rdev: %d\n", output, (int)st->st_rdev)); DBG(3, sprintf(output, "%s st_size: %d\n", output, (int) st->st_size)); DBG(3, sprintf(output, "%s st_blksize: %d\n", output, (int) st->st_blksize)); DBG(3, sprintf(output, "%s st_blocks: %d\n", output, (int) st->st_blocks)); DBG(3, sprintf(output, "%s st_atime: %#10x\n", output, (unsigned int) st->st_atime)); DBG(3, sprintf(output, "%s st_mtime: %#10x\n", output, (unsigned int) st->st_mtime)); DBG(3, sprintf(output, "%s st_ctime: %#10x", output, (unsigned int) st->st_ctime)); } int sysio_fstat(int fd, void *buf) { int err; struct stat *st = (struct stat *)buf; err = fstat(fd, st); if (err < 0) { my_perror("fstat"); } my_errno = errno; last_ret_val = err; print_stat(st); return SUCCESS; } int sysio_lstat(char *filename, void *buf) { int err; struct stat *st = (struct stat *)buf; err = lstat(filename, st); if (err < 0) { my_perror("lstat"); } my_errno = errno; last_ret_val = err; print_stat(st); return SUCCESS; } int sysio_stat(char *filename, void *buf) { int err; struct stat *st = (struct stat *)buf; err = stat(filename, st); if (err < 0) { my_perror("stat"); } my_errno = errno; last_ret_val = err; print_stat(st); return SUCCESS; } int sysio_getdirentries(int fd, char *buf, size_t nbytes, off_t *basep) { int err; struct dirent *dp; err = getdirentries(fd, buf, nbytes, basep); last_ret_val = err; DBG(4, sprintf(output, "Read %d bytes\n", err)); dp = (struct dirent *)buf; while (err > 0) { DBG(3, sprintf(output, "\t%s: ino %llu off %llu len %x type %c\n", dp->d_name, (unsigned long long )dp->d_ino, (unsigned long long )dp->d_off, dp->d_reclen, (char )dp->d_type)); err -= dp->d_reclen; dp = (void *)dp + dp->d_reclen; } my_errno = errno; return last_ret_val; } int sysio_mkdir(char *path, char *mode_arg) { int err; mode_t mode; struct stat st; /* Is the new mode symbolic? */ if (isalpha(mode_arg[0])) { /* Could be specifying defines */ if (mode_arg[0] == 'S') mode = get_mode(mode_arg, DEFINED, st.st_mode); else mode = get_mode(mode_arg, SYMBOLIC, st.st_mode); } else mode = get_mode(mode_arg, NUMERIC, st.st_mode); DBG(3, sprintf(output, "Using a mode of %o and a file of %s\n", mode, path)); if (mode == 0) { DBG(2, sprintf(output, "Invalid mode\n")); return INVALID_ARGS; } err = mkdir(path, mode); my_errno = errno; last_ret_val = err; return SUCCESS; } int sysio_creat(char *path, char *mode_arg) { mode_t mode; int err; /* Is the new mode symbolic? */ if (isalpha(mode_arg[0])) { /* Could be specifying defines */ if (mode_arg[0] == 'S') mode = get_mode(mode_arg, DEFINED, 0); else mode = get_mode(mode_arg, SYMBOLIC, 0); } else mode = get_mode(mode_arg, NUMERIC, 0); DBG(3, sprintf(output, "Using a mode of %o and a file of %s\n", mode, path)); if (mode == 0) { DBG(2, sprintf(output, "Invalid mode\n")); return INVALID_ARGS; } err = creat(path, mode); my_errno = errno; last_ret_val = err; return SUCCESS; } void print_statvfs(struct statvfs *st) { DBG(3, sprintf(output, "%sstruct statvfs: \n", output)); DBG(3, sprintf(output, "%s f_bsize: %x\n", output, (unsigned int) st->f_bsize)); DBG(3, sprintf(output, "%s f_frsize: %x\n", output, (unsigned int) st->f_frsize)); DBG(3, sprintf(output, "%s f_blocks: %x\n", output, (unsigned int) st->f_blocks)); DBG(3, sprintf(output, "%s f_bfree: %x\n", output, (unsigned int) st->f_bfree)); DBG(3, sprintf(output, "%s f_bavail: %x\n", output, (unsigned int) st->f_bavail)); DBG(3, sprintf(output, "%s f_files: %x\n", output, (unsigned int) st->f_files)); DBG(3, sprintf(output, "%s f_ffree: %x\n", output, (unsigned int) st->f_ffree)); DBG(3, sprintf(output, "%s f_favail: %x\n", output, (unsigned int) st->f_favail)); DBG(3, sprintf(output, "%s f_files: %x\n", output, (unsigned int) st->f_files)); DBG(3, sprintf(output, "%s f_fsid: %x\n", output, (unsigned int) st->f_fsid)); DBG(3, sprintf(output, "%s f_flag: %x\n", output, (unsigned int) st->f_flag)); DBG(3, sprintf(output, "%s f_fnamemax: %x\n", output, (unsigned int) st->f_namemax)); } int sysio_statvfs(char *filename, void *buf) { int err; struct statvfs *st = (struct statvfs *)buf; err = statvfs(filename, st); if ( err == -1) { my_perror("statvfs"); } my_errno = errno; last_ret_val = err; print_statvfs(st); return SUCCESS; } int sysio_fstatvfs(int fd, void *buf) { int err; struct statvfs *st = (struct statvfs *)buf; err = fstatvfs(fd, st); if (err == -1) { my_perror("fstatvfs"); } my_errno = errno; last_ret_val = err; print_statvfs(st); return SUCCESS; } int sysio_umask(char *mode_arg) { mode_t mode; /* Is the new mode symbolic? */ if (isalpha(mode_arg[0])) { /* Could be specifying defines */ if (mode_arg[0] == 'S') mode = get_mode(mode_arg, DEFINED, 0); else mode = get_mode(mode_arg, SYMBOLIC, 0); } else mode = get_mode(mode_arg, NUMERIC, 0); last_ret_val = umask(mode); my_errno = errno; return SUCCESS; } int sysio_mknod(char *path, char *mode_arg, dev_t dev) { int err; int mode; /* Is the new mode symbolic? */ if (isalpha(mode_arg[0])) { /* Could be specifying defines */ if (mode_arg[0] == 'S') mode = get_mode(mode_arg, DEFINED, 0); else mode = get_mode(mode_arg, SYMBOLIC, 0); } else mode = get_mode(mode_arg, NUMERIC, 0); if (mode < 0) { DBG(2,sprintf(output, "Cant get mode from %s\n", mode_arg)); return INVALID_VAR; } err = mknod(path, (mode_t) mode, dev); if (err < 0) my_perror("mknod"); last_ret_val = err; my_errno = errno; return SUCCESS; } --- NEW FILE --- #!/usr/bin/perl -w # # VERY basic functionality test for sysio. To run, just type ./test_all.pl # use strict; # Will use this directory... system("mkdir tmp_dir"); my $failures = 0; my $success = 0; # Get cwd.. my $cwd = $ENV{PWD}; # Get the sysio dir my $sysdir = $cwd; $sysdir =~ s/\/\w+$//; # Test getdirentries my $res = `./test_list.pl ../`; chop($res); if ($res ne "list test successful") { print "Basic getdirentries test failed with message: $res\n"; $failures++; } else { $success++; } # Test mount $res = `./test_list.pl -m native:$sysdir $cwd/tmp_dir`; chop($res); if ($res ne "list test successful") { print "Mount test failed with message: $res\n"; $failures++; } else { $success++; } # Test getcwd $res = `./test_getcwd.pl $sysdir`; chop($res); if ($res ne "getcwd test successful") { print "getcwd test failed with message: $res\n"; $failures++; } else { $success++; } # Test copy $res = `./test_copy.pl ../README tmp_dir/README`; chop($res); if ($res ne "copy test successful") { print "copy test failed with message: $res\n"; $failures++; } else { $success++; } # Test stats $res = `./test_stats.pl tmp_dir/README`; chop($res); if ($res ne "stat test successful") { print "stat test failed with message: $res\n"; $failures++; } else { $success++; } print "$failures tests failed and $success tests succeeded\n"; # cleanup system(`rm -rf tmp_dir`); --- NEW FILE --- #!/usr/bin/perl -w # # getdirentries test: Tests the equivalent of a ls. Note that this is not # the most robust test in the world; it simply verifies # that libsysio returns all the entries in the directory # # use IPC::Open2; use strict; use helper; sub usage { print "Usage: ./test_copy.pl <src> <dest>: Copy a file from src to dest\n"; exit(-1); } sub process_cmd { my ($src, $dest) = @_; eval { open2(\*OUTFILE, \*CMDFILE, "./test_driver --np"); }; if ($@) { if ($@ =~ /^open2/) { warn "open2 failed: $!\n$@\n"; return; } die; } my $outfh = \*OUTFILE; my $cmdfh = \*CMDFILE; helper::send_cmd($cmdfh, $outfh, "init", "CALL init\n"); # Get the filesize of src my $size = -s $src; my $bufsize; if ( $size > 1024) { # Arbitrary limit $bufsize = 1024; } else { $bufsize = $size; } # Open src my $cmdstr = '$src = CALL open '."$src O_RDONLY\n"; helper::send_cmd($cmdfh, $outfh, "open", $cmdstr); # Open dest $cmdstr = '$dest = CALL open '."$dest O_RDWR|O_CREAT 7777\n"; helper::send_cmd($cmdfh, $outfh, "open", $cmdstr); # Allocate buffer $cmdstr = '$buf = ALLOC '."$bufsize\n"; helper::send_cmd($cmdfh, $outfh, "ALLOC", $cmdstr); # Read size bytes from src and write them out to dest my $bytes = $size; while ($bytes > 0) { $cmdstr = 'CALL read $src $buf '."$bufsize\n"; helper::send_cmd($cmdfh, $outfh, "read", $cmdstr); my $res = helper::verify_cmd($cmdfh, $outfh, "read"); my $readb = oct($res); # Now write $readb back out to dest $cmdstr = 'CALL write $dest $buf '."$readb\n"; helper::send_cmd($cmdfh, $outfh, "write", $cmdstr); $res = helper::verify_cmd($cmdfh, $outfh, "write"); if ($readb != oct($res)) { print STDOUT "ERROR! Read $readb bytes but got back $res bytes\n"; exit 1; } $bytes -= $readb; } # Clean up $cmdstr = 'CALL close $src'."\n"; helper::send_cmd($cmdfh, $outfh, "close", $cmdstr); $cmdstr = 'CALL close $dest'."\n"; helper::send_cmd($cmdfh, $outfh, "close", $cmdstr); my $cmpres = system("cmp -s $src $dest"); if ($cmpres != 0) { print STDOUT "ERROR! File $src differs from $dest\n"; exit 1; } helper::print_and_exit($cmdfh, $outfh, 0, "copy test successful\n"); } if (@ARGV != 2) { usage; } my $src = $ARGV[0]; my $dest = $ARGV[1]; process_cmd($src, $dest); exit 0; --- NEW FILE --- #define _BSD_SOURCE #define _XOPEN_SOURCE 600 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <getopt.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/uio.h> #include <sys/statvfs.h> #include <fcntl.h> #include <sys/queue.h> #include <dirent.h> #include <unistd.h> #include <ctype.h> #include "sysio.h" #include "mount.h" #include "test.h" #include "test_driver.h" struct queue_t; typedef struct cmd_tree_t { char *res_name; char *val; int arg_count; struct queue_t *children; } cmd_tree; struct queue_t { char *val; cmd_tree *cmd; struct queue_t *next; }; struct cmd_t cmd_list[] = { {"alloc", get_buffer, usage_get_buffer}, {"chdir", do_chdir, usage_chdir}, {"chmod", do_chmod, usage_chmod}, {"chown", do_chown, usage_chown}, {"close", do_close, usage_close}, {"cmpstr", cmp_bufs, usage_cmpbufs}, {"creat", do_creat, usage_creat}, {"debug", do_setdebug, usage_setdebug}, {"dup", do_dup, usage_dup}, {"dup2", do_dup2, usage_dup2}, {"endian", get_endian, usage_endian}, {"exit", do_exit, usage_exit}, {"fcntl", do_fcntl, usage_fcntl}, {"fdatasync", do_fdatasync, usage_fdatasync}, {"fill", do_fillbuff, usage_do_fillbuff}, {"free", free_buffer, usage_free_buffer}, {"fstat", do_fstat, usage_fstat}, {"fstatvfs", do_fstatvfs, usage_fstatvfs}, {"fsync", do_fsync, usage_fsync}, {"ftruncate", do_ftruncate, usage_ftruncate}, {"getcwd", do_getcwd, usage_getcwd}, {"getdirentries", do_getdirentries, usage_getdirentries}, {"init", do_init, usage_init}, {"ioctl", do_ioctl, usage_ioctl}, {"iodone", do_iodone, usage_iodone}, {"iowait", do_iowait, usage_iowait}, {"ipread", do_ipread, usage_ipread}, {"ipreadv", do_ipreadv, usage_ipreadv}, {"ipwrite", do_ipwrite, usage_ipwrite}, {"ipwritev", do_ipwritev, usage_ipwritev}, {"iread", do_iread, usage_iread}, {"ireadv", do_ireadv, usage_ireadv}, {"iwrite", do_iwrite, usage_iwrite}, {"iwritev", do_iwritev, usage_iwritev}, {"list", do_list, usage_list}, {"lseek", do_lseek, usage_lseek}, {"lstat", do_lstat, usage_lstat}, {"mkdir", do_mkdir, usage_mkdir}, {"mknod", do_mknod, usage_mknod}, {"mount", do_mount, usage_mount}, {"open", do_open, usage_open}, {"printbuf", do_printbuf, usage_do_printbuf}, {"printline", do_printline, usage_printline}, {"pread", do_pread, usage_pread}, {"preadv", do_preadv, usage_preadv}, {"pwritev", do_pwritev, usage_pwritev}, {"pwrite", do_pwrite, usage_pwrite}, {"quit", do_exit, usage_exit}, {"read", do_read, usage_read}, {"readv", do_readv, usage_readv}, {"rmdir", do_rmdir, usage_rmdir}, {"sizeof", get_sizeof, usage_sizeof}, /* {"setoutput", do_setoutput, usage_setoutput}, */ {"stat", do_stat, usage_stat}, {"statvfs", do_statvfs, usage_statvfs}, {"symlink", do_symlink, usage_symlink}, {"truncate", do_truncate, usage_truncate}, {"umask", do_umask, usage_umask}, {"umount", do_umount, usage_umount}, {"unlink", do_unlink, usage_unlink}, {"write", do_write, usage_write}, {"writev", do_writev, usage_writev}, {NULL, NULL, NULL} }; int run_cmd(cmd_tree *cmd_arg); cmd_tree* build_tree(char **cmd, int *length, int total); /* * ################################################## * # Memory functions # * # Intended to allow users to gain access to # * # buffers of memory to be manipulated later # * ################################################## */ void * alloc_buff32(unsigned int size, int align) { void* buf; if (posix_memalign(&buf, align, size)) return 0; return buf; } void free_buf32(void * ptr) { free(ptr); } long alloc_buff64(unsigned int size, int align) { char * buf; long ret_value; if (posix_memalign((void **)&buf, align, size)) return 0; ret_value = (long)buf; return ret_value; } void free_buf64(long ptr) { free((char *)ptr); } /* * Hash function for variables. Shamelessly stolen * from the ext3 code */ unsigned int dx_hack_hash (const char *name, int len) { unsigned int hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9; while (len--) { unsigned int hash = hash1 + (hash0 ^ (*name++ * 7152373)); if (hash & 0x80000000) hash -= 0x7fffffff; hash1 = hash0; hash0 = hash; } return hash0; } struct var_mapping *get_map(char *var_name) { int index; struct var_mapping_list *curr; if (var_name[0] == '$') { /* It is a name--chop off the initial and get the mapping $ */ var_name++; } index = dx_hack_hash(var_name, strlen(var_name)); index %= MAX_VARS -1; DBG(5, fprintf(outfp, "Got index of %d\n", index)); curr = &map[index]; while ((curr) && (curr->map.obj != -1) ) { if (strcmp(curr->map.name, var_name)) curr = curr->next; else return &curr->map; } return NULL; } char *get_str(char *var_name) { /* See if it is a quoted string */ if (var_name[0] == '"') { /* Chop off the beginning and end quotes and return the string */ int len = strlen(var_name); var_name[len-1] = '\0'; var_name++; } return var_name; } int get_obj(char *var_name) { struct var_mapping *var_map; int i; /* If var_name is a digit, we assume it is a literal */ if (isdigit(var_name[0])) return atoi(var_name); /* * Check for '|', indicates that one or more values are or'd * together */ for (i=0; i < strlen(var_name); i++) { if (var_name[i] == '|') { char *str1 = malloc(i+1); char *str2 = malloc(strlen(var_name)-i+1); int obj; struct var_mapping *tmp_map; memcpy(str1, var_name, i); memcpy(str2, (char *)&var_name[i+1], strlen(var_name)-i); tmp_map = get_map(str1); if (!tmp_map) { free(str1); free(str2); return -1; } obj = tmp_map->obj; tmp_map = get_map(str2); if (!tmp_map) { free(str1); free(str2); return -1; } obj |= tmp_map->obj; free(str1); free(str2); return obj; } } var_map = get_map(var_name); if (!var_map) return -1; else return var_map->obj; } void store_result(char *var_name, int result) { int index = dx_hack_hash(var_name, strlen(var_name)); struct var_mapping_list *map_obj; struct var_mapping_list *new_map; index %= MAX_VARS -1 ; if (map[index].map.obj >= 0) { /* Got a collision --just chain it*/ new_map = malloc(sizeof(struct var_mapping_list)); map_obj = &map[index]; while (map_obj->next != NULL) map_obj = map_obj->next; map_obj->next = new_map; } else new_map = &map[index]; new_map->map.name = malloc(sizeof(var_name) + 1); strcpy(new_map->map.name, var_name); new_map->map.obj = result; new_map->map.type = last_type; new_map->next = NULL; DBG(3, fprintf(outfp, "Stored %d in index %d hashed with %s\n", result, index, var_name)); } void free_obj(char *obj_name) { int index; struct var_mapping_list *prev, *curr; /* See if it is a variable name */ if (obj_name[0] == '$') { /* It is a name--chop off the initial $ */ obj_name++; } index = dx_hack_hash(obj_name, strlen(obj_name)); index %= MAX_VARS -1; DBG(5, fprintf(outfp, "Got index of %d\n", index)); curr = &map[index]; prev = NULL; while ((curr) && (curr->map.obj != -1) ) { if (strcmp(curr->map.name, obj_name)) { prev = curr; curr = curr->next; } else break; } /* Remove the object from the chain */ if (prev) prev->next = curr->next; curr->map.obj = -1; free(curr->map.name); if (prev) free(curr); } /* * Given a long string, returns the string divided into * whitespace seperated words in list. Returns the number * of words */ int parser(char *str, char** list) { int len, i=0, j=0, counter=-1; int in_quotes = 0; char *new_str; len = strlen(str); DBG(5, fprintf(outfp, "str is %s len is %d\n", str, len)); while (i < len) { if ((i==0) || ((str[i] == ' ') && (in_quotes == 0)) ) { if (i != 0) { new_str[j] = '\0'; DBG(5, fprintf(outfp, "Got word %s\n", list[counter])); i++; } while ((str[i] == ' ') && (in_quotes == 0)) i++; counter++; new_str = list[counter] = malloc(MAX_WORD); j = 0; } new_str[j] = str[i]; if (str[i] == '"') { if (in_quotes) in_quotes = 0; else in_quotes = 1; } if ((str[i] == ' ') && (in_quotes==0)){ while (str[i+1] == ' ') i++; new_str[j] = '\0'; } i++; j++; } new_str[j] = '\0'; DBG(5, fprintf(outfp, "Got word %s\n", list[counter])); return counter +1; } int execute_cmd(char *cmd, char **args, int arg_count) { int i = 0; if (!strcmp(cmd, "help")) { if (arg_c... [truncated message content] |