{"record":{"id":"0415790443184ff7","repo":"DrKLO/Telegram","slug":"s-can-t-open-s-for-writing","errorCode":null,"errorMessage":"%s: can't open %s for writing\n","messagePattern":"(.+?): can't open (.+?) for writing\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/jpegtran.c","lineNumber":614,"sourceCode":"                                                 &transformoption);\n#else\n  dst_coef_arrays = src_coef_arrays;\n#endif\n\n  /* Close input file, if we opened it.\n   * Note: we assume that jpeg_read_coefficients consumed all input\n   * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will\n   * only consume more while (!cinfo->inputctl->eoi_reached).\n   * We cannot call jpeg_finish_decompress here since we still need the\n   * virtual arrays allocated from the source object for processing.\n   */\n  if (fp != stdin)\n    fclose(fp);\n\n  /* Open the output file. */\n  if (outfilename != NULL) {\n    if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) {\n      fprintf(stderr, \"%s: can't open %s for writing\\n\", progname,\n              outfilename);\n      exit(EXIT_FAILURE);\n    }\n  } else {\n    /* default output file is stdout */\n    fp = write_stdout();\n  }\n\n  /* Adjust default compression parameters by re-parsing the options */\n  file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);\n\n  /* Specify data destination for compression */\n#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)\n  if (jpeg_c_int_param_supported(&dstinfo, JINT_COMPRESS_PROFILE) &&\n      jpeg_c_get_int_param(&dstinfo, JINT_COMPRESS_PROFILE)\n        == JCP_MAX_COMPRESSION)\n    jpeg_mem_dest(&dstinfo, &outbuffer, &outsize);\n  else","sourceCodeStart":596,"sourceCodeEnd":632,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/jpegtran.c#L596-L632","documentation":"jpegtran opens the output file with fopen(outfilename, WRITE_BINARY) at jpegtran.c:613. If fopen returns NULL, the output path is invalid: the parent directory doesn't exist, the process lacks write permission, or the disk is full. The program exits before writing any output.","triggerScenarios":"Output directory doesn't exist (e.g. /tmp/out/ when /tmp/out is missing). No write permission on the target directory (e.g. /system on Android). Disk full or quota exceeded. The output path is a directory, not a file.","commonSituations":"JNI caller passes an output path in a sandboxed directory that the native process can't write to. Output path to external storage on Android without WRITE_EXTERNAL_STORAGE permission. Path with a trailing slash interpreted as a directory.","solutions":["Ensure the parent directory exists: `mkdir -p $(dirname outpath)` before invoking","Verify write permission: `access(dirname, W_OK) == 0`","Use the app's internal storage (`getFilesDir()`) for output","Check available disk space before writing large outputs"],"exampleFix":"// before\njpegtran input.jpg -outfile /sdcard/output/out.jpg\n\n// after\nmkdir -p /sdcard/output\njpegtran input.jpg -outfile /sdcard/output/out.jpg","handlingStrategy":"validation","validationCode":"// Validate output path writability before invoking jpegtran\nint validate_output_path(const char *path) {\n    char dir[4096];\n    strncpy(dir, path, sizeof(dir)-1); dir[sizeof(dir)-1] = '\\0';\n    char *slash = strrchr(dir, '/');\n    if (slash) *slash = '\\0';\n    else strcpy(dir, \".\");\n    if (access(dir, W_OK) != 0) {\n        fprintf(stderr, \"Output directory not writable: %s\\n\", dir);\n        return -1;\n    }\n    // check available space (at least 2x input size)\n    struct statvfs vfs;\n    if (statvfs(dir, &vfs) == 0) {\n        unsigned long long avail = (unsigned long long)vfs.f_bavail * vfs.f_bsize;\n        if (avail < 1024*1024) return -1; // less than 1MB free\n    }\n    return 0;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create the output directory before invoking jpegtran","Verify write permission on the output directory with access(dir, W_OK)","Use the app's internal storage for output to avoid permission issues"],"tags":["jpeg","jpegtran","file-io","write-permission","output"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}