Added simple wrappers for fopen(3) and fclose(3).

Add simple wrapper functions for fopen(3) and fclose(3) that are
somewhat akin to the nxt_file_open() and nxt_file_close() wrappers that
log errors.

Suggested-by: Alejandro Colomar <alx@nginx.com>
Reviewed-by: Alejandro Colomar <alx@nginx.com>
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
This commit is contained in:
Andrew Clayton
2022-12-01 01:39:57 +00:00
parent 55b9a5307d
commit 9466daf9bd
2 changed files with 41 additions and 0 deletions

View File

@@ -500,6 +500,43 @@ nxt_fd_close(nxt_fd_t fd)
} }
FILE *
nxt_file_fopen(nxt_task_t *task, const char *pathname, const char *mode)
{
int err;
FILE *fp;
#if (NXT_DEBUG)
nxt_thread_time_update(task->thread);
#endif
fp = fopen(pathname, mode);
err = (fp == NULL) ? nxt_errno : 0;
nxt_debug(task, "fopen(\"%s\", \"%s\"): fp:%p err:%d", pathname, mode, fp,
err);
if (nxt_fast_path(fp != NULL)) {
return fp;
}
nxt_alert(task, "fopen(\"%s\") failed %E", pathname, err);
return NULL;
}
void
nxt_file_fclose(nxt_task_t *task, FILE *fp)
{
nxt_debug(task, "fclose(%p)", fp);
if (nxt_slow_path(fclose(fp) == -1)) {
nxt_alert(task, "fclose() failed %E", nxt_errno);
}
}
/* /*
* nxt_file_redirect() redirects the file to the fd descriptor. * nxt_file_redirect() redirects the file to the fd descriptor.
* Then the fd descriptor is closed. * Then the fd descriptor is closed.

View File

@@ -186,6 +186,10 @@ NXT_EXPORT ssize_t nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size);
NXT_EXPORT ssize_t nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size); NXT_EXPORT ssize_t nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size);
NXT_EXPORT void nxt_fd_close(nxt_fd_t fd); NXT_EXPORT void nxt_fd_close(nxt_fd_t fd);
NXT_EXPORT FILE *nxt_file_fopen(nxt_task_t *task, const char *pathname,
const char *mode);
NXT_EXPORT void nxt_file_fclose(nxt_task_t *task, FILE *fp);
NXT_EXPORT nxt_int_t nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd); NXT_EXPORT nxt_int_t nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd);
NXT_EXPORT nxt_int_t nxt_file_stderr(nxt_file_t *file); NXT_EXPORT nxt_int_t nxt_file_stderr(nxt_file_t *file);
NXT_EXPORT nxt_int_t nxt_stderr_start(void); NXT_EXPORT nxt_int_t nxt_stderr_start(void);