BRL-CAD
file.h
Go to the documentation of this file.
1/* F I L E . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20
21#ifndef BU_FILE_H
22#define BU_FILE_H
23
24#include "common.h"
25
26#include <stdio.h> /* for FILE */
27#include <stddef.h> /* for size_t */
28#include <stdlib.h> /* for getenv */
29
30#include "bu/defines.h"
31#include "bu/magic.h"
32#include "bu/vls.h"
33
34
35__BEGIN_DECLS
36
37
38/** @addtogroup bu_file
39 *
40 * @brief Routines for handling, inspecting, and comparing files and
41 * directories.
42 *
43 * These routines involve filesystem interaction such as reporting
44 * whether a file exists or is of a particular type.
45 *
46 */
47/** @{ */
48/** @file bu/file.h */
49
50/**
51 * Returns truthfully whether the given file path exists or not. An
52 * empty or NULL path name is treated as a non-existent file and, as
53 * such, will return false. If fd is non-NULL, it will be set to a
54 * read-only open file descriptor for the provided path. If path is
55 * "-", fd is set to standard input's file descriptor if provided.
56 *
57 * @return >0 The given filename exists.
58 * @return 0 The given filename does not exist.
59 */
60BU_EXPORT extern int bu_file_exists(const char *path, int *fd);
61
62/**
63 * Returns the size of the given file, or -1 if the size cannot
64 * be determined.
65 *
66 * @return >=0 The given filename exists.
67 * @return -1 The given filename does not exist or there was an error.
68 */
69BU_EXPORT extern int bu_file_size(const char *path);
70
71/**
72 * Returns truthfully as to whether the two provided filenames are the
73 * same file. If either file does not exist, the result is false. If
74 * either filename is empty or NULL, it is treated as non-existent
75 * and, as such, will also return false.
76 */
77BU_EXPORT extern int bu_file_same(const char *fn1, const char *fn2);
78
79/**
80 * returns truthfully if current user can read the specified file or
81 * directory.
82 */
83BU_EXPORT extern int bu_file_readable(const char *path);
84
85/**
86 * returns truthfully if current user can write to the specified file
87 * or directory.
88 */
89BU_EXPORT extern int bu_file_writable(const char *path);
90
91/**
92 * returns truthfully if current user can run the specified file or
93 * directory.
94 */
95BU_EXPORT extern int bu_file_executable(const char *path);
96
97/**
98 * Returns truthfully whether the given file path is a directory. An
99 * empty or NULL path name is treated as a non-existent directory and,
100 * as such, will return false.
101 *
102 * @return >0 The given filename is a directory
103 * @return 0 The given filename is not a directory.
104 */
105BU_EXPORT extern int bu_file_directory(const char *path);
106
107/**
108 * Returns truthfully whether the given file path is a symbolic link.
109 * An empty or NULL path name is treated as a non-existent link and,
110 * as such, will return false.
111 *
112 * @return >0 The given filename is a symbolic link
113 * @return 0 The given filename is not a symbolic link.
114 */
115BU_EXPORT extern int bu_file_symbolic(const char *path);
116
117/**
118 * forcibly attempts to delete a specified file. if the file can be
119 * deleted by relaxing file permissions, they will be changed in order
120 * to delete the file.
121 *
122 * returns truthfully if the specified file was deleted.
123 */
124BU_EXPORT extern int bu_file_delete(const char *path);
125
126
127/**
128 * Get a listing of files in a directory, optionally matching a pattern.
129 *
130 * If the caller provides a pointer to an argv-style 'files' array,
131 * this function will dynamically allocate an array of strings, filled
132 * with the sorted listing of matching file(s). It is the caller's
133 * responsibility to free a non-NULL array with bu_argv_free().
134 *
135
136 * If '*files' is NULL, the caller is expected to free the matches
137 * array with bu_argv_free(). If '*files' is non-NULL (i.e., array of
138 * string pointers is already allocated or on the stack), the caller
139 * is expected to ensure adequate entries are preallocated and to free
140 * all strings with bu_argv_free() or as otherwise necessary.
141 *
142 * Example:
143 @code
144 // This allocates an array for storing matches, filling in the
145 // array with all directory paths starting with 'a' through 'e' and
146 // ending with a '.c' in the src/libbu directory.
147
148 char **my_matches = NULL;
149 size_t count = bu_file_list("src/libbu", "[a-e]*.c", &my_matches);
150 @endcode
151 *
152 * @return the number of directory entries matching the provided
153 * pattern.
154 */
155BU_EXPORT extern size_t bu_file_list(const char *path, const char *pattern, char ***files);
156
157
158/**
159 * This routine expands a path to a resolved canonical full path.
160 *
161 * Returns a pointer to the canonical path. If resolved_path is NULL,
162 * caller is responsible for freeing the returned path via bu_free.
163 * If supplying a non-NULL resolved_path buffer, it must hold at least
164 * MAXPATHLEN characters.
165 */
166BU_EXPORT extern char *bu_file_realpath(const char *path, char *resolved_path);
167
168/**
169 * Windows corecrt_io.h defines an lseek and an _lseeki64, with different function
170 * signatures, that cause trouble when we try to simply define it à la:
171 *
172 * #define lseek _lseeki64.
173 *
174 * Similarly, _ftelli64 has a problematic signature.
175 */
176BU_EXPORT int bu_fseek(FILE *stream, b_off_t offset, int origin);
177BU_EXPORT b_off_t bu_lseek(int fd, b_off_t offset, int whence);
178BU_EXPORT b_off_t bu_ftell(FILE *stream);
179
180/** @} */
181
182__END_DECLS
183
184#endif /* BU_FILE_H */
185
186/*
187 * Local Variables:
188 * mode: C
189 * tab-width: 8
190 * indent-tabs-mode: t
191 * c-file-style: "stroustrup"
192 * End:
193 * ex: shiftwidth=4 tabstop=8
194 */
Header file for the BRL-CAD common definitions.
int bu_file_symbolic(const char *path)
int bu_file_size(const char *path)
b_off_t bu_ftell(FILE *stream)
int bu_file_executable(const char *path)
int bu_file_directory(const char *path)
int bu_file_same(const char *fn1, const char *fn2)
int bu_file_readable(const char *path)
size_t bu_file_list(const char *path, const char *pattern, char ***files)
b_off_t bu_lseek(int fd, b_off_t offset, int whence)
char * bu_file_realpath(const char *path, char *resolved_path)
int bu_fseek(FILE *stream, b_off_t offset, int origin)
int bu_file_delete(const char *path)
int bu_file_writable(const char *path)
int bu_file_exists(const char *path, int *fd)
#define b_off_t
Definition: common.h:222
Global registry of recognized magic numbers.