📄 open.c
字号:
if (flag & APR_DELONCLOSE) {
attributes |= FILE_FLAG_DELETE_ON_CLOSE;
}
if (flag & APR_OPENLINK) {
attributes |= FILE_FLAG_OPEN_REPARSE_POINT;
}
/* Without READ or WRITE, we fail unless apr called apr_file_open
* internally with the private APR_OPENINFO flag.
*
* With the APR_OPENINFO flag on NT, use the option flag
* FILE_FLAG_BACKUP_SEMANTICS to allow us to open directories.
* See the static resolve_ident() fn in file_io/win32/filestat.c
*/
if (!(flag & (APR_READ | APR_WRITE))) {
if (flag & APR_OPENINFO) {
if (apr_os_level >= APR_WIN_NT) {
attributes |= FILE_FLAG_BACKUP_SEMANTICS;
}
}
else {
return APR_EACCES;
}
if (flag & APR_READCONTROL)
oflags |= READ_CONTROL;
}
if (flag & APR_XTHREAD) {
/* This win32 specific feature is required
* to allow multiple threads to work with the file.
*/
attributes |= FILE_FLAG_OVERLAPPED;
}
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
apr_wchar_t wfname[APR_PATH_MAX];
if (flag & APR_SENDFILE_ENABLED) {
/* This feature is required to enable sendfile operations
* against the file on Win32. Also implies APR_XTHREAD.
*/
flag |= APR_XTHREAD;
attributes |= FILE_FLAG_SEQUENTIAL_SCAN | FILE_FLAG_OVERLAPPED;
}
if (rv = utf8_to_unicode_path(wfname, sizeof(wfname)
/ sizeof(apr_wchar_t), fname))
return rv;
handle = CreateFileW(wfname, oflags, sharemode,
NULL, createflags, attributes, 0);
}
#endif
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI {
handle = CreateFileA(fname, oflags, sharemode,
NULL, createflags, attributes, 0);
if (flag & APR_SENDFILE_ENABLED) {
/* This feature is not supported on this platform.
*/
flag &= ~APR_SENDFILE_ENABLED;
}
}
#endif
if (handle == INVALID_HANDLE_VALUE) {
return apr_get_os_error();
}
(*new) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
(*new)->pool = pool;
(*new)->filehand = handle;
(*new)->fname = apr_pstrdup(pool, fname);
(*new)->flags = flag;
(*new)->timeout = -1;
(*new)->ungetchar = -1;
if (flag & APR_APPEND) {
(*new)->append = 1;
SetFilePointer((*new)->filehand, 0, NULL, FILE_END);
}
if (flag & APR_BUFFERED) {
(*new)->buffered = 1;
(*new)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
}
/* Need the mutex to handled buffered and O_APPEND style file i/o */
if ((*new)->buffered || (*new)->append) {
rv = apr_thread_mutex_create(&(*new)->mutex,
APR_THREAD_MUTEX_DEFAULT, pool);
if (rv) {
if (file_cleanup(*new) == APR_SUCCESS) {
apr_pool_cleanup_kill(pool, *new, file_cleanup);
}
return rv;
}
}
if (!(flag & APR_FILE_NOCLEANUP)) {
apr_pool_cleanup_register((*new)->pool, (void *)(*new), file_cleanup,
apr_pool_cleanup_null);
}
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
{
apr_status_t stat;
if ((stat = file_cleanup(file)) == APR_SUCCESS) {
apr_pool_cleanup_kill(file->pool, file, file_cleanup);
if (file->mutex) {
apr_thread_mutex_destroy(file->mutex);
}
return APR_SUCCESS;
}
return stat;
}
APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
{
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
apr_wchar_t wpath[APR_PATH_MAX];
apr_status_t rv;
if (rv = utf8_to_unicode_path(wpath, sizeof(wpath)
/ sizeof(apr_wchar_t), path)) {
return rv;
}
if (DeleteFileW(wpath))
return APR_SUCCESS;
}
#endif
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
if (DeleteFile(path))
return APR_SUCCESS;
#endif
return apr_get_os_error();
}
APR_DECLARE(apr_status_t) apr_file_rename(const char *frompath,
const char *topath,
apr_pool_t *pool)
{
IF_WIN_OS_IS_UNICODE
{
#if APR_HAS_UNICODE_FS
apr_wchar_t wfrompath[APR_PATH_MAX], wtopath[APR_PATH_MAX];
apr_status_t rv;
if (rv = utf8_to_unicode_path(wfrompath, sizeof(wfrompath)
/ sizeof(apr_wchar_t), frompath)) {
return rv;
}
if (rv = utf8_to_unicode_path(wtopath, sizeof(wtopath)
/ sizeof(apr_wchar_t), topath)) {
return rv;
}
#ifndef _WIN32_WCE
if (MoveFileExW(wfrompath, wtopath, MOVEFILE_REPLACE_EXISTING |
MOVEFILE_COPY_ALLOWED))
#else
if (MoveFileW(wfrompath, wtopath))
#endif
return APR_SUCCESS;
#else
if (MoveFileEx(frompath, topath, MOVEFILE_REPLACE_EXISTING |
MOVEFILE_COPY_ALLOWED))
return APR_SUCCESS;
#endif
}
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
/* Windows 95 and 98 do not support MoveFileEx, so we'll use
* the old MoveFile function. However, MoveFile requires that
* the new file not already exist...so we have to delete that
* file if it does. Perhaps we should back up the to-be-deleted
* file in case something happens?
*/
HANDLE handle = INVALID_HANDLE_VALUE;
if ((handle = CreateFile(topath, GENERIC_WRITE, 0, 0,
OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE )
{
CloseHandle(handle);
if (!DeleteFile(topath))
return apr_get_os_error();
}
if (MoveFile(frompath, topath))
return APR_SUCCESS;
}
#endif
return apr_get_os_error();
}
APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile,
apr_file_t *file)
{
*thefile = file->filehand;
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file,
apr_os_file_t *thefile,
apr_int32_t flags,
apr_pool_t *pool)
{
(*file) = apr_pcalloc(pool, sizeof(apr_file_t));
(*file)->pool = pool;
(*file)->filehand = *thefile;
(*file)->ungetchar = -1; /* no char avail */
(*file)->timeout = -1;
(*file)->flags = flags;
if (flags & APR_APPEND) {
(*file)->append = 1;
}
if (flags & APR_BUFFERED) {
(*file)->buffered = 1;
(*file)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
}
if ((*file)->append || (*file)->buffered) {
apr_status_t rv;
rv = apr_thread_mutex_create(&(*file)->mutex,
APR_THREAD_MUTEX_DEFAULT, pool);
if (rv) {
if (file_cleanup(*file) == APR_SUCCESS) {
apr_pool_cleanup_kill(pool, *file, file_cleanup);
}
return rv;
}
}
/* XXX... we pcalloc above so all others are zeroed.
* Should we be testing if thefile is a handle to
* a PIPE and set up the mechanics appropriately?
*
* (*file)->pipe;
*/
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
{
if (fptr->eof_hit == 1) {
return APR_EOF;
}
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile, apr_pool_t *pool)
{
#ifdef _WIN32_WCE
return APR_ENOTIMPL;
#else
apr_os_file_t file_handle;
apr_set_os_error(APR_SUCCESS);
file_handle = GetStdHandle(STD_ERROR_HANDLE);
if (!file_handle)
file_handle = INVALID_HANDLE_VALUE;
return apr_os_file_put(thefile, &file_handle,
APR_WRITE | APR_STDERR_FLAG, pool);
#endif
}
APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile, apr_pool_t *pool)
{
#ifdef _WIN32_WCE
return APR_ENOTIMPL;
#else
apr_os_file_t file_handle;
apr_set_os_error(APR_SUCCESS);
file_handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (!file_handle)
file_handle = INVALID_HANDLE_VALUE;
return apr_os_file_put(thefile, &file_handle,
APR_WRITE | APR_STDOUT_FLAG, pool);
#endif
}
APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile, apr_pool_t *pool)
{
#ifdef _WIN32_WCE
return APR_ENOTIMPL;
#else
apr_os_file_t file_handle;
apr_set_os_error(APR_SUCCESS);
file_handle = GetStdHandle(STD_INPUT_HANDLE);
if (!file_handle)
file_handle = INVALID_HANDLE_VALUE;
return apr_os_file_put(thefile, &file_handle,
APR_READ | APR_STDIN_FLAG, pool);
#endif
}
APR_POOL_IMPLEMENT_ACCESSOR(file);
APR_IMPLEMENT_INHERIT_SET(file, flags, pool, file_cleanup)
APR_IMPLEMENT_INHERIT_UNSET(file, flags, pool, file_cleanup)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -