ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset) { //printf("INTERCEPTED pwrite\n"); clock_t start = clock(); ssize_t ret = _pwrite(fd,buf,count,offset); clock_t end = clock(); double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC); double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC); fprintf(logFile,"%lf %lf pwrite %d %p %zu %ld = %zu\n",called_time,exec_time,fd,buf,count,offset,ret); return ret; }
ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset) { const char *p = buf; ssize_t total = 0; while (count > 0) { ssize_t written = _pwrite(fd, p, count, offset); if (unlikely(written < 0)) return -1; if (unlikely(!written)) { errno = ENOSPC; return -1; } count -= written; p += written; total += written; offset += written; } return total; }
/** * ntfs_pwrite - positioned write to disk * @dev: device to write to * @pos: position in file descriptor to write to * @count: number of bytes to write * @b: data buffer to write to disk * * This function will write @count bytes from data buffer @b to the device @dev * at position @pos. * * On success, return the number of successfully written bytes. If this number * is lower than @count this means that the write has been interrupted in * flight or that an error was encountered during the write so that the write * is partial. 0 means nothing was written (also return 0 when @count is 0). * * On error and nothing has been written, return -1 with errno set * appropriately to the return code of either seek, write, or set * to EINVAL in case of invalid arguments. */ s64 ntfs_pwrite(struct ntfs_device *dev, const s64 pos, s64 count, const void *b) { s64 written, total; struct ntfs_device_operations *dops; s64 (*_pwrite)(struct ntfs_device *, const void *, s64, s64); ntfs_log_trace("Entering for pos 0x%llx, count 0x%llx.\n", pos, count); if (!b || count < 0 || pos < 0) { errno = EINVAL; return -1; } if (!count) return 0; if (NDevReadOnly(dev)) { errno = EROFS; return -1; } dops = dev->d_ops; _pwrite = dops->pwrite; if (!_pwrite) _pwrite = fake_pwrite; seek: /* * Locate to position if pwrite is to be emulated by seek() + write(). */ if (_pwrite == fake_pwrite && dops->seek(dev, pos, SEEK_SET) == (off_t)-1) { ntfs_log_perror("ntfs_pwrite: seek to 0x%llx returned error", pos); return -1; } NDevSetDirty(dev); /* Write the data. */ for (total = 0; count; count -= written, total += written) { written = _pwrite(dev, (const char*)b + total, count, pos + total); /* If everything ok, continue. */ if (written > 0) continue; /* * If nothing written or error return number of bytes written. */ if (!written || total) break; /* * If pwrite is not supported by the OS, fall back to emulating * it by seek() + write() and set the device pwrite() pointer * to NULL so we automatically use seek() + write() from now * on. */ if (errno == ENOSYS && _pwrite != fake_pwrite) { _pwrite = fake_pwrite; dops->pwrite = NULL; goto seek; } /* Nothing written and error, return error status. */ return written; } /* Finally, return the number of bytes written. */ return total; }