static int write_note(int fd, uint_t type, const void *desc, size_t descsz, off64_t *offp) { /* * Note headers are the same regardless of the data model of the * ELF file; we arbitrarily use Elf64_Nhdr here. */ struct { Elf64_Nhdr nhdr; char name[8]; } n; bzero(&n, sizeof (n)); bcopy("CORE", n.name, 4); n.nhdr.n_type = type; n.nhdr.n_namesz = 5; n.nhdr.n_descsz = roundup(descsz, 4); if (pwrite64(fd, &n, sizeof (n), *offp) != sizeof (n)) return (-1); *offp += sizeof (n); if (pwrite64(fd, desc, n.nhdr.n_descsz, *offp) != n.nhdr.n_descsz) return (-1); *offp += n.nhdr.n_descsz; return (0); }
// FlowField2D constructor int FlowField2D::SaveFlowField2D(char* filename){ int fd_g,pf; ostream* o_stream = &cout; ofstream* f_stream = (ofstream*)(o_stream); ssize_t max_write = 1024L*1024L*1024L; ssize_t one_write = 0L; ssize_t len = 0L; off_t off = 0L; LoadSwapFile2D(filename,GetX(),GetY(),sizeof(FlowNode2D< FP, NUM_COMPONENTS>),&pf,&fd_g,f_stream); char* TmpPtr=(char*)GetMatrixPtr(); if(GetMatrixSize() > max_write) { for(off = 0L,one_write = max_write; len < GetMatrixSize(); off += max_write) { len += pwrite64(fd_g,TmpPtr+off,one_write,off); if(GetMatrixSize() - len < max_write) one_write = GetMatrixSize() - len; } if(len != GetMatrixSize()) *f_stream << "Error: len(" << len << ") != MatrixSize(" << GetMatrixSize() << ") " << endl << flush; } else { len = pwrite64(fd_g,GetMatrixPtr(),GetMatrixSize(),0L); } close(fd_g); return len; }
static int fusecow_write(const char *path, const char *buf, size_t size, off64_t offset, struct fuse_file_info *fi) { (void) fi; long long int block_number = offset / block_size; if(offset + size > (block_number+1)*block_size) { size = (block_number+1)*block_size - offset; // write only one block. write_safe will care } int res; if(write_map_get(block_number)) { res=pwrite64(fd_write, buf, size, offset); } else { int remaining = block_size; while(remaining) { res=pread64(fd, copyup_buffer + block_size - remaining, remaining, block_number*block_size); if(res==0) { memset(copyup_buffer + block_size - remaining, 0, remaining); break; } if(res==-1) { if(errno==EINTR) continue; return -errno; } remaining -= res; } memcpy(copyup_buffer+offset%block_size, buf, size); remaining=block_size; while(remaining) { fprintf(stderr, "Performing write at offset %lld\n", block_number*block_size); res=pwrite64(fd_write, copyup_buffer + block_size - remaining, remaining, block_number*block_size); if(res==-1) { if(errno==EINTR) continue; return -errno; } remaining -= res; } write_map_set(block_number, 1); res = size; } if (res == -1) { res = -errno; } return res; }
static int write_shdr(pgcore_t *pgc, shstrtype_t name, uint_t type, ulong_t flags, uintptr_t addr, ulong_t offset, size_t size, uint_t link, uint_t info, uintptr_t addralign, uintptr_t entsize) { if (pgc->P->status.pr_dmodel == PR_MODEL_ILP32) { Elf32_Shdr shdr; bzero(&shdr, sizeof (shdr)); shdr.sh_name = shstrtab_ndx(&pgc->pgc_shstrtab, name); shdr.sh_type = type; shdr.sh_flags = flags; shdr.sh_addr = (Elf32_Addr)addr; shdr.sh_offset = offset; shdr.sh_size = size; shdr.sh_link = link; shdr.sh_info = info; shdr.sh_addralign = addralign; shdr.sh_entsize = entsize; if (pwrite64(pgc->pgc_fd, &shdr, sizeof (shdr), *pgc->pgc_soff) != sizeof (shdr)) return (-1); *pgc->pgc_soff += sizeof (shdr); #ifdef _LP64 } else { Elf64_Shdr shdr; bzero(&shdr, sizeof (shdr)); shdr.sh_name = shstrtab_ndx(&pgc->pgc_shstrtab, name); shdr.sh_type = type; shdr.sh_flags = flags; shdr.sh_addr = addr; shdr.sh_offset = offset; shdr.sh_size = size; shdr.sh_link = link; shdr.sh_info = info; shdr.sh_addralign = addralign; shdr.sh_entsize = entsize; if (pwrite64(pgc->pgc_fd, &shdr, sizeof (shdr), *pgc->pgc_soff) != sizeof (shdr)) return (-1); *pgc->pgc_soff += sizeof (shdr); #endif /* _LP64 */ } return (0); }
int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb, struct btrfs_device *device) { u64 bytenr; u32 crc; int i, ret; if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) { btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr); crc = ~(u32)0; crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); btrfs_csum_final(crc, (char *)&sb->csum[0]); /* * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is * zero filled, we can use it directly */ ret = pwrite64(device->fd, root->fs_info->super_copy, BTRFS_SUPER_INFO_SIZE, root->fs_info->super_bytenr); BUG_ON(ret != BTRFS_SUPER_INFO_SIZE); return 0; } for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { bytenr = btrfs_sb_offset(i); if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes) break; btrfs_set_super_bytenr(sb, bytenr); crc = ~(u32)0; crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); btrfs_csum_final(crc, (char *)&sb->csum[0]); /* * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is * zero filled, we can use it directly */ ret = pwrite64(device->fd, root->fs_info->super_copy, BTRFS_SUPER_INFO_SIZE, bytenr); BUG_ON(ret != BTRFS_SUPER_INFO_SIZE); } return 0; }
file_error osd_write(osd_file *file, const void *buffer, UINT64 offset, UINT32 count, UINT32 *actual) { UINT32 result; switch (file->type) { case SDLFILE_FILE: #if defined(SDLMAME_DARWIN) || defined(SDLMAME_BSD) result = pwrite(file->handle, buffer, count, offset); if (!result) #elif defined(SDLMAME_WIN32) || defined(SDLMAME_NO64BITIO) || defined(SDLMAME_OS2) || defined(__CELLOS_LV2__) lseek(file->handle, (UINT32)offset&0xffffffff, SEEK_SET); result = write(file->handle, buffer, count); if (!result) #elif defined(SDLMAME_UNIX) result = pwrite64(file->handle, buffer, count, offset); if (!result) #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; } }
void Driver_FILE::set_frame(const int frameid, const 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 = pwrite(fd, frame.content, this->framesize_in_byte, 1L*frameid*this->framesize_in_byte); #else ret = pwrite64(fd, frame.content, this->framesize_in_byte, 1L*frameid*this->framesize_in_byte); #endif if(ret < 0){ assert(false && "Driver_FILE: fail to write in set()"); } }else{ assert(false && "Driver_FILE: key not in hash"); } #if defined(__MACH__) pthread_mutex_unlock(&spinlock); #else pthread_spin_unlock(&spinlock); #endif }
void Driver_FILE::add_frame(const int frameid) { assert(frameid >= 0 && "Driver_FILE: only support INTEGER-many frames for now. If you want larger storage, please increase the framesize instead."); #if defined(__MACH__) pthread_mutex_lock(&spinlock); #else pthread_spin_lock(&spinlock); #endif if(objmask.find(frameid) != objmask.end()){ // if already contains the key, error assert(false && "Driver_FILE: duplicate key init'ed"); }else{ // else, insert using the default constructor objmask[frameid] = true; } #ifdef __MACH__ ret = pwrite(fd, empty_frame.content, this->framesize_in_byte, 1L*frameid*this->framesize_in_byte); #else ret = pwrite64(fd, empty_frame.content, this->framesize_in_byte, 1L*frameid*this->framesize_in_byte); #endif if(ret < 0){ assert(false && "Driver_FILE: fail to write in init()"); } #if defined(__MACH__) pthread_mutex_unlock(&spinlock); #else pthread_spin_unlock(&spinlock); #endif }
int pci_vfio_write_config(const struct rte_intr_handle *intr_handle, const void *buf, size_t len, off_t offs) { return pwrite64(intr_handle->vfio_dev_fd, buf, len, VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs); }
/* * Given a file descriptor, clear (zero) the label information. */ int zpool_clear_label(int fd) { struct stat statbuf; int l; vdev_label_t *label; uint64_t size; if (fstat_blk(fd, &statbuf) == -1) return (0); size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL) return (-1); for (l = 0; l < VDEV_LABELS; l++) { if (pwrite64(fd, label, sizeof (vdev_label_t), label_offset(size, l)) != sizeof (vdev_label_t)) { free(label); return (-1); } } free(label); return (0); }
static int root_write(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 = pwrite64(vp->v_fd, uiop->uio_iov->iov_base, uiop->uio_iov->iov_len, uiop->uio_loffset); if(iolen == -1) { error = errno; perror("pwrite64"); } if(iolen != uiop->uio_iov->iov_len) fprintf(stderr, "root_write(): 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; }
static int fusecow_write_safe(const char *path, const char *buf, size_t size, off64_t offset, struct fuse_file_info *fi) { int res=0; if(strcmp(path, "/") != 0) return -ENOENT; size_t remaining = size; while(remaining) { int ret=fusecow_write(path, buf+res, remaining, offset+res, fi); if(ret==0) { break; } if(ret==-1) { if(errno==EINTR) continue; return -errno; } res+=ret; remaining-=ret; } if(offset+res > st_size) { fprintf(stderr, "Growing files is known to fail\n"); return -ENOSPC; st_size = offset+res; if(mem_write_map!=MAP_FAILED) { memcpy(mem_write_map+8, &st_size, sizeof st_size); } else { pwrite64(fd_write_map, &st_size, sizeof st_size, 8); } } return res; }
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 write_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 = pwrite64(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; }
static int write_verity_state(const char *fname, off64_t offset, int32_t mode) { int fd; int rc = -1; struct verity_state s = { VERITY_STATE_HEADER, VERITY_STATE_VERSION, mode }; fd = TEMP_FAILURE_RETRY(open(fname, O_WRONLY | O_SYNC | O_CLOEXEC)); if (fd == -1) { PERROR << "Failed to open " << fname; goto out; } if (TEMP_FAILURE_RETRY(pwrite64(fd, &s, sizeof(s), offset)) != sizeof(s)) { PERROR << "Failed to write " << sizeof(s) << " bytes to " << fname << " to offset " << offset; goto out; } rc = 0; out: if (fd != -1) { close(fd); } return rc; }
static void Pwrite(int fd, void *buf, size_t size, off64_t off) { if (pwrite64(fd, buf, size, off) != size) logprint(SC_SL_ERR | SC_EXIT_ERR, "pwrite: %s", strerror(errno)); }
ssize_t RandomAccessFile::write(uint64_t offset, void* src, size_t size) { BOOST_ASSERT( size <= WINDOW_SIZE ); ssize_t written = pwrite64(_fd, src, size, offset); if ((size_t)written != size) throw std::ios_base::failure("Failed to write"); return written; }
ssize_t full_pwrite64(int fd, const void *buf, size_t count, int64_t offset) { #if CCTOOLS_OPSYS_CYGWIN || CCTOOLS_OPSYS_DARWIN || CCTOOLS_OPSYS_FREEBSD || CCTOOLS_OPSYS_DRAGONFLY FULL_PIO(pwrite(fd, buf, count, offset)); #else FULL_PIO(pwrite64(fd, buf, count, offset)); #endif }
static int pci_device_linux_sysfs_write( struct pci_device * dev, const void * data, pciaddr_t offset, pciaddr_t size, pciaddr_t * bytes_written ) { char name[256]; pciaddr_t temp_size = size; int err = 0; int fd; const char *data_bytes = data; if ( bytes_written != NULL ) { *bytes_written = 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_WRONLY | O_CLOEXEC); if ( fd == -1 ) { return errno; } while ( temp_size > 0 ) { const ssize_t bytes = pwrite64( fd, data_bytes, temp_size, offset ); /* If zero bytes were written, 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_written != NULL ) { *bytes_written = size - temp_size; } close( fd ); return err; }
int write_file(int fd, unsigned long long *offset, const char *buf, int len) { int n; n = pwrite64(fd, buf, len, *offset); if(n < 0) return(-errno); *offset += n; return(n); }
JNIEXPORT jint JNICALL Java_sun_nio_ch_FileDispatcher_pwrite0(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, pwrite64(fd, buf, len, offset), JNI_FALSE); }
static int write_backward( off64_t offset, long long *count, long long *total) { off64_t end, off = offset; ssize_t bytes = 0, bytes_requested; long long cnt = *count; int ops = 0; if ((end = off - cnt) < 0) { cnt += end; /* subtraction, end is negative */ end = 0; } *total = 0; *count = cnt; /* Do initial unaligned write if needed */ if ((bytes_requested = (off % buffersize))) { bytes_requested = min(cnt, bytes_requested); off -= bytes_requested; bytes = pwrite(file->fd, buffer, bytes_requested, off); if (bytes == 0) return ops; if (bytes < 0) { perror("pwrite64"); return -1; } ops++; *total += bytes; if (bytes < bytes_requested) return ops; cnt -= bytes; } /* Iterate backward through the rest of the range */ while (cnt > end) { bytes_requested = min(cnt, buffersize); off -= bytes_requested; bytes = pwrite64(file->fd, buffer, bytes_requested, off); if (bytes == 0) break; if (bytes < 0) { perror("pwrite64"); return -1; } ops++; *total += bytes; if (bytes < bytes_requested) break; cnt -= bytes; } return ops; }
ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size, loff_t offset) { #ifdef USE_UBLIO return ublio_pwrite(dev->ufh, buffer, size, offset); #elif defined(__ANDROID__) return pwrite64(dev->fd, buffer, size, offset); #else return pwrite(dev->fd, buffer, size, offset); #endif }
ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size, off64_t offset) { #ifdef USE_UBLIO return ublio_pwrite(dev->ufh, buffer, size, offset); #elif defined(USE_TRANSITIONAL_LFS) && USE_TRANSITIONAL_LFS return pwrite64(dev->fd, buffer, size, offset); #else return pwrite(dev->fd, buffer, size, offset); #endif }
static int do_pwrite( int fd, off64_t offset, ssize_t count, ssize_t buffer_size) { if (!vectors) return pwrite64(fd, buffer, min(count, buffer_size), offset); return do_pwritev(fd, offset, count, buffer_size); }
tb_long_t tb_file_pwrit(tb_file_ref_t file, tb_byte_t const* data, tb_size_t size, tb_hize_t offset) { // check tb_assert_and_check_return_val(file, -1); // writ it #ifdef TB_CONFIG_POSIX_HAVE_PWRITE64 return pwrite64(tb_file2fd(file), data, (size_t)size, offset); #else return pwrite(tb_file2fd(file), data, (size_t)size, offset); #endif }
extern "C" ssize_t __pwrite64_chk(int fd, const void* buf, size_t count, off64_t offset, size_t buf_size) { if (__predict_false(count > buf_size)) { __fortify_chk_fail("pwrite64: prevented read past end of buffer", 0); } if (__predict_false(count > SSIZE_MAX)) { __fortify_chk_fail("pwrite64: count > SSIZE_MAX", 0); } return pwrite64(fd, buf, count, offset); }
void CloseSwapFile(char* SwapFileName, void* SwapData, ssize_t FileSize, int fd, int isDel #ifdef _MPI ,int rank #endif //_MPI ) { #ifdef WRITE_LARGE_FILE #warning "Write file > 2Gb" ssize_t max_write = 1024L*1024L*1024L; ssize_t one_write = 0L; ssize_t len = 0L; off_t off = 0L; char* TmpPtr=(char*)SwapData; if ( FileSize > max_write ) { for ( off = 0L,one_write = max_write; len < FileSize; off += max_write ) { len += pwrite64(fd,TmpPtr+off,one_write,off); if ( FileSize - len < max_write ) one_write = FileSize - len; } if ( len != FileSize ) #ifdef _MPI if ( rank == 0 ) #endif //_MPI cout << "Error: len(" << len << ") != FileSize(" << FileSize << ") " << endl << flush; } else { len = pwrite64(fd,SwapData,FileSize,0L); } #else msync(SwapData,FileSize,MS_SYNC); #endif // WRITE_LARGE_FILE munmap(SwapData,FileSize); close(fd); #ifdef _REMOVE_SWAPFILE_ if ( isDel ) unlink(SwapFileName); #endif //_REMOVE_SWAPFILE_ SwapData=NULL; }
ssize_t sys_pwrite(int fd, const void *buf, size_t count, SMB_OFF_T off) { ssize_t ret; do { #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PWRITE64) ret = pwrite64(fd, buf, count, off); #else ret = pwrite(fd, buf, count, off); #endif } while (ret == -1 && errno == EINTR); return ret; }
static int dump_symtab(pgcore_t *pgc, file_info_t *fptr, uint_t index, int dynsym) { sym_tbl_t *sym = dynsym ? &fptr->file_dynsym : &fptr->file_symtab; shstrtype_t symname = dynsym ? STR_DYNSYM : STR_SYMTAB; shstrtype_t strname = dynsym ? STR_DYNSTR : STR_STRTAB; uint_t symtype = dynsym ? SHT_DYNSYM : SHT_SYMTAB; size_t size; uintptr_t addr = fptr->file_map->map_pmap.pr_vaddr; if (sym->sym_data_pri == NULL || sym->sym_symn == 0 || sym->sym_strs == NULL) return (0); size = sym->sym_hdr_pri.sh_size; if (pwrite64(pgc->pgc_fd, sym->sym_data_pri->d_buf, size, *pgc->pgc_doff) != size) return (-1); if (write_shdr(pgc, symname, symtype, 0, addr, *pgc->pgc_doff, size, index + 1, sym->sym_hdr_pri.sh_info, sym->sym_hdr_pri.sh_addralign, sym->sym_hdr_pri.sh_entsize) != 0) return (-1); *pgc->pgc_doff += roundup(size, 8); size = sym->sym_strhdr.sh_size; if (pwrite64(pgc->pgc_fd, sym->sym_strs, size, *pgc->pgc_doff) != size) return (-1); if (write_shdr(pgc, strname, SHT_STRTAB, SHF_STRINGS, addr, *pgc->pgc_doff, size, 0, 0, 1, 0) != 0) return (-1); *pgc->pgc_doff += roundup(size, 8); return (0); }