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-2024 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  */
60 BU_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  */
69 BU_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  */
77 BU_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  */
83 BU_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  */
89 BU_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  */
95 BU_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  */
105 BU_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  */
115 BU_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  */
124 BU_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  * If '*files' is NULL, the caller is expected to free the matches
136  * array with bu_argv_free(). If '*files' is non-NULL (i.e., array of
137  * string pointers is already allocated or on the stack), the caller
138  * is expected to ensure adequate entries are preallocated and to free
139  * all strings with bu_argv_free() or as otherwise necessary.
140  *
141  * Example:
142  @code
143  // This allocates an array for storing matches, filling in the
144  // array with all directory paths starting with 'a' through 'e' and
145  // ending with a '.c' in the src/libbu directory.
146 
147  char **my_matches = NULL;
148  size_t count = bu_file_list("src/libbu", "[a-e]*.c", &my_matches);
149  @endcode
150  *
151  * @return the number of directory entries matching the provided
152  * pattern.
153  */
154 BU_EXPORT extern size_t bu_file_list(const char *path, const char *pattern, char ***files);
155 
156 
157 /**
158  * This routine expands a path to a resolved canonical full path.
159  *
160  * Returns a pointer to the canonical path. If resolved_path is NULL,
161  * caller is responsible for freeing the returned path via bu_free.
162  * If supplying a non-NULL resolved_path buffer, it must hold at least
163  * MAXPATHLEN characters.
164  */
165 BU_EXPORT extern char *bu_file_realpath(const char *path, char *resolved_path);
166 
167 /**
168  * Windows corecrt_io.h defines an lseek and an _lseeki64, with different function
169  * signatures, that cause trouble when we try to simply define it à la:
170  *
171  * #define lseek _lseeki64.
172  *
173  * Similarly, _ftelli64 has a problematic signature.
174  */
175 BU_EXPORT int bu_fseek(FILE *stream, b_off_t offset, int origin);
176 BU_EXPORT b_off_t bu_lseek(int fd, b_off_t offset, int whence);
177 BU_EXPORT b_off_t bu_ftell(FILE *stream);
178 
179 /** @} */
180 
181 __END_DECLS
182 
183 #endif /* BU_FILE_H */
184 
185 /*
186  * Local Variables:
187  * mode: C
188  * tab-width: 8
189  * indent-tabs-mode: t
190  * c-file-style: "stroustrup"
191  * End:
192  * ex: shiftwidth=4 tabstop=8
193  */
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)
char * bu_file_realpath(const char *path, char *resolved_path)
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)
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:223
Global registry of recognized magic numbers.