- while (len>0) {
- int len1 = MIN(len, SPARSE_WRITE_SIZE);
- int r1 = write_sparse(f, buf, len1);
+/*
+ * write_file does not allow incomplete writes. It loops internally
+ * until len bytes are written or errno is set.
+ */
+int write_file(int f,char *buf,size_t len)
+{
+ int ret = 0;
+
+ while (len > 0) {
+ int r1;
+ if (sparse_files > 0) {
+ int len1 = MIN(len, SPARSE_WRITE_SIZE);
+ r1 = write_sparse(f, buf, len1);
+ } else {
+ if (!wf_writeBuf) {
+ wf_writeBufSize = WRITE_SIZE * 8;
+ wf_writeBufCnt = 0;
+ wf_writeBuf = new_array(char, wf_writeBufSize);
+ if (!wf_writeBuf)
+ out_of_memory("write_file");
+ }
+ r1 = MIN(len, wf_writeBufSize - wf_writeBufCnt);
+ if (r1) {
+ memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
+ wf_writeBufCnt += r1;
+ }
+ if (wf_writeBufCnt == wf_writeBufSize) {
+ if (flush_write_file(f) < 0)
+ return -1;
+ if (!r1 && len)
+ continue;
+ }
+ }