Wireshark 5.1.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
file_util.h
Go to the documentation of this file.
1
10
11#ifndef __FILE_UTIL_H__
12#define __FILE_UTIL_H__
13
14#include <stdbool.h>
15
16#include "ws_symbol_export.h"
17
18#ifdef _WIN32
19#include <io.h> /* for _read(), _write(), etc. */
20#include <gmodule.h>
21#include <glib/gstdio.h> /* for g_open(), g_fopen(), etc. */
22#endif
23
24#include <fcntl.h> /* for open() */
25
26#ifdef HAVE_UNISTD_H
27#include <unistd.h> /* for read(), write(), close(), etc. */
28#endif
29
30#include <sys/stat.h> /* for stat() and struct stat */
31
32#include <stdio.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif /* __cplusplus */
37
38/* We set a larger IO Buffer size for the capture files */
39#define IO_BUF_SIZE (64 * 1024)
40
41/*
42 * Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
43 * define them either.)
44 *
45 * Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO.
46 */
47#ifndef S_ISREG
48#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
49#endif
50#ifndef S_IFIFO
51#define S_IFIFO _S_IFIFO
52#endif
53#ifndef S_ISFIFO
54#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
55#endif
56#ifndef S_ISDIR
57#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
58#endif
59
60#ifdef _WIN32
61
62/*
63 * The structure to pass to ws_stat64() and ws_fstat64().
64 */
65#define ws_statb64 struct _stat64
66
67/* Win32 (and Win64): we use UTF-8 for filenames and pathnames throughout
68 * the code, so file functions must convert filenames and pathnames from
69 * UTF-8 to UTF-16 as we use NT Unicode (Win9x - now unsupported - used
70 * locale-based encoding here). Microsoft's UN*X-style wrappers don't
71 * do that - they expect locale-based encodings - so we need our own
72 * wrappers. We enforce code page 65001 (UTF-8) as the active code page
73 * via an application manifest in our executables now, but someone could
74 * build a different application without a manifest and link against these
75 * libraries. (If we don't think that's a real problem, we could just map
76 * to the UCRT wrappers directly instead of the GLib wrappers.)
77 *
78 * File descriptors and FILE pointers belong to the C runtime that
79 * created them, so callers of the wrappers that return or take them
80 * must use the same C runtime as GLib. Our builds use the same
81 * one for libwsutil and GLib (the UCRT, or the debug UCRT in Debug
82 * builds), and we already pass file descriptors to GLib, e.g. with
83 * g_io_channel_win32_new_fd(). With CMake, a plugin that uses the
84 * imported targets and the default MSVC_RUNTIME_LIBRARY gets the
85 * matching C runtime; one built with the static C runtime (/MT) or
86 * against MINGW64's msvcrt.dll doesn't.
87 *
88 * We have our own ws_stdio_stat64() instead of g_stat(), which can't
89 * return the size of a file of 2 GB or more: with MSVC on 64-bit
90 * Windows, GStatBuf is struct stat, which has a 32-bit st_size.
91 *
92 * Note also that g_rename() uses MoveFileEx() with
93 * MOVEFILE_REPLACE_EXISTING, so that it acts like UN*X rename(),
94 * removing the target if necessary.
95 */
96
97WS_DLL_PUBLIC int ws_stdio_stat64 (const char *filename, ws_statb64 *buf);
98
99#define ws_open g_open
100#define ws_rename g_rename
101#define ws_mkdir g_mkdir
102#define ws_stat64 ws_stdio_stat64
103#define ws_unlink g_unlink
104#define ws_remove g_remove
105#define ws_fopen g_fopen
106#define ws_freopen g_freopen
107
108/*
109 * These routines don't take pathnames, so they don't require
110 * pathname-converting wrappers on Windows.
111 */
112
113typedef unsigned int ws_file_size_t;
114typedef signed int ws_file_ssize_t;
115
116#define ws_read _read
117#define ws_write _write
118#define ws_close _close
119#define ws_dup _dup
120#define ws_fseek64 _fseeki64 /* use _fseeki64 for 64-bit offset support */
121#define ws_fstat64 _fstati64 /* use _fstati64 for 64-bit size support */
122#define ws_ftell64 _ftelli64 /* use _ftelli64 for 64-bit offset support */
123#define ws_lseek64 _lseeki64 /* use _lseeki64 for 64-bit offset support */
124#define ws_fdopen _fdopen
125#define ws_fileno _fileno
126#define ws_isatty _isatty
127#define ws_getc_unlocked _fgetc_nolock
128
129/*
130 * Other CRT functions. getpid probably belongs in sys_util.h or proc_util.h
131 * but neither yet exist.
132 */
133#define ws_getpid _getpid
134#define ws_umask _umask
135
136/* DLL loading */
137
143WS_DLL_PUBLIC
144bool ws_init_dll_search_path(void);
145
152
153WS_DLL_PUBLIC
154void *ws_load_library(const char *library_name);
155
161WS_DLL_PUBLIC
162GModule *load_wpcap_module(void);
163
168WS_DLL_PUBLIC void create_app_running_mutex(void);
169
172WS_DLL_PUBLIC void close_app_running_mutex(void);
173
176WS_DLL_PUBLIC int ws_close_if_possible(int fd);
177
178#else /* _WIN32 */
179
180/*
181 * The structure to pass to ws_fstat64().
182 */
183#define ws_statb64 struct stat
184
185/* Not Windows, presumed to be UN*X-compatible */
186#define ws_open open
187#define ws_rename rename
188#define ws_mkdir(dir,mode) mkdir(dir,mode)
189#define ws_stat64 stat
190#define ws_unlink unlink
191#define ws_remove remove
192#define ws_fopen fopen
193#define ws_freopen freopen
194
195typedef size_t ws_file_size_t;
196typedef ssize_t ws_file_ssize_t;
197
198#define ws_read read
199#define ws_write write
200#ifdef __cplusplus
201/*
202 * Just in case this is used in a class with a close method or member.
203 */
204#define ws_close ::close
205#else
206#define ws_close close
207#endif
208
209#define ws_close_if_possible ws_close
210
211#define ws_dup dup
212#ifdef HAVE_FSEEKO
213#define ws_fseek64 fseeko /* AC_SYS_LARGEFILE should make off_t 64-bit */
214#define ws_ftell64 ftello /* AC_SYS_LARGEFILE should make off_t 64-bit */
215#else
216#define ws_fseek64(fh,offset,whence) fseek(fh,(long)(offset),whence)
217#define ws_ftell64 ftell
218#endif
219#define ws_fstat64 fstat /* AC_SYS_LARGEFILE should make off_t 64-bit */
220#define ws_lseek64 lseek /* AC_SYS_LARGEFILE should make off_t 64-bit */
221#define ws_fdopen fdopen
222#define ws_fileno fileno
223#define ws_isatty isatty
224#define ws_getc_unlocked getc_unlocked
225#define O_BINARY 0 /* Win32 needs the O_BINARY flag for open() */
226
227/* Other CRT functions */
228#define ws_getpid getpid
229#define ws_umask umask
230
231#endif /* _WIN32 */
232
233/* directory handling */
234#define WS_DIR GDir
235#define WS_DIRENT const char
236#define ws_dir_open g_dir_open
237#define ws_dir_read_name g_dir_read_name
238#define ws_dir_get_name(dirent) dirent
239#define ws_dir_rewind g_dir_rewind
240#define ws_dir_close g_dir_close
241
242/* XXX - remove include "sys/stat.h" from files that include this header */
243/* XXX - update docs (e.g. README.developer) */
244
245/* Error codes from file handling routines */
246#define FILE_ERR_CANT_OPEN -1
247#define FILE_ERR_CANT_COMPRESS -2
248#define FILE_ERR_SHORT_WRITE -3
249#define FILE_ERR_CANT_CLOSE -4
250#define FILE_ERR_INTERNAL -5
251
252#ifdef __cplusplus
253}
254#endif /* __cplusplus */
255
256#endif /* __FILE_UTIL_H__ */