gboolean io_pipe (IOStream *input, IOStream *output, GError **err) { gsize neof; /* Invariants to make life easier. */ g_return_val_if_fail (input->bufsz == output->bufsz, TRUE); g_assert (input->mode & IO_MODE_READ); g_assert (output->mode & IO_MODE_WRITE); if (input->s.read.curpos == input->bufsz) { if (_io_read (input, err)) return TRUE; } if (output->s.write.curpos == output->bufsz) { if (_io_write (output, err)) return TRUE; } g_return_val_if_fail (input->s.read.curpos == output->s.write.curpos, TRUE); while (!input->s.read.eof) { if (_io_fd_write (output->fd, input->s.read.buf + input->s.read.curpos, input->bufsz - input->s.read.curpos, err)) return TRUE; if (_io_read (input, err)) return TRUE; } neof = input->s.read.endpos - input->s.read.curpos; if (neof > 0) { if (_io_fd_write (output->fd, input->s.read.buf + input->s.read.curpos, neof, err)) return TRUE; } return FALSE; }
void cow_dfield_read(cow_dfield *f, char *fname) { #if (COW_HDF5) if (_io_check_file_exists(fname)) return; clock_t start = clock(); _io_read(f, fname); cow_dfield_syncguard(f); double sec = (double)(clock() - start) / CLOCKS_PER_SEC; printf("[%s] read from %s/%s took %f minutes\n", MODULE, fname, f->name, sec/60.0); fflush(stdout); #endif }
int32_t Shell_disect(int32_t argc, char *argv[] ) { /* Body */ int32_t return_code = SHELL_EXIT_SUCCESS; bool print_usage, shorthelp = FALSE; uint32_t sector,num_sectors; int32_t offset=0; MQX_FILE_PTR fd, fs; uint32_t e,i; unsigned char *buffer; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if ((argc < 2) || (argc > 5)) { printf("Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } else if ( !Shell_parse_uint_32(argv[1], (uint32_t *) &offset )) { printf("Error, invalid length\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } else { num_sectors = 1; if (argc >= 3) { if ( !Shell_parse_uint_32(argv[2], (uint32_t *) &num_sectors )) { num_sectors = 1; } } if (argc >= 4) { fd = fopen(argv[3], "r"); if (!fd) { printf("Error, unable to open file %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } } else { fs = Shell_get_current_filesystem(argv); _io_ioctl(fs, IO_IOCTL_GET_DEVICE_HANDLE, &fd); } if (fd) { buffer = _mem_alloc(SECTOR_SIZE); if (buffer) { for(sector=0;sector<num_sectors;sector++) { if (fseek(fd, offset+sector, IO_SEEK_SET) == IO_ERROR) { printf("Error, unable to seek to sector %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } else if (_io_read(fd, (char *) buffer, 1) != 1) { printf("Error, unable to read sector %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } else { printf("\nSector # %d\n",offset+sector); for (e=0;e<16;e++) { for (i=0;i<32;i++) { printf("%02x ",(uint32_t) buffer[e*32+i]); } printf("\n"); } } } _mem_free(buffer); } } printf("\n"); if (argc >= 4) { fclose(fd); } } } if (print_usage) { if (shorthelp) { printf("%s <sector> [<device>]\n", argv[0]); } else { printf("Usage: %s <sector> [<device>]\n", argv[0]); printf(" <sector> = sector number\n"); printf(" <device> = low level device\n"); } } return return_code; } /* Endbody */
int_32 Shell_di(int_32 argc, char_ptr argv[] ) { /* Body */ boolean print_usage, shorthelp = FALSE; int_32 return_code = SHELL_EXIT_SUCCESS; int_32 offset; MQX_FILE_PTR fd, fs; uint_32 backup=0; uchar_ptr buffer=NULL; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if ((argc < 2 ) || (argc > 3)) { printf("Invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } else if ( !Shell_parse_uint_32(argv[1], (uint_32_ptr) &offset )) { printf("Error, invalid length\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } else { buffer = _mem_alloc(SECTOR_SIZE); if (buffer==NULL) { printf("Error, unable to allocate sector buffer\n"); return SHELL_EXIT_ERROR; } if (argc==3) { fd = fopen(argv[2], "b"); } else { fs = Shell_get_current_filesystem(argv); _io_ioctl(fs, IO_IOCTL_GET_DEVICE_HANDLE, &fd); } if (fd) { if (fseek(fd, offset, IO_SEEK_SET) == IO_ERROR) { printf("Error, unable to seek to sector %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } else if (_io_read(fd, (char_ptr) buffer, 1) != 1) { printf("Error, unable to read sector %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } if (return_code == SHELL_EXIT_SUCCESS) { printf("\n"); backup = print_bpb(buffer); if (backup) { if (fseek(fd, backup, IO_SEEK_SET) == IO_ERROR) { printf("Error, unable to seek to sector %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } else if (_io_read(fd, (char_ptr) buffer, 1) != 1) { printf("Error, unable to read sector %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } if (return_code == SHELL_EXIT_SUCCESS) { printf("\n"); print_bpb(buffer); } } } if (fseek(fd, 1, IO_SEEK_SET) == IO_ERROR) { printf("Error, unable to seek to sector %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } else if (_io_read(fd, (char_ptr) buffer, 1) != 1) { printf("Error, unable to read sector %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } if (return_code == SHELL_EXIT_SUCCESS) { printf("\n"); print_fsi(buffer); } if (argc==3) { fclose(fd); } } _mem_free(buffer); } } if (print_usage) { if (shorthelp) { printf("%s <sector> [<device>]\n", argv[0]); } else { printf("Usage: %s <sector> [<device>]\n", argv[0]); printf(" <sector> = sector number\n"); printf(" <device> = low level device\n"); } } return return_code; } /* Endbody */
int_32 Shell_dirent(int_32 argc, char_ptr argv[] ) { /* Body */ boolean print_usage, shorthelp = FALSE; int_32 return_code = SHELL_EXIT_SUCCESS; uint_32 sector,num_sectors; int_32 offset; MQX_FILE_PTR fd, fs; uint_32 e; uchar_ptr buffer; MFS_DIR_ENTRY_PTR dirents; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if ((argc < 2) || (argc > 4)) { printf("Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } else if ( !Shell_parse_uint_32(argv[1], (uint_32_ptr) &offset )) { printf("Error, invalid length\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } else { num_sectors = 1; if (argc >= 3) { if ( !Shell_parse_uint_32(argv[2], (uint_32_ptr) &num_sectors )) { num_sectors = 1; } } if (argc == 4) { fd = fopen(argv[3], "r"); if (!fd) { printf("Error, unable to open file %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } } else { fs = Shell_get_current_filesystem(argv); _io_ioctl(fs, IO_IOCTL_GET_DEVICE_HANDLE, &fd); } if (fd) { buffer = _mem_alloc(SECTOR_SIZE); if (buffer) { for(sector=0;sector<num_sectors;sector++) { if (fseek(fd, offset+sector, IO_SEEK_SET) == IO_ERROR) { printf("Error, unable to seek to sector %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } else if (_io_read(fd, (char_ptr) buffer, 1) !=1) { printf("Error, unable to read sector %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } else if (!is_zero(buffer, SECTOR_SIZE)) { printf("\nEntry # %d",offset+sector); dirents = (MFS_DIR_ENTRY_PTR) buffer; for (e=0;e<DIRENTS_PER_SECTOR;e++) { print_dirent(&dirents[e]); } printf("\n"); } } _mem_free(buffer); } else { printf("Error, unable to allocate sector buffer.\n" ); return_code = SHELL_EXIT_ERROR; } if (argc >= 4) { fclose(fd); } } } } if (print_usage) { if (shorthelp) { printf("%s <sector> [<device>]\n", argv[0]); } else { printf("Usage: %s <sector> [<device>]\n", argv[0]); printf(" <sector> = sector number\n"); printf(" <device> = low level device\n"); } } return return_code; } /* Endbody */
gssize io_read_into_user_buf (IOStream *io, DSType type, gsize nvals, gpointer buf, GError **err) { gsize nbytes, ninbuf; g_assert (io->mode == IO_MODE_READ); g_assert (buf != NULL); nbytes = nvals * ds_type_sizes[type]; /* At EOF? If we, all we need to do is decode the already-read * data into the caller's buffer. If there isn't as much data left * as the user requested, return a short read. */ if (io->s.read.eof) { nbytes = MIN (io->s.read.endpos - io->s.read.curpos, nbytes); if (nbytes % ds_type_sizes[type] != 0) /* Nonintegral number of items -- treat as truncated file * which we consider to be an I/O error.*/ return -1; nvals = nbytes / ds_type_sizes[type]; io_recode_data_copy (io->s.read.buf + io->s.read.curpos, buf, type, nvals); io->s.read.curpos += nbytes; return nvals; } /* Not at EOF. First, we copy over whatever data have already been * buffered. If there's no more to be done, we'll leave the buffer * in the correct state to be refilled for the next operation. */ ninbuf = MIN (nbytes, io->bufsz - io->s.read.curpos); memcpy (buf, io->s.read.buf + io->s.read.curpos, ninbuf); io->s.read.curpos += ninbuf; /* If we need to read more, do so, reading directly into the * caller's buffer. Preserve alignment within our buffer at * the end of things, and read only in multiples of our block * size. */ if (ninbuf < nbytes) { gsize ntoread = nbytes - ninbuf; gsize nblocks = ntoread / io->bufsz; /* truncating div. */ if (nblocks > 0) { /* Read all but the last block directly into the user's buffer. */ gssize nread; ntoread = nblocks * io->bufsz; nread = _io_fd_read (io->fd, buf + ninbuf, ntoread, err); if (nread < 0) return -1; if (nread < ntoread) { /* EOF, short read */ io->s.read.curpos = 0; io->s.read.endpos = 0; io->s.read.eof = TRUE; } ntoread -= nread; ninbuf += nread; } if (ntoread > 0 && !io->s.read.eof) { /* Not EOF and we have a little bit more to read. (Specifically, we * have less than bufsz.) This batch gets read into the IO stream * buffer to preserve its alignment properties.*/ gsize navail; if (_io_read (io, err)) return -1; if (io->s.read.eof) navail = MIN (ntoread, io->s.read.endpos); else navail = ntoread; memcpy (buf + ninbuf, io->s.read.buf, navail); ninbuf += navail; io->s.read.curpos = navail; } } /* We may have a short read -- integral number of items read? */ if (ninbuf % ds_type_sizes[type] != 0) return -1; /* Yay, finished successfully. */ nvals = ninbuf / ds_type_sizes[type]; io_recode_data_inplace (buf, type, nvals); return nvals; }
gssize io_read_into_temp_buf (IOStream *io, gsize nbytes, gpointer *dest, GError **err) { g_assert (io->mode == IO_MODE_READ); /* disallow this situation for now. */ g_assert (nbytes <= io->bufsz); if (dest != NULL) *dest = NULL; if (io->s.read.eof) { /* The request is either entirely within the read * buffer or truncated by EOF; either way, we don't * need to use the scratch buf. */ if (dest != NULL) *dest = io->s.read.buf + io->s.read.curpos; if (io->s.read.curpos + nbytes <= io->s.read.endpos) { /* Even though we're near EOF we can still * fulfill this entire request. */ io->s.read.curpos += nbytes; return nbytes; } /* We can only partially fulfill the request. */ nbytes = io->s.read.endpos - io->s.read.curpos; io->s.read.curpos = io->s.read.endpos; return nbytes; } if (io->s.read.curpos == io->bufsz) { /* Last time we read exactly up to a block boundary. * Read in a new block and try again. */ if (_io_read (io, err)) return -1; return io_read_into_temp_buf (io, nbytes, dest, err); } if (io->s.read.curpos + nbytes <= io->bufsz) { /* Not at EOF, and the request is entirely buffered */ if (dest != NULL) *dest = io->s.read.buf + io->s.read.curpos; io->s.read.curpos += nbytes; return nbytes; } /* We handled EOF, and the request isn't entirely buffered, * and we handled the exact-buffer-boundary case. We must * be reading across the end of the current buffer. */ { gsize nlow = io->bufsz - io->s.read.curpos; gpointer buf2; gsize nhi; GError *suberr = NULL; /* Copy in what we've already got in the current buffer. */ memcpy (io->s.read.scratch, io->s.read.buf + io->s.read.curpos, nlow); /* Try to get the rest. May hit EOF. */ if (_io_read (io, err)) return -1; nhi = io_read_into_temp_buf (io, nbytes - nlow, &buf2, &suberr); if (suberr != NULL) { g_propagate_error (err, suberr); return -1; } memcpy (io->s.read.scratch + nlow, buf2, nhi); *dest = io->s.read.scratch; return nlow + nhi; } }