static int fusecow_read(const char *path, char *buf, size_t size, off64_t offset, struct fuse_file_info *fi) { int res; long long int block_number = offset / block_size; if(offset + size > (block_number+1)*block_size) { size = (block_number+1)*block_size - offset; // read only one block } if(write_map_get(block_number)) { res=pread64(fd_write, buf, size, offset); } else if(read_map_get(block_number)) { res=pread64(fd_read, buf, size, offset); } else { res=pread64(fd, buf, size, offset); // Data was never read yet, write to copy-on-read file. if (res != write_read(buf, size, offset) && read_only) res = -errno; } if (res == -1) res = -errno; return res; }
size_t LocalFileLoader::ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags) { #if PPSSPP_PLATFORM(ANDROID) // pread64 doesn't appear to actually be 64-bit safe, though such ISOs are uncommon. See #10862. if (absolutePos <= 0x7FFFFFFF) { #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS < 64 return pread64(fd_, data, bytes * count, absolutePos) / bytes; #else return pread(fd_, data, bytes * count, absolutePos) / bytes; #endif } else { // Since pread64 doesn't change the file offset, it should be safe to avoid the lock in the common case. std::lock_guard<std::mutex> guard(readLock_); lseek64(fd_, absolutePos, SEEK_SET); return read(fd_, data, bytes * count) / bytes; } #elif !defined(_WIN32) #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS < 64 return pread64(fd_, data, bytes * count, absolutePos) / bytes; #else return pread(fd_, data, bytes * count, absolutePos) / bytes; #endif #else DWORD read = -1; OVERLAPPED offset = { 0 }; offset.Offset = (DWORD)(absolutePos & 0xffffffff); offset.OffsetHigh = (DWORD)((absolutePos & 0xffffffff00000000) >> 32); auto result = ReadFile(handle_, data, (DWORD)(bytes * count), &read, &offset); return result == TRUE ? (size_t)read / bytes : -1; #endif }
void ADIOI_HFS_ReadContig(ADIO_File fd, void *buf, int count, MPI_Datatype datatype, int file_ptr_type, ADIO_Offset offset, ADIO_Status *status, int *error_code) { MPI_Count err=-1, datatype_size, len; #ifndef PRINT_ERR_MSG static char myname[] = "ADIOI_HFS_READCONTIG"; #endif MPI_Type_size_x(datatype, &datatype_size); len = datatype_size * count; #ifdef SPPUX fd->fp_sys_posn = -1; /* set it to null, since we are using pread */ if (file_ptr_type == ADIO_EXPLICIT_OFFSET) err = pread64(fd->fd_sys, buf, len, offset); else { /* read from curr. location of ind. file pointer */ err = pread64(fd->fd_sys, buf, len, fd->fp_ind); fd->fp_ind += err; } #endif #ifdef HPUX if (file_ptr_type == ADIO_EXPLICIT_OFFSET) { if (fd->fp_sys_posn != offset) lseek64(fd->fd_sys, offset, SEEK_SET); err = read(fd->fd_sys, buf, len); fd->fp_sys_posn = offset + err; /* individual file pointer not updated */ } else { /* read from curr. location of ind. file pointer */ if (fd->fp_sys_posn != fd->fp_ind) lseek64(fd->fd_sys, fd->fp_ind, SEEK_SET); err = read(fd->fd_sys, buf, len); fd->fp_ind += err; fd->fp_sys_posn = fd->fp_ind; } #endif #ifdef HAVE_STATUS_SET_BYTES if (err != -1) MPIR_Status_set_bytes(status, datatype, err); #endif if (err == -1 ) { #ifdef MPICH *error_code = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE, myname, __LINE__, MPI_ERR_IO, "**io", "**io %s", strerror(errno)); #elif defined(PRINT_ERR_MSG) *error_code = (err == -1) ? MPI_ERR_UNKNOWN : MPI_SUCCESS; #else /* MPICH-1 */ *error_code = MPIR_Err_setmsg(MPI_ERR_IO, MPIR_ADIO_ERROR, myname, "I/O Error", "%s", strerror(errno)); ADIOI_Error(fd, *error_code, myname); #endif } else *error_code = MPI_SUCCESS; }
int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr) { u8 fsid[BTRFS_FSID_SIZE]; int fsid_is_initialized = 0; struct btrfs_super_block buf; int i; int ret; u64 transid = 0; u64 bytenr; if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) { ret = pread64(fd, &buf, sizeof(buf), sb_bytenr); if (ret < sizeof(buf)) return -1; if (btrfs_super_bytenr(&buf) != sb_bytenr || buf.magic != cpu_to_le64(BTRFS_MAGIC)) return -1; memcpy(sb, &buf, sizeof(*sb)); return 0; } for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { bytenr = btrfs_sb_offset(i); ret = pread64(fd, &buf, sizeof(buf), bytenr); if (ret < sizeof(buf)) break; if (btrfs_super_bytenr(&buf) != bytenr ) continue; /* if magic is NULL, the device was removed */ if (buf.magic == 0 && i == 0) return -1; if (buf.magic != cpu_to_le64(BTRFS_MAGIC)) continue; if (!fsid_is_initialized) { memcpy(fsid, buf.fsid, sizeof(fsid)); fsid_is_initialized = 1; } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) { /* * the superblocks (the original one and * its backups) contain data of different * filesystems -> the super cannot be trusted */ continue; } if (btrfs_super_generation(&buf) > transid) { memcpy(sb, &buf, sizeof(*sb)); transid = btrfs_super_generation(&buf); } } return transid > 0 ? 0 : -1; }
/* get PCI BAR number where MSI-X interrupts are */ static int pci_vfio_get_msix_bar(int fd, int *msix_bar, uint32_t *msix_table_offset, uint32_t *msix_table_size) { int ret; uint32_t reg; uint16_t flags; uint8_t cap_id, cap_offset; /* read PCI capability pointer from config space */ ret = pread64(fd, ®, sizeof(reg), VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + PCI_CAPABILITY_LIST); if (ret != sizeof(reg)) { RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI " "config space!\n"); return -1; } /* we need first byte */ cap_offset = reg & 0xFF; while (cap_offset) { /* read PCI capability ID */ ret = pread64(fd, ®, sizeof(reg), VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + cap_offset); if (ret != sizeof(reg)) { RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI " "config space!\n"); return -1; } /* we need first byte */ cap_id = reg & 0xFF; /* if we haven't reached MSI-X, check next capability */ if (cap_id != PCI_CAP_ID_MSIX) { ret = pread64(fd, ®, sizeof(reg), VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + cap_offset); if (ret != sizeof(reg)) { RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI " "config space!\n"); return -1; } /* we need second byte */ cap_offset = (reg & 0xFF00) >> 8; continue; } /* else, read table offset */ else {
static int handle_read(struct fuse* fuse, struct fuse_handler* handler, const struct fuse_in_header* hdr, const struct fuse_read_in* req) { struct handle *h = id_to_ptr(req->fh); __u64 unique = hdr->unique; __u32 size = req->size; __u64 offset = req->offset; int res; /* Don't access any other fields of hdr or req beyond this point, the read buffer * overlaps the request buffer and will clobber data in the request. This * saves us 128KB per request handler thread at the cost of this scary comment. */ TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token, h, h->fd, size, offset); if (size > sizeof(handler->read_buffer)) { return -EINVAL; } res = pread64(h->fd, handler->read_buffer, size, offset); if (res < 0) { return -errno; } fuse_reply(fuse, unique, handler->read_buffer, res); return NO_STATUS; }
int pci_vfio_read_config(const struct rte_intr_handle *intr_handle, void *buf, size_t len, off_t offs) { return pread64(intr_handle->vfio_dev_fd, buf, len, VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs); }
file_error osd_read(osd_file *file, void *buffer, UINT64 offset, UINT32 count, UINT32 *actual) { ssize_t result; switch (file->type) { case SDLFILE_FILE: #if defined(SDLMAME_DARWIN) || defined(SDLMAME_BSD) result = pread(file->handle, buffer, count, offset); if (result < 0) #elif defined(SDLMAME_WIN32) || defined(SDLMAME_NO64BITIO) || defined(SDLMAME_OS2) || defined(__CELLOS_LV2__) lseek(file->handle, (UINT32)offset&0xffffffff, SEEK_SET); result = read(file->handle, buffer, count); if (result < 0) #elif defined(SDLMAME_UNIX) result = pread64(file->handle, buffer, count, offset); if (result < 0) #else #error Unknown SDL SUBARCH! #endif return error_to_file_error(errno); if (actual != NULL) *actual = result; return FILERR_NONE; break; default: return FILERR_FAILURE; } }
/* * In order to read from a block-oriented device, we pick up the seek pointer, * read each containing block, and then copy the desired range of bytes back * into the caller's buffer. Unfortunately Solaris hardcodes the notion of * DEV_BSIZE as the transfer unit for such devices; no ioctl() to obtain the * transfer unit dynamically is currently available. At the end of the * transfer we reset the seek pointer to where the caller thinks it should be. */ static ssize_t fdio_bdev_read(mdb_io_t *io, void *buf, size_t nbytes) { fd_data_t *fdp = io->io_data; ssize_t resid = nbytes; uchar_t blk[DEV_BSIZE]; off64_t off; if (io->io_next != NULL) return (IOP_READ(io->io_next, buf, nbytes)); if ((off = lseek64(fdp->fd_fd, 0, SEEK_CUR)) == -1) return (-1); /* errno is set for us */ while (resid != 0) { off64_t devoff = off & ~(DEV_BSIZE - 1); size_t blkoff = off & (DEV_BSIZE - 1); size_t len = MIN(resid, DEV_BSIZE - blkoff); if (pread64(fdp->fd_fd, blk, DEV_BSIZE, devoff) != DEV_BSIZE) break; /* errno is set for us, unless EOF */ bcopy(&blk[blkoff], buf, len); resid -= len; off += len; buf = (char *)buf + len; } if (resid == nbytes && nbytes != 0) return (set_errno(EMDB_EOF)); (void) lseek64(fdp->fd_fd, off, SEEK_SET); return (nbytes - resid); }
void Driver_FILE::get_frame(const int frameid, Frame& frame) { #if defined(__MACH__) pthread_mutex_lock(&spinlock); #else pthread_spin_lock(&spinlock); #endif if(objmask.find(frameid) != objmask.end()){ // if in hash #ifdef __MACH__ ret = pread(fd, frame.content, this->framesize_in_byte, 1L*frameid*this->framesize_in_byte); #else ret = pread64(fd, frame.content, this->framesize_in_byte, 1L*frameid*this->framesize_in_byte); #endif if(ret < 0){ assert(false && "Driver_FILE: fail to read in get()"); } }else{ assert(false && "Driver_FILE: key not in hash"); } #if defined(__MACH__) pthread_mutex_unlock(&spinlock); #else pthread_spin_unlock(&spinlock); #endif }
static ssize_t Pread_idle(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr) { size_t resid = n; while (resid > 0) { map_info_t *mp; uintptr_t mapoff; ssize_t len; off64_t off; if ((mp = Paddr2mptr(P, addr)) == NULL) break; mapoff = addr - mp->map_pmap.pr_vaddr; len = MIN(resid, mp->map_pmap.pr_size - mapoff); off = mp->map_offset + mapoff; if ((len = pread64(P->asfd, buf, len, off)) <= 0) break; resid -= len; addr += len; buf = (char *)buf + len; } return (n - resid); }
static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full, int force) { u8 super_block_data[BTRFS_SUPER_INFO_SIZE]; struct btrfs_super_block *sb; u64 ret; sb = (struct btrfs_super_block *)super_block_data; ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr); if (ret != BTRFS_SUPER_INFO_SIZE) { /* check if the disk if too short for further superblock */ if (ret == 0 && errno == 0) return 0; fprintf(stderr, "ERROR: Failed to read the superblock on %s at %llu\n", filename, (unsigned long long)sb_bytenr); fprintf(stderr, "ERROR: error = '%s', errno = %d\n", strerror(errno), errno); return 1; } printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename); printf("---------------------------------------------------------\n"); if (btrfs_super_magic(sb) != BTRFS_MAGIC && !force) { fprintf(stderr, "ERROR: bad magic on superblock on %s at %llu\n", filename, (unsigned long long)sb_bytenr); } else { dump_superblock(sb, full); } return 0; }
static int read_physical(struct btrfs_root *root, int fd, u64 offset, u64 bytenr, u64 len) { char *iobuf = malloc(len); ssize_t done; size_t total_read = 0; int ret = 1; if (!iobuf) { fprintf(stderr, "No memory\n"); return -1; } while (total_read < len) { done = pread64(fd, iobuf + total_read, len - total_read, bytenr + total_read); if (done < 0) { fprintf(stderr, "Failed to read: %s\n", strerror(errno)); ret = -1; goto out; } total_read += done; } ret = search_iobuf(root, iobuf, total_read, offset); out: free(iobuf); return ret; }
bool_t FSFH_read(FSFileHandle *self, char *dest, int64_t offset, size_t len) { int64_t check_val; // Sanity check. if (offset < 0) { Err_set_error(Err_new(CB_newf("Can't read from an offset less than 0 (%i64)", offset))); return false; } // Read. check_val = pread64(self->fd, dest, len, offset); if (check_val != (int64_t)len) { if (check_val == -1) { Err_set_error(Err_new(CB_newf("Tried to read %u64 bytes, got %i64: %s", (uint64_t)len, check_val, strerror(errno)))); } else { Err_set_error(Err_new(CB_newf("Tried to read %u64 bytes, got %i64", (uint64_t)len, check_val))); } return false; } return true; }
static int read_block(int fd, void *buf, unsigned long block, unsigned long block_size) { char *b; off64_t offset; unsigned long remaining; ssize_t rc; b = buf; offset = block * block_size; remaining = block_size; while (remaining > 0) { rc = pread64(fd, b, remaining, offset); if (rc < 0) { if (errno == EINTR) continue; fprintf(stderr, "write failed: %s\n", strerror(errno)); return -1; } b += rc; offset += rc; remaining -= rc; } return 0; }
void write_map_set(long long int block, char val) { long long int offset = 32 + block/8; if((mem_write_map!=MAP_FAILED) && (offset > mem_write_map_size) ) { munmap(mem_write_map, mem_write_map_size); mem_write_map_size = (block/8/getpagesize()+1)*getpagesize(); ftruncate(fd_write_map, mem_write_map_size); mem_write_map = mmap(NULL, mem_write_map_size , PROT_READ|PROT_WRITE, MAP_SHARED, fd_write_map, 0); } char c; if(mem_write_map!=MAP_FAILED) { c = mem_write_map[offset]; } else { pread64(fd_write_map, &c, 1, offset); } char mask = 1 << (block%8); c &= ~mask; if(val) { c |= mask; } if(mem_write_map!=MAP_FAILED) { mem_write_map[offset]=c; } else { pwrite64(fd_write_map, &c, 1, offset); } }
static int root_read(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr, caller_context_t *ct) { ASSERT(vp->v_fd != -1); ASSERT(vp->v_type != VBLK || IS_P2ALIGNED(uiop->uio_loffset, 512)); ASSERT(vp->v_type != VBLK || IS_P2ALIGNED(uiop->uio_iov->iov_len, 512)); int error = 0; ssize_t iolen = pread64(vp->v_fd, uiop->uio_iov->iov_base, uiop->uio_iov->iov_len, uiop->uio_loffset); if(iolen == -1) { error = errno; perror("pread64"); } if(iolen != uiop->uio_iov->iov_len) fprintf(stderr, "root_read(): len: %lli iolen: %lli offset: %lli file: %s\n", (longlong_t) uiop->uio_iov->iov_len, (longlong_t) iolen, (longlong_t) uiop->uio_loffset, vp->v_path); if(error) return error; uiop->uio_resid -= iolen; return 0; }
extern jlong file_read(JNIEnv *env, jlong fhandle, jlong seek, jlong length, jlong buffer) { /* Set fixed values at start and end of buffer: */ prepare_read_buffer(env, buffer, length); int rc = pread64((int) fhandle, (void*) buffer, (size_t) (int) length, (off64_t) seek); if (rc == -1) { if (errno == 0) { PTOD("Errno is zero after a failed read. Setting to 799"); return 799; } return errno; } else if (rc != length) { PTOD1("Invalid byte count. Expecting %lld", length); PTOD1("but read only %d bytes.", rc);; return 798; } /* Make sure read was REALLY OK: */ return check_read_buffer(env, buffer, length); }
int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr) { u8 fsid[BTRFS_FSID_SIZE]; struct btrfs_super_block buf; int i; int ret; u64 transid = 0; u64 bytenr; if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) { ret = pread64(fd, &buf, sizeof(buf), sb_bytenr); if (ret < sizeof(buf)) return -1; if (btrfs_super_bytenr(&buf) != sb_bytenr || strncmp((char *)(&buf.magic), BTRFS_MAGIC, sizeof(buf.magic))) return -1; memcpy(sb, &buf, sizeof(*sb)); return 0; } for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { bytenr = btrfs_sb_offset(i); ret = pread64(fd, &buf, sizeof(buf), bytenr); if (ret < sizeof(buf)) break; if (btrfs_super_bytenr(&buf) != bytenr || strncmp((char *)(&buf.magic), BTRFS_MAGIC, sizeof(buf.magic))) continue; if (i == 0) memcpy(fsid, buf.fsid, sizeof(fsid)); else if (memcmp(fsid, buf.fsid, sizeof(fsid))) continue; if (btrfs_super_generation(&buf) > transid) { memcpy(sb, &buf, sizeof(*sb)); transid = btrfs_super_generation(&buf); } } return transid > 0 ? 0 : -1; }
ssize_t full_pread64(int fd, void *buf, size_t count, int64_t offset) { #if CCTOOLS_OPSYS_CYGWIN || CCTOOLS_OPSYS_DARWIN || CCTOOLS_OPSYS_FREEBSD || CCTOOLS_OPSYS_DRAGONFLY FULL_PIO(pread(fd, buf, count, offset)); #else FULL_PIO(pread64(fd, buf, count, offset)); #endif }
static int pci_device_linux_sysfs_read( struct pci_device * dev, void * data, pciaddr_t offset, pciaddr_t size, pciaddr_t * bytes_read ) { char name[256]; pciaddr_t temp_size = size; int err = 0; int fd; char *data_bytes = data; if ( bytes_read != NULL ) { *bytes_read = 0; } /* Each device has a directory under sysfs. Within that directory there * is a file named "config". This file used to access the PCI config * space. It is used here to obtain most of the information about the * device. */ snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config", SYS_BUS_PCI, dev->domain, dev->bus, dev->dev, dev->func ); fd = open( name, O_RDONLY | O_CLOEXEC); if ( fd == -1 ) { return errno; } while ( temp_size > 0 ) { const ssize_t bytes = pread64( fd, data_bytes, temp_size, offset ); /* If zero bytes were read, then we assume it's the end of the * config file. */ if (bytes == 0) break; if ( bytes < 0 ) { err = errno; break; } temp_size -= bytes; offset += bytes; data_bytes += bytes; } if ( bytes_read != NULL ) { *bytes_read = size - temp_size; } close( fd ); return err; }
JNIEXPORT jint JNICALL Java_sun_nio_ch_FileDispatcher_pread0(JNIEnv *env, jclass clazz, jobject fdo, jlong address, jint len, jlong offset) { jint fd = fdval(env, fdo); void *buf = (void *)jlong_to_ptr(address); return convertReturnVal(env, pread64(fd, buf, len, offset), JNI_TRUE); }
int read_file(int fd, unsigned long long *offset, char *buf, int len) { int n; n = pread64(fd, buf, len, *offset); if(n < 0) return(-errno); *offset += n; return(n); }
void check_pread(int fd, void* buf, size_t size, off_t offset, const char* msg, const char* fname) { int r = pread64(fd, buf, size, offset); if (r != size) { fprintf(stderr, "read %d, expected %d at offset %lld from %s\n", r, (int) size, (longlong) offset, fname); perror(msg); assert(0); } }
ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size, loff_t offset) { #ifdef USE_UBLIO return ublio_pread(dev->ufh, buffer, size, offset); #elif defined(__ANDROID__) return pread64(dev->fd, buffer, size, offset); #else return pread(dev->fd, buffer, size, offset); #endif }
ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size, off64_t offset) { #ifdef USE_UBLIO return ublio_pread(dev->ufh, buffer, size, offset); #elif defined(USE_TRANSITIONAL_LFS) && USE_TRANSITIONAL_LFS return pread64(dev->fd, buffer, size, offset); #else return pread(dev->fd, buffer, size, offset); #endif }
void read_block(int fd, lba lba, block *data) { ssize_t result = pread64(fd, data, sizeof(block), lba*sizeof(block)); if(result == -1) { fprintf(stderr, "Unable to read LBA %Li: %s\n", lba, strerror(errno)); exit(1); } if(result != sizeof(block)) { fprintf(stderr, "Inexplicably got wrong amount of bytes for LBA %Li\n", lba); exit(1); } }
static int do_pread( int fd, off64_t offset, ssize_t count, ssize_t buffer_size) { if (!vectors) return pread64(fd, buffer, min(count, buffer_size), offset); return do_preadv(fd, offset, count, buffer_size); }
static void Pread(int fd, void *buf, size_t size, offset_t off) { ssize_t sz = pread64(fd, buf, size, off); if (sz < 0) logprint(SC_SL_ERR | SC_EXIT_ERR, "pread: %s", strerror(errno)); else if (sz != size) logprint(SC_SL_ERR | SC_EXIT_ERR, "pread: size %ld != %ld", sz, size); }
tb_long_t tb_file_pread(tb_file_ref_t file, tb_byte_t* data, tb_size_t size, tb_hize_t offset) { // check tb_assert_and_check_return_val(file, -1); // read it #ifdef TB_CONFIG_POSIX_HAVE_PREAD64 return pread64(tb_file2fd(file), data, (size_t)size, offset); #else return pread(tb_file2fd(file), data, (size_t)size, offset); #endif }