📄 readwrite.c
字号:
} apr_thread_mutex_unlock(thefile->mutex); return rv; } else { if (!thefile->pipe) { apr_off_t offset = 0; apr_status_t rc; if (thefile->append) { /* apr_file_lock will mutex the file across processes. * The call to apr_thread_mutex_lock is added to avoid * a race condition between LockFile and WriteFile * that occasionally leads to deadlocked threads. */ apr_thread_mutex_lock(thefile->mutex); rc = apr_file_lock(thefile, APR_FLOCK_EXCLUSIVE); if (rc != APR_SUCCESS) { apr_thread_mutex_unlock(thefile->mutex); return rc; } rc = apr_file_seek(thefile, APR_END, &offset); if (rc != APR_SUCCESS) { apr_thread_mutex_unlock(thefile->mutex); return rc; } } if (thefile->pOverlapped) { thefile->pOverlapped->Offset = (DWORD)thefile->filePtr; thefile->pOverlapped->OffsetHigh = (DWORD)(thefile->filePtr >> 32); } rv = WriteFile(thefile->filehand, buf, (DWORD)*nbytes, &bwrote, thefile->pOverlapped); if (thefile->append) { apr_file_unlock(thefile); apr_thread_mutex_unlock(thefile->mutex); } } else { rv = WriteFile(thefile->filehand, buf, (DWORD)*nbytes, &bwrote, thefile->pOverlapped); } if (rv) { *nbytes = bwrote; rv = APR_SUCCESS; } else { (*nbytes) = 0; rv = apr_get_os_error(); if (rv == APR_FROM_OS_ERROR(ERROR_IO_PENDING)) { DWORD timeout_ms; if (thefile->timeout == 0) { timeout_ms = 0; } else if (thefile->timeout < 0) { timeout_ms = INFINITE; } else { timeout_ms = (DWORD)(thefile->timeout / 1000); } rv = WaitForSingleObject(thefile->pOverlapped->hEvent, timeout_ms); switch (rv) { case WAIT_OBJECT_0: GetOverlappedResult(thefile->filehand, thefile->pOverlapped, &bwrote, TRUE); *nbytes = bwrote; rv = APR_SUCCESS; break; case WAIT_TIMEOUT: rv = APR_TIMEUP; break; case WAIT_FAILED: rv = apr_get_os_error(); break; default: break; } if (rv != APR_SUCCESS) { if (apr_os_level >= APR_WIN_98) CancelIo(thefile->filehand); } } } if (rv == APR_SUCCESS && thefile->pOverlapped && !thefile->pipe) { thefile->filePtr += *nbytes; } } return rv;}/* ToDo: Write for it anyway and test the oslevel! * Too bad WriteFileGather() is not supported on 95&98 (or NT prior to SP2) */APR_DECLARE(apr_status_t) apr_file_writev(apr_file_t *thefile, const struct iovec *vec, apr_size_t nvec, apr_size_t *nbytes){ apr_status_t rv = APR_SUCCESS; apr_size_t i; apr_size_t bwrote = 0; char *buf; *nbytes = 0; for (i = 0; i < nvec; i++) { buf = vec[i].iov_base; bwrote = vec[i].iov_len; rv = apr_file_write(thefile, buf, &bwrote); *nbytes += bwrote; if (rv != APR_SUCCESS) { break; } } return rv;}APR_DECLARE(apr_status_t) apr_file_putc(char ch, apr_file_t *thefile){ apr_size_t len = 1; return apr_file_write(thefile, &ch, &len);}APR_DECLARE(apr_status_t) apr_file_ungetc(char ch, apr_file_t *thefile){ thefile->ungetchar = (unsigned char) ch; return APR_SUCCESS;}APR_DECLARE(apr_status_t) apr_file_getc(char *ch, apr_file_t *thefile){ apr_status_t rc; apr_size_t bread; bread = 1; rc = apr_file_read(thefile, ch, &bread); if (rc) { return rc; } if (bread == 0) { thefile->eof_hit = TRUE; return APR_EOF; } return APR_SUCCESS; }APR_DECLARE(apr_status_t) apr_file_puts(const char *str, apr_file_t *thefile){ apr_size_t len = strlen(str); return apr_file_write(thefile, str, &len);}APR_DECLARE(apr_status_t) apr_file_gets(char *str, int len, apr_file_t *thefile){ apr_size_t readlen; apr_status_t rv = APR_SUCCESS; int i; for (i = 0; i < len-1; i++) { readlen = 1; rv = apr_file_read(thefile, str+i, &readlen); if (rv != APR_SUCCESS && rv != APR_EOF) return rv; if (readlen == 0) { /* If we have bytes, defer APR_EOF to the next call */ if (i > 0) rv = APR_SUCCESS; break; } if (str[i] == '\n') { i++; /* don't clobber this char below */ break; } } str[i] = 0; return rv;}APR_DECLARE(apr_status_t) apr_file_flush(apr_file_t *thefile){ if (thefile->buffered) { DWORD numbytes, written = 0; apr_status_t rc = 0; char *buffer; apr_size_t bytesleft; if (thefile->direction == 1 && thefile->bufpos) { buffer = thefile->buffer; bytesleft = thefile->bufpos; do { if (bytesleft > APR_DWORD_MAX) { numbytes = APR_DWORD_MAX; } else { numbytes = (DWORD)bytesleft; } if (!WriteFile(thefile->filehand, buffer, numbytes, &written, NULL)) { rc = apr_get_os_error(); thefile->filePtr += written; break; } thefile->filePtr += written; bytesleft -= written; buffer += written; } while (bytesleft > 0); if (rc == 0) thefile->bufpos = 0; } return rc; } /* There isn't anything to do if we aren't buffering the output * so just return success. */ return APR_SUCCESS; }struct apr_file_printf_data { apr_vformatter_buff_t vbuff; apr_file_t *fptr; char *buf;};static int file_printf_flush(apr_vformatter_buff_t *buff){ struct apr_file_printf_data *data = (struct apr_file_printf_data *)buff; if (apr_file_write_full(data->fptr, data->buf, data->vbuff.curpos - data->buf, NULL)) { return -1; } data->vbuff.curpos = data->buf; return 0;}APR_DECLARE_NONSTD(int) apr_file_printf(apr_file_t *fptr, const char *format, ...){ struct apr_file_printf_data data; va_list ap; int count; data.buf = malloc(HUGE_STRING_LEN); if (data.buf == NULL) { return 0; } data.vbuff.curpos = data.buf; data.vbuff.endpos = data.buf + HUGE_STRING_LEN; data.fptr = fptr; va_start(ap, format); count = apr_vformatter(file_printf_flush, (apr_vformatter_buff_t *)&data, format, ap); /* apr_vformatter does not call flush for the last bits */ if (count >= 0) file_printf_flush((apr_vformatter_buff_t *)&data); va_end(ap); free(data.buf); return count;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -