| 1 |
#ifndef RBIMPL_INTERN_FILE_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_FILE_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Public APIs related to ::rb_cFile. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/attr/nonnull.h" |
| 24 |
#include "ruby/internal/attr/pure.h" |
| 25 |
#include "ruby/internal/dllexport.h" |
| 26 |
#include "ruby/internal/value.h" |
| 27 |
|
| 28 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 29 |
|
| 30 |
/* file.c */ |
| 31 |
|
| 32 |
RBIMPL_ATTR_NONNULL(()) |
| 33 |
/** |
| 34 |
* Identical to rb_file_expand_path(), except how arguments are passed. |
| 35 |
* |
| 36 |
* @param[in] argc Number of objects of `argv`. |
| 37 |
* @param[in] argv Filename, and base directory, in that order. |
| 38 |
* @exception rb_eArgError Wrong `argc`. |
| 39 |
* @exception rb_eTypeError Non-string passed. |
| 40 |
* @exception rb_eEncCompatError No conversion from arguments to a path. |
| 41 |
* @return Expanded path. |
| 42 |
* |
| 43 |
* @internal |
| 44 |
* |
| 45 |
* It seems nobody actually uses this function right now. Maybe delete it? |
| 46 |
*/ |
| 47 |
VALUE rb_file_s_expand_path(int argc, const VALUE *argv); |
| 48 |
|
| 49 |
/** |
| 50 |
* Identical to rb_file_absolute_path(), except it additionally understands |
| 51 |
* `~`. If a given pathname starts with `~someone/`, that part expands to the |
| 52 |
* user's home directory (or that of current process' owner's in case of `~/`). |
| 53 |
* |
| 54 |
* @param[in] fname Relative file name. |
| 55 |
* @param[in] dname Lookup base directory name, or in case |
| 56 |
* ::RUBY_Qnil is passed the process' current |
| 57 |
* working directory is assumed. |
| 58 |
* @exception rb_eArgError Home directory is not absolute. |
| 59 |
* @exception rb_eTypeError Non-string passed. |
| 60 |
* @exception rb_eEncCompatError No conversion from arguments to a path. |
| 61 |
* @return Expanded path. |
| 62 |
*/ |
| 63 |
VALUE rb_file_expand_path(VALUE fname, VALUE dname); |
| 64 |
|
| 65 |
RBIMPL_ATTR_NONNULL(()) |
| 66 |
/** |
| 67 |
* Identical to rb_file_absolute_path(), except how arguments are passed. |
| 68 |
* |
| 69 |
* @param[in] argc Number of objects of `argv`. |
| 70 |
* @param[in] argv Filename, and base directory, in that order. |
| 71 |
* @exception rb_eArgError Wrong `argc`. |
| 72 |
* @exception rb_eTypeError Non-string passed. |
| 73 |
* @exception rb_eEncCompatError No conversion from arguments to a path. |
| 74 |
* @return Expanded path. |
| 75 |
* |
| 76 |
* @internal |
| 77 |
* |
| 78 |
* It seems nobody actually uses this function right now. Maybe delete it? |
| 79 |
*/ |
| 80 |
VALUE rb_file_s_absolute_path(int argc, const VALUE *argv); |
| 81 |
|
| 82 |
/** |
| 83 |
* Maps a relative path to its absolute representation. Relative paths are |
| 84 |
* referenced from the passed directory name, or from the process' current |
| 85 |
* working directory in case ::RUBY_Qnil is passed. |
| 86 |
* |
| 87 |
* @param[in] fname Relative file name. |
| 88 |
* @param[in] dname Lookup base directory name, or in case |
| 89 |
* ::RUBY_Qnil is passed the process' current |
| 90 |
* working directory is assumed. |
| 91 |
* @exception rb_eArgError Strings contain NUL bytes. |
| 92 |
* @exception rb_eTypeError Non-string passed. |
| 93 |
* @exception rb_eEncCompatError No conversion from arguments to a path. |
| 94 |
* @return Expanded path. |
| 95 |
*/ |
| 96 |
VALUE rb_file_absolute_path(VALUE fname, VALUE dname); |
| 97 |
|
| 98 |
/** |
| 99 |
* Strips a file path's last component (and trailing separators if any). This |
| 100 |
* function is relatively simple on POSIX environments; just splits the input |
| 101 |
* with `/`, strips the last one, if something remains joins them again, |
| 102 |
* otherwise the return value is `"."`. However when it comes to Windows this |
| 103 |
* function is quite very much complicated. We have to take UNC etc. into |
| 104 |
* account. So for instance `"C:foo"`'s dirname is `"C:."`. |
| 105 |
* |
| 106 |
* @param[in] fname File name to strip. |
| 107 |
* @exception rb_eTypeError `fname` is not a String. |
| 108 |
* @exception rb_eArgError `fname` contains NUL bytes. |
| 109 |
* @exception rb_eEncCompatError `fname`'s encoding is not path-compat. |
| 110 |
* @return A dirname of `fname`. |
| 111 |
* @note This is a "pure" operation; it computes the return value solely |
| 112 |
* from the passed object and never does any file IO. |
| 113 |
*/ |
| 114 |
VALUE rb_file_dirname(VALUE fname); |
| 115 |
|
| 116 |
RBIMPL_ATTR_NONNULL(()) |
| 117 |
/** |
| 118 |
* Resolves a feature's path. This function takes for instance `"json"` and |
| 119 |
* `[".so", ".rb"]`, and iterates over the `$LOAD_PATH` to see if there is |
| 120 |
* either `json.so` or `json.rb` in the directory. |
| 121 |
* |
| 122 |
* This is not what everything `require` does, but at least `require` is built |
| 123 |
* on top of it. |
| 124 |
* |
| 125 |
* @param[in,out] feature File to search, and return buffer. |
| 126 |
* @param[in] exts List of file extensions. |
| 127 |
* @exception rb_eTypeError `feature` is not a String. |
| 128 |
* @exception rb_eArgError `feature` contains NUL bytes. |
| 129 |
* @exception rb_eEncCompatError `feature`'s encoding is not path-compat. |
| 130 |
* @retval 0 Not found |
| 131 |
* @retval otherwise Found index in `ext`, plus one. |
| 132 |
* @post `*feature` is a resolved path. |
| 133 |
*/ |
| 134 |
int rb_find_file_ext(VALUE *feature, const char *const *exts); |
| 135 |
|
| 136 |
/** |
| 137 |
* Identical to rb_find_file_ext(), except it takes a feature name and is |
| 138 |
* extension at once, e.g. `"json.rb"`. This difference is much like how |
| 139 |
* `require` and `load` are different. |
| 140 |
* |
| 141 |
* @param[in] path A path relative to `$LOAD_PATH`. |
| 142 |
* @exception rb_eTypeError `path` is not a String. |
| 143 |
* @exception rb_eArgError `path` contains NUL bytes. |
| 144 |
* @exception rb_eEncCompatError `path`'s encoding is not path-compat. |
| 145 |
* @return Expanded path. |
| 146 |
*/ |
| 147 |
VALUE rb_find_file(VALUE path); |
| 148 |
|
| 149 |
/** |
| 150 |
* Queries if the given path is either a directory, or a symlink that |
| 151 |
* (potentially recursively) points to such thing. |
| 152 |
* |
| 153 |
* @param[in] _ Ignored (why...?) |
| 154 |
* @param[in] path String, or IO. In case of IO it issues |
| 155 |
* `fstat(2)` instead of `stat(2)`. |
| 156 |
* @exception rb_eFrozenError `path` is a frozen IO (why...?) |
| 157 |
* @exception rb_eTypeError `path` is neither String nor IO. |
| 158 |
* @exception rb_eArgError `path` contains NUL bytes. |
| 159 |
* @exception rb_eEncCompatError `path`'s encoding is not path-compat. |
| 160 |
* @retval RUBY_Qtrue `path` is a directory. |
| 161 |
* @retval RUBY_Qfalse Otherwise. |
| 162 |
*/ |
| 163 |
VALUE rb_file_directory_p(VALUE _, VALUE path); |
| 164 |
|
| 165 |
/** |
| 166 |
* Converts a string into an "OS Path" encoding, if any. In most operating |
| 167 |
* systems there are no such things like per-OS default encoding of filename. |
| 168 |
* For them this function is no-op. However most notably on MacOS, pathnames |
| 169 |
* are UTF-8 encoded. It converts the given string into such encoding. |
| 170 |
* |
| 171 |
* @param[in] path An instance of ::rb_cString. |
| 172 |
* @exception rb_eEncCompatError `path`'s encoding is not path-compat. |
| 173 |
* @return `path`'s contents converted to the OS' path encoding. |
| 174 |
*/ |
| 175 |
VALUE rb_str_encode_ospath(VALUE path); |
| 176 |
|
| 177 |
RBIMPL_ATTR_NONNULL(()) |
| 178 |
RBIMPL_ATTR_PURE() |
| 179 |
/** |
| 180 |
* Queries if the given path is an absolute path. On POSIX environments it is |
| 181 |
* as easy as `path[0] == '/'`. However on Windows, drive letters and UNC |
| 182 |
* paths are also taken into account. |
| 183 |
* |
| 184 |
* @param[in] path A possibly relative path string. |
| 185 |
* @retval 1 `path` is absolute. |
| 186 |
* @retval 0 `path` is relative. |
| 187 |
*/ |
| 188 |
int rb_is_absolute_path(const char *path); |
| 189 |
|
| 190 |
/** |
| 191 |
* Queries the file size of the given file. Because this function calls |
| 192 |
* `fstat(2)` internally, it is a failure to pass a closed file to this |
| 193 |
* function. |
| 194 |
* |
| 195 |
* This function flushes the passed file's buffer if any. Can take time. |
| 196 |
* |
| 197 |
* @param[in] file A file object. |
| 198 |
* @exception rb_eFrozenError `file` is frozen. |
| 199 |
* @exception rb_eIOError `file` is closed. |
| 200 |
* @exception rb_eSystemCallError Permission denied etc. |
| 201 |
* @exception rb_eNoMethodError The given non-file object doesn't respond |
| 202 |
* to `#size`. |
| 203 |
* @return The size of the passed file. |
| 204 |
* @note Passing a non-regular file such as a UNIX domain socket to this |
| 205 |
* function is not a failure. But the return value is |
| 206 |
* unpredictable. POSIX's `<sys/stat.h>` states that "the use of |
| 207 |
* this field is unspecified" then. |
| 208 |
*/ |
| 209 |
rb_off_t rb_file_size(VALUE file); |
| 210 |
|
| 211 |
RBIMPL_SYMBOL_EXPORT_END() |
| 212 |
|
| 213 |
#endif /* RBIMPL_INTERN_FILE_H */ |