Cleanup: rename BLI_make_existing_file for clarity

Rename BLI_make_existing_file to BLI_file_ensure_parent_dir_exists.
The previous name read as if it would make (touch) the file,
where as it ensures the directory component of the path exists.

Move from BLI_path to BLI_fileops as path utilities should only
manipulate paths and not deal with file IO creation
(this has more in common with BLI_file_touch for e.g.).
This commit is contained in:
Campbell Barton
2023-05-03 11:49:47 +10:00
parent bb341eaf12
commit 1f96fa1129
24 changed files with 51 additions and 52 deletions

View File

@@ -3319,7 +3319,7 @@ void dynamicPaint_outputSurfaceImage(DynamicPaintSurface *surface,
/* Validate output file path */
BLI_path_abs(output_file, BKE_main_blendfile_path_from_global());
BLI_make_existing_file(output_file);
BLI_file_ensure_parent_dir_exists(output_file);
/* Init image buffer */
ibuf = IMB_allocImBuf(surface->image_resolution, surface->image_resolution, 32, IB_rectfloat);

View File

@@ -2554,7 +2554,7 @@ int BKE_imbuf_write(ImBuf *ibuf, const char *name, const ImageFormatData *imf)
{
BKE_image_format_to_imbuf(ibuf, imf);
BLI_make_existing_file(name);
BLI_file_ensure_parent_dir_exists(name);
const bool ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
if (ok == 0) {

View File

@@ -8,6 +8,7 @@
#include <cerrno>
#include <cstring>
#include "BLI_fileops.h"
#include "BLI_listbase.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
@@ -862,7 +863,7 @@ bool BKE_image_render_write_exr(ReportList *reports,
errno = 0;
BLI_make_existing_file(filepath);
BLI_file_ensure_parent_dir_exists(filepath);
int compress = (imf ? imf->exr_codec : 0);
bool success = IMB_exr_begin_write(

View File

@@ -1881,7 +1881,7 @@ static void movieclip_build_proxy_ibuf(
*/
BLI_thread_lock(LOCK_MOVIECLIP);
BLI_make_existing_file(filepath);
BLI_file_ensure_parent_dir_exists(filepath);
if (IMB_saveiff(scaleibuf, filepath, IB_rect) == 0) {
perror(filepath);
}

View File

@@ -314,8 +314,7 @@ int BKE_packedfile_write_to_file(ReportList *reports,
}
}
/* make sure the path to the file exists... */
BLI_make_existing_file(name);
BLI_file_ensure_parent_dir_exists(name);
file = BLI_open(name, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
if (file == -1) {

View File

@@ -1475,13 +1475,11 @@ static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
fp = BLI_fopen(filepath, "rb");
}
else if (mode == PTCACHE_FILE_WRITE) {
/* Will create the dir if needs be, same as "//textures" is created. */
BLI_make_existing_file(filepath);
BLI_file_ensure_parent_dir_exists(filepath);
fp = BLI_fopen(filepath, "wb");
}
else if (mode == PTCACHE_FILE_UPDATE) {
BLI_make_existing_file(filepath);
BLI_file_ensure_parent_dir_exists(filepath);
fp = BLI_fopen(filepath, "rb+");
}

View File

@@ -160,7 +160,7 @@ static void filepath_avi(char *string, const RenderData *rd, bool preview, const
strcpy(string, rd->pic);
BLI_path_abs(string, BKE_main_blendfile_path_from_global());
BLI_make_existing_file(string);
BLI_file_ensure_parent_dir_exists(string);
if (rd->scemode & R_EXTENSION) {
if (!BLI_path_extension_check(string, ".avi")) {

View File

@@ -1385,7 +1385,7 @@ static void ffmpeg_filepath_get(FFMpegContext *context,
BLI_strncpy(string, rd->pic, FILE_MAX);
BLI_path_abs(string, BKE_main_blendfile_path_from_global());
BLI_make_existing_file(string);
BLI_file_ensure_parent_dir_exists(string);
autosplit[0] = '\0';

View File

@@ -276,7 +276,14 @@ bool BLI_file_is_writable(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NON
* Creates the file with nothing in it, or updates its last-modified date if it already exists.
* Returns true if successful (like the unix touch command).
*/
bool BLI_file_touch(const char *file) ATTR_NONNULL();
bool BLI_file_touch(const char *file) ATTR_NONNULL(1);
/**
* Ensures that the parent directory of `filepath` exists.
*
* \return true on success (i.e. given path now exists on file-system), false otherwise.
*/
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1);
bool BLI_file_alias_target(const char *filepath, char *r_targetpath) ATTR_WARN_UNUSED_RESULT;
bool BLI_file_magic_is_gzip(const char header[4]);

View File

@@ -37,12 +37,6 @@ void BLI_setenv_if_new(const char *env, const char *val) ATTR_NONNULL(1);
*/
const char *BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT;
/**
* Ensures that the parent directory of `name` exists.
*
* \return true on success (i.e. given path now exists on file-system), false otherwise.
*/
bool BLI_make_existing_file(const char *name) ATTR_NONNULL(1);
/**
* Converts `/foo/bar.txt` to `/foo/` and `bar.txt`
*

View File

@@ -285,6 +285,15 @@ bool BLI_file_touch(const char *file)
return false;
}
bool BLI_file_ensure_parent_dir_exists(const char *filepath)
{
char di[FILE_MAX];
BLI_path_split_dir_part(filepath, di, sizeof(di));
/* Make if the dir doesn't exist. */
return BLI_dir_create_recursive(di);
}
#ifdef WIN32
static void callLocalErrorCallBack(const char *err)

View File

@@ -1292,15 +1292,6 @@ const char *BLI_getenv(const char *env)
#endif
}
bool BLI_make_existing_file(const char *name)
{
char di[FILE_MAX];
BLI_path_split_dir_part(name, di, sizeof(di));
/* Make if the dir doesn't exist. */
return BLI_dir_create_recursive(di);
}
static bool path_extension_check_ex(const char *str,
const size_t str_len,
const char *ext,

View File

@@ -458,7 +458,7 @@ void DebugInfo::export_operation(const NodeOperation *op, MemoryBuffer *render)
const std::string file_name = operation_class_name(op) + "_" + std::to_string(op->get_id()) +
".png";
const std::string path = get_operations_export_dir() + file_name;
BLI_make_existing_file(path.c_str());
BLI_file_ensure_parent_dir_exists(path.c_str());
IMB_saveiff(ibuf, path.c_str(), ibuf->flags);
IMB_freeImBuf(ibuf);
}

View File

@@ -3,6 +3,8 @@
#include "COM_OutputFileMultiViewOperation.h"
#include "BLI_fileops.h"
#include "BKE_image.h"
#include "BKE_image_format.h"
#include "BKE_main.h"
@@ -56,7 +58,7 @@ void *OutputOpenExrSingleLayerMultiViewOperation::get_handle(const char *filenam
add_exr_channels(exrhandle, nullptr, datatype_, srv->name, width, false, nullptr);
}
BLI_make_existing_file(filename);
BLI_file_ensure_parent_dir_exists(filename);
/* prepare the file with all the channels */
@@ -169,7 +171,7 @@ void *OutputOpenExrMultiLayerMultiViewOperation::get_handle(const char *filename
}
}
BLI_make_existing_file(filename);
BLI_file_ensure_parent_dir_exists(filename);
/* prepare the file with all the channels for the header */
StampData *stamp_data = create_stamp_data();

View File

@@ -3,8 +3,8 @@
#include "COM_OutputFileOperation.h"
#include "BLI_fileops.h"
#include "BLI_listbase.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BKE_image.h"
@@ -408,7 +408,7 @@ void OutputOpenExrMultiLayerOperation::deinit_execution()
(rd_->scemode & R_EXTENSION) != 0,
true,
suffix);
BLI_make_existing_file(filename);
BLI_file_ensure_parent_dir_exists(filename);
for (uint i = 0; i < layers_.size(); i++) {
OutputOpenExrLayer &layer = layers_[i];

View File

@@ -761,9 +761,7 @@ class AssetIndexFile : public AbstractFile {
bool ensure_parent_path_exists() const
{
/* `BLI_make_existing_file` only ensures parent path, otherwise than expected from the name of
* the function. */
return BLI_make_existing_file(get_file_path());
return BLI_file_ensure_parent_dir_exists(get_file_path());
}
void write_contents(AssetIndex &content)

View File

@@ -93,7 +93,7 @@ static int wm_collada_export_exec(bContext *C, wmOperator *op)
/* Avoid File write exceptions in Collada */
if (!BLI_exists(filepath)) {
BLI_make_existing_file(filepath);
BLI_file_ensure_parent_dir_exists(filepath);
if (!BLI_file_touch(filepath)) {
BKE_report(op->reports, RPT_ERROR, "Can't create export file");
fprintf(stdout, "Collada export: Can not create: %s\n", filepath);

View File

@@ -3187,7 +3187,7 @@ static int sequencer_export_subtitles_exec(bContext *C, wmOperator *op)
/* Avoid File write exceptions. */
if (!BLI_exists(filepath)) {
BLI_make_existing_file(filepath);
BLI_file_ensure_parent_dir_exists(filepath);
if (!BLI_file_touch(filepath)) {
BKE_report(op->reports, RPT_ERROR, "Can't create subtitle file");
return OPERATOR_CANCELLED;

View File

@@ -75,7 +75,7 @@ anim_index_builder *IMB_index_builder_create(const char *name)
BLI_strncpy(rv->temp_name, name, sizeof(rv->temp_name));
BLI_string_join(rv->temp_name, sizeof(rv->temp_name), name, temp_ext);
BLI_make_existing_file(rv->temp_name);
BLI_file_ensure_parent_dir_exists(rv->temp_name);
rv->fp = BLI_fopen(rv->temp_name, "wb");
@@ -497,7 +497,7 @@ static struct proxy_output_ctx *alloc_proxy_output_ffmpeg(
rv->anim = anim;
get_proxy_filepath(rv->anim, rv->proxy_size, filepath, true);
if (!BLI_make_existing_file(filepath)) {
if (!BLI_file_ensure_parent_dir_exists(filepath)) {
return nullptr;
}
@@ -1321,7 +1321,7 @@ static IndexBuildContext *index_fallback_create_context(struct anim *anim,
char filepath[FILE_MAX];
get_proxy_filepath(anim, proxy_sizes[i], filepath, true);
BLI_make_existing_file(filepath);
BLI_file_ensure_parent_dir_exists(filepath);
context->proxy_ctx[i] = alloc_proxy_output_avi(
anim, filepath, anim->x * proxy_fac[i], anim->y * proxy_fac[i], quality);

View File

@@ -72,8 +72,7 @@ void ImagesExporter::export_UV_Image(Image *image, bool use_copies)
BLI_path_join(export_path, sizeof(export_path), export_dir, export_file);
/* make dest directory if it doesn't exist */
BLI_make_existing_file(export_path);
BLI_file_ensure_parent_dir_exists(export_path);
}
if (is_generated || is_dirty || is_packed) {

View File

@@ -67,7 +67,7 @@ void path_reference_copy(const Set<std::pair<std::string, std::string>> &copy_se
if (0 == BLI_path_cmp_normalized(src, dst)) {
continue; /* Source and dest are the same. */
}
if (!BLI_make_existing_file(dst)) {
if (!BLI_file_ensure_parent_dir_exists(dst)) {
fprintf(stderr, "Can't make directory for '%s', not copying\n", dst);
continue;
}

View File

@@ -2249,7 +2249,7 @@ void RE_RenderAnim(Render *re,
if (rd.mode & R_TOUCH) {
if (!is_multiview_name) {
if (!BLI_exists(name)) {
BLI_make_existing_file(name); /* makes the dir if its not there */
BLI_file_ensure_parent_dir_exists(name);
BLI_file_touch(name);
}
}
@@ -2264,7 +2264,7 @@ void RE_RenderAnim(Render *re,
BKE_scene_multiview_filepath_get(srv, name, filepath);
if (!BLI_exists(filepath)) {
BLI_make_existing_file(filepath); /* makes the dir if its not there */
BLI_file_ensure_parent_dir_exists(filepath);
BLI_file_touch(filepath);
}
}

View File

@@ -336,7 +336,7 @@ static void seq_disk_cache_get_file_path(SeqDiskCache *disk_cache,
static void seq_disk_cache_create_version_file(char *path)
{
BLI_make_existing_file(path);
BLI_file_ensure_parent_dir_exists(path);
FILE *file = BLI_fopen(path, "w");
if (file) {
@@ -553,8 +553,9 @@ bool seq_disk_cache_write_file(SeqDiskCache *disk_cache, SeqCacheKey *key, ImBuf
char filepath[FILE_MAX];
seq_disk_cache_get_file_path(disk_cache, key, filepath, sizeof(filepath));
BLI_make_existing_file(filepath);
BLI_file_ensure_parent_dir_exists(filepath);
/* Touch the file. */
FILE *file = BLI_fopen(filepath, "rb+");
if (!file) {
file = BLI_fopen(filepath, "wb+");
@@ -568,8 +569,8 @@ bool seq_disk_cache_write_file(SeqDiskCache *disk_cache, SeqCacheKey *key, ImBuf
DiskCacheFile *cache_file = seq_disk_cache_get_file_entry_by_path(disk_cache, filepath);
DiskCacheHeader header;
memset(&header, 0, sizeof(header));
/* #BLI_make_existing_file() above may create an empty file. This is fine, don't attempt reading
* the header in that case. */
/* The file may be empty when touched (above).
* This is fine, don't attempt reading the header in that case. */
if (cache_file->fstat.st_size != 0 && !seq_disk_cache_read_header(file, &header)) {
fclose(file);
seq_disk_cache_delete_file(disk_cache, cache_file);
@@ -606,7 +607,7 @@ ImBuf *seq_disk_cache_read_file(SeqDiskCache *disk_cache, SeqCacheKey *key)
DiskCacheHeader header;
seq_disk_cache_get_file_path(disk_cache, key, filepath, sizeof(filepath));
BLI_make_existing_file(filepath);
BLI_file_ensure_parent_dir_exists(filepath);
FILE *file = BLI_fopen(filepath, "rb");
if (!file) {

View File

@@ -299,7 +299,7 @@ static void seq_proxy_build_frame(const SeqRenderData *context,
ibuf->planes = 24;
}
BLI_make_existing_file(name);
BLI_file_ensure_parent_dir_exists(name);
const bool ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
if (ok == false) {