From 1f96fa11299e0cfa79bdba30cded1a831f0d5965 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 3 May 2023 11:49:47 +1000 Subject: [PATCH] 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.). --- source/blender/blenkernel/intern/dynamicpaint.cc | 2 +- source/blender/blenkernel/intern/image.cc | 2 +- source/blender/blenkernel/intern/image_save.cc | 3 ++- source/blender/blenkernel/intern/movieclip.c | 2 +- source/blender/blenkernel/intern/packedFile.c | 3 +-- source/blender/blenkernel/intern/pointcache.c | 6 ++---- source/blender/blenkernel/intern/writeavi.c | 2 +- source/blender/blenkernel/intern/writeffmpeg.c | 2 +- source/blender/blenlib/BLI_fileops.h | 9 ++++++++- source/blender/blenlib/BLI_path_util.h | 6 ------ source/blender/blenlib/intern/fileops.c | 9 +++++++++ source/blender/blenlib/intern/path_util.c | 9 --------- source/blender/compositor/intern/COM_Debug.cc | 2 +- .../operations/COM_OutputFileMultiViewOperation.cc | 6 ++++-- .../compositor/operations/COM_OutputFileOperation.cc | 4 ++-- source/blender/editors/asset/intern/asset_indexer.cc | 4 +--- source/blender/editors/io/io_collada.c | 2 +- .../blender/editors/space_sequencer/sequencer_edit.c | 2 +- source/blender/imbuf/intern/indexer.cc | 6 +++--- source/blender/io/collada/ImageExporter.cpp | 3 +-- source/blender/io/common/intern/path_util.cc | 2 +- source/blender/render/intern/pipeline.cc | 4 ++-- source/blender/sequencer/intern/disk_cache.c | 11 ++++++----- source/blender/sequencer/intern/proxy.c | 2 +- 24 files changed, 51 insertions(+), 52 deletions(-) diff --git a/source/blender/blenkernel/intern/dynamicpaint.cc b/source/blender/blenkernel/intern/dynamicpaint.cc index 5a5f95d0d07..05063ef6b6d 100644 --- a/source/blender/blenkernel/intern/dynamicpaint.cc +++ b/source/blender/blenkernel/intern/dynamicpaint.cc @@ -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); diff --git a/source/blender/blenkernel/intern/image.cc b/source/blender/blenkernel/intern/image.cc index 675cde9d7ec..bc1f9d08fe1 100644 --- a/source/blender/blenkernel/intern/image.cc +++ b/source/blender/blenkernel/intern/image.cc @@ -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) { diff --git a/source/blender/blenkernel/intern/image_save.cc b/source/blender/blenkernel/intern/image_save.cc index c9e870bbecd..69dad6147d2 100644 --- a/source/blender/blenkernel/intern/image_save.cc +++ b/source/blender/blenkernel/intern/image_save.cc @@ -8,6 +8,7 @@ #include #include +#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( diff --git a/source/blender/blenkernel/intern/movieclip.c b/source/blender/blenkernel/intern/movieclip.c index ded061e4de5..81e7d398e56 100644 --- a/source/blender/blenkernel/intern/movieclip.c +++ b/source/blender/blenkernel/intern/movieclip.c @@ -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); } diff --git a/source/blender/blenkernel/intern/packedFile.c b/source/blender/blenkernel/intern/packedFile.c index 64039ecac07..fc46635cbb4 100644 --- a/source/blender/blenkernel/intern/packedFile.c +++ b/source/blender/blenkernel/intern/packedFile.c @@ -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) { diff --git a/source/blender/blenkernel/intern/pointcache.c b/source/blender/blenkernel/intern/pointcache.c index e986ecfb3a6..35d6ecbc4ca 100644 --- a/source/blender/blenkernel/intern/pointcache.c +++ b/source/blender/blenkernel/intern/pointcache.c @@ -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+"); } diff --git a/source/blender/blenkernel/intern/writeavi.c b/source/blender/blenkernel/intern/writeavi.c index accff94a3b5..76f0cb99957 100644 --- a/source/blender/blenkernel/intern/writeavi.c +++ b/source/blender/blenkernel/intern/writeavi.c @@ -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")) { diff --git a/source/blender/blenkernel/intern/writeffmpeg.c b/source/blender/blenkernel/intern/writeffmpeg.c index 7c6eb4f5c00..42bcfe10062 100644 --- a/source/blender/blenkernel/intern/writeffmpeg.c +++ b/source/blender/blenkernel/intern/writeffmpeg.c @@ -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'; diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h index a4f1eef647c..2aa27973fb7 100644 --- a/source/blender/blenlib/BLI_fileops.h +++ b/source/blender/blenlib/BLI_fileops.h @@ -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]); diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h index ba0c327de57..bdc75054f92 100644 --- a/source/blender/blenlib/BLI_path_util.h +++ b/source/blender/blenlib/BLI_path_util.h @@ -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` * diff --git a/source/blender/blenlib/intern/fileops.c b/source/blender/blenlib/intern/fileops.c index bb760437c26..e196d50813e 100644 --- a/source/blender/blenlib/intern/fileops.c +++ b/source/blender/blenlib/intern/fileops.c @@ -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) diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 038d91b5946..17bb5bc2aef 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -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, diff --git a/source/blender/compositor/intern/COM_Debug.cc b/source/blender/compositor/intern/COM_Debug.cc index a670af5eaca..da2bef224a4 100644 --- a/source/blender/compositor/intern/COM_Debug.cc +++ b/source/blender/compositor/intern/COM_Debug.cc @@ -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); } diff --git a/source/blender/compositor/operations/COM_OutputFileMultiViewOperation.cc b/source/blender/compositor/operations/COM_OutputFileMultiViewOperation.cc index 7650746e3ed..3cb8f6d99b8 100644 --- a/source/blender/compositor/operations/COM_OutputFileMultiViewOperation.cc +++ b/source/blender/compositor/operations/COM_OutputFileMultiViewOperation.cc @@ -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(); diff --git a/source/blender/compositor/operations/COM_OutputFileOperation.cc b/source/blender/compositor/operations/COM_OutputFileOperation.cc index c0ad745613f..d2c6cb13ade 100644 --- a/source/blender/compositor/operations/COM_OutputFileOperation.cc +++ b/source/blender/compositor/operations/COM_OutputFileOperation.cc @@ -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]; diff --git a/source/blender/editors/asset/intern/asset_indexer.cc b/source/blender/editors/asset/intern/asset_indexer.cc index b6da39dcf3a..253b099d277 100644 --- a/source/blender/editors/asset/intern/asset_indexer.cc +++ b/source/blender/editors/asset/intern/asset_indexer.cc @@ -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) diff --git a/source/blender/editors/io/io_collada.c b/source/blender/editors/io/io_collada.c index 5de69694616..e12bd6c52a3 100644 --- a/source/blender/editors/io/io_collada.c +++ b/source/blender/editors/io/io_collada.c @@ -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); diff --git a/source/blender/editors/space_sequencer/sequencer_edit.c b/source/blender/editors/space_sequencer/sequencer_edit.c index 02f9b7a3e67..db05e572911 100644 --- a/source/blender/editors/space_sequencer/sequencer_edit.c +++ b/source/blender/editors/space_sequencer/sequencer_edit.c @@ -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; diff --git a/source/blender/imbuf/intern/indexer.cc b/source/blender/imbuf/intern/indexer.cc index a1fdea5d967..248c82a450a 100644 --- a/source/blender/imbuf/intern/indexer.cc +++ b/source/blender/imbuf/intern/indexer.cc @@ -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); diff --git a/source/blender/io/collada/ImageExporter.cpp b/source/blender/io/collada/ImageExporter.cpp index 49b5193d135..0062dbac995 100644 --- a/source/blender/io/collada/ImageExporter.cpp +++ b/source/blender/io/collada/ImageExporter.cpp @@ -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) { diff --git a/source/blender/io/common/intern/path_util.cc b/source/blender/io/common/intern/path_util.cc index 337923dc837..5ca6d2b1979 100644 --- a/source/blender/io/common/intern/path_util.cc +++ b/source/blender/io/common/intern/path_util.cc @@ -67,7 +67,7 @@ void path_reference_copy(const Set> ©_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; } diff --git a/source/blender/render/intern/pipeline.cc b/source/blender/render/intern/pipeline.cc index 0f577eab665..7f4fcc05a05 100644 --- a/source/blender/render/intern/pipeline.cc +++ b/source/blender/render/intern/pipeline.cc @@ -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); } } diff --git a/source/blender/sequencer/intern/disk_cache.c b/source/blender/sequencer/intern/disk_cache.c index 00fba9f8879..4f17d3419e6 100644 --- a/source/blender/sequencer/intern/disk_cache.c +++ b/source/blender/sequencer/intern/disk_cache.c @@ -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) { diff --git a/source/blender/sequencer/intern/proxy.c b/source/blender/sequencer/intern/proxy.c index bb415d14dea..1e21d53dad8 100644 --- a/source/blender/sequencer/intern/proxy.c +++ b/source/blender/sequencer/intern/proxy.c @@ -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) {