Yalantis/uCrop · error · CImgIOException

cimg::temporary_path(): Failed to locate path for writing te

Error message

cimg::temporary_path(): Failed to locate path for writing temporary files.

What it means

cimg::temporary_path() probes candidate directories (environment variables like TMPDIR/TEMP/TMP, system temp paths, the current directory) by attempting to create and remove a scratch file. If no candidate is writable it releases its mutex and throws CImgIOException, because many CImg features (temp files for ffmpeg, ImageMagick, display) need a writable temp directory.

Source

Thrown at ucrop/src/main/jni/CImg.h:69756

        _cimg_test_temporary_path("C:\\WINDOWS\\Temp");
        _cimg_test_temporary_path("C:\\Temp");
        _cimg_test_temporary_path("C:");
        _cimg_test_temporary_path("D:\\WINNT\\Temp");
        _cimg_test_temporary_path("D:\\WINDOWS\\Temp");
        _cimg_test_temporary_path("D:\\Temp");
        _cimg_test_temporary_path("D:");
#else
        _cimg_test_temporary_path("/tmp");
        _cimg_test_temporary_path("/var/tmp");
#endif
        if (!path_found) {
          *s_path = 0;
          std::strncpy(tmp,filename_tmp,tmp._width - 1);
          if ((file=cimg::std_fopen(tmp,"wb"))!=0) { cimg::fclose(file); std::remove(tmp); path_found = true; }
        }
        if (!path_found) {
          cimg::mutex(7,0);
          throw CImgIOException("cimg::temporary_path(): Failed to locate path for writing temporary files.\n");
        }
      }
      cimg::mutex(7,0);
      return s_path;
    }

    //! Get/set path to the \c wget binary.
    /**
       \param user_path Specified path, or \c 0 to get the path currently used.
       \param reinit_path Force path to be recalculated (may take some time).
       \return Path containing the \c wget binary.
    **/
    inline const char *wget_path(const char *const user_path, const bool reinit_path) {
      static CImg<char> s_path;
      cimg::mutex(7);
      if (reinit_path) s_path.assign();
      if (user_path) {
        if (!s_path) s_path.assign(1024);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Set the TMPDIR environment variable to an existing writable directory before running the process.
  2. Explicitly configure CImg: call cimg::temporary_path("/data/local/tmp") with a verified writable path.
  3. Check directory permissions on the candidate temp locations and the CWD.
  4. If sandboxed (Android/app container), create a dedicated cache dir and export it as TMPDIR at startup.

Example fix

// before
const char* tmp = cimg::temporary_path(); // throws if none found
// after
if (!cimg::path_exists("/data/local/tmp"))
  throw std::runtime_error("no writable temp dir available");
cimg::temporary_path("/data/local/tmp");
const char* tmp = cimg::temporary_path();
Defensive patterns

Strategy: fallback

Validate before calling

// Probe a writable temp dir before calling CImg APIs that need one
const char* candidates[] = { getenv("TMPDIR"), getenv("TEMP"), getenv("TMP"), "." };
bool haveTemp = false;
for (const char* d : candidates)
  if (d && cimg::path_exists(d)) { haveTemp = true; break; }
if (!haveTemp) setenv("TMPDIR", "/data/local/tmp", 1); // or fail early

Try / catch

try {
  const char* tmp = cimg::temporary_path();
} catch (CImgIOException& e) {
  cimg::warn("no writable temp dir; configure TMPDIR");
}

Prevention

When it happens

Trigger: Calling cimg::temporary_path() (or any feature needing temp files, e.g. save_ffmpeg_external, save_magick, X11 display helpers) when TMPDIR/TEMP/TMP are unset or point to nonexistent dirs, and the current directory is read-only.

Common situations: Sandboxed Android app with no writable CWD; hardened server with read-only working directory and no TMPDIR set; misconfigured TEMP pointing to a deleted folder; running under a no-write mount.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/657f36690a6ce78b. Report an issue: GitHub.