From 2b3eb35a9f2ad00afd09e26e4e2a988baac4dbaf Mon Sep 17 00:00:00 2001 From: Joshua Minter Date: Sun, 24 Jan 2021 13:17:13 +1000 Subject: [PATCH] Create missing directories --- CMakeLists.txt | 7 ++++--- src/control/dbgctrl.c | 12 ++++++------ src/output.c | 45 +++++++++++++++++++++++++++++++++++++++++++ src/rsbuild.h.in | 3 ++- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ea711d..b9d80e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.0) project(ReplaySorcery VERSION 0.5.0) include(CheckCCompilerFlag) -include(CheckIncludeFile) +include(CheckSymbolExists) include(ExternalProject) find_package(PkgConfig REQUIRED) @@ -143,8 +143,9 @@ if (LIBDRM_FOUND) endif() # Build configuration -check_include_file(unistd.h RS_BUILD_UNISTD_FOUND) -check_include_file(fcntl.h RS_BUILD_FCNTL_FOUND) +check_symbol_exists(read unistd.h RS_BUILD_READ_FOUND) +check_symbol_exists(fcntl fcntl.h RS_BUILD_FCNTL_FOUND) +check_symbol_exists(mkdir sys/stat.h RS_BUILD_MKDIR_FOUND) configure_file(src/rsbuild.h.in rsbuild.h) target_include_directories(${binary} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/src/control/dbgctrl.c b/src/control/dbgctrl.c index db7ab5e..4c818f2 100644 --- a/src/control/dbgctrl.c +++ b/src/control/dbgctrl.c @@ -20,14 +20,14 @@ #include "control.h" #include "rsbuild.h" #include -#ifdef RS_BUILD_UNISTD_FOUND +#ifdef RS_BUILD_READ_FOUND #include #endif #ifdef RS_BUILD_FCNTL_FOUND #include #endif -#if defined(RS_BUILD_UNISTD_FOUND) && defined(RS_BUILD_FCNTL_FOUND) +#if defined(RS_BUILD_READ_FOUND) && defined(RS_BUILD_FCNTL_FOUND) static int debugControlWantsSave(RSControl *control) { (void)control; int ret = 0; @@ -50,7 +50,7 @@ static int debugControlWantsSave(RSControl *control) { #endif int rsDebugControlCreate(RSControl *control) { -#if defined(RS_BUILD_UNISTD_FOUND) && defined(RS_BUILD_FCNTL_FOUND) +#if defined(RS_BUILD_READ_FOUND) && defined(RS_BUILD_FCNTL_FOUND) control->destroy = NULL; control->wantsSave = debugControlWantsSave; int flags = fcntl(0, F_GETFL); @@ -59,11 +59,11 @@ int rsDebugControlCreate(RSControl *control) { #else (void)control; -#ifndef RS_BUILD_UNISTD_FOUND - av_log(NULL, AV_LOG_ERROR, " was not found during compilation\n"); +#ifndef RS_BUILD_READ_FOUND + av_log(NULL, AV_LOG_ERROR, "read() was not found during compilation\n"); #endif #ifndef RS_BUILD_FCNTL_FOUND - av_log(NULL, AV_LOG_ERROR, " was not found during compilation\n"); + av_log(NULL, AV_LOG_ERROR, "fcntl() was not found during compilation\n"); #endif return AVERROR(ENOSYS); #endif diff --git a/src/output.c b/src/output.c index 09bfdc1..0825321 100644 --- a/src/output.c +++ b/src/output.c @@ -19,10 +19,52 @@ #include "output.h" #include "config.h" +#include "rsbuild.h" #include "util.h" #include #include #include +#ifdef RS_BUILD_MKDIR_FOUND +#include +#endif + +static int outputDirectory(char *path) { +#ifdef RS_BUILD_MKDIR_FOUND + int ret; + char *slash = strrchr(path, '/'); + if (slash == NULL) { + return 0; + } + + *slash = '\0'; + if ((ret = mkdir(path, 0777)) < 0) { + ret = errno; + } + if (ret == ENOENT) { + if ((ret = outputDirectory(path)) < 0) { + goto error; + } + if ((ret = mkdir(path, 0777)) < 0) { + ret = errno; + } + } + if (ret != 0 && ret != EEXIST) { + ret = AVERROR(ret); + av_log(NULL, AV_LOG_ERROR, "Failed to create directory: %s\n", av_err2str(ret)); + goto error; + } + + ret = 0; +error: + *slash = '/'; + return ret; + +#else + (void)path; + av_log(NULL, AV_LOG_WARNING, "mkdir() was not found during compilation\n"); + return 0; +#endif +} int rsOutputCreate(RSOutput *output) { int ret; @@ -51,6 +93,9 @@ int rsOutputCreate(RSOutput *output) { if ((ret = av_bprint_finalize(&buffer, &output->path)) < 0) { goto error; } + if ((ret = outputDirectory(output->path)) < 0) { + goto error; + } av_log(NULL, AV_LOG_INFO, "Saving video to '%s'...\n", output->path); if ((ret = avformat_alloc_output_context2(&output->formatCtx, NULL, "mp4", diff --git a/src/rsbuild.h.in b/src/rsbuild.h.in index cae3df5..ff0a0f6 100644 --- a/src/rsbuild.h.in +++ b/src/rsbuild.h.in @@ -27,7 +27,8 @@ #cmakedefine RS_BUILD_X11_FOUND #cmakedefine RS_BUILD_PULSE_FOUND #cmakedefine RS_BUILD_LIBDRM_FOUND -#cmakedefine RS_BUILD_UNISTD_FOUND +#cmakedefine RS_BUILD_READ_FOUND #cmakedefine RS_BUILD_FCNTL_FOUND +#cmakedefine RS_BUILD_MKDIR_FOUND #endif