END_TEST START_TEST(test_mkcachedir) { const char *workdir = test_globals.tmpdir; char * dir; fail_if(asprintf(&dir, "%s%s", workdir, "/mkcachedir/sub/deep") == -1); fail_if(mkcachedir(dir)); fail_if(access(dir, R_OK|X_OK|W_OK)); free(dir); fail_if(asprintf(&dir, "%s%s", workdir, "/mkcachedir/sub2/wd-XXXXXX") == -1); fail_if(mkcachedir(dir)); fail_if(g_str_has_suffix(dir, "XXXXXX")); /* mkcache dir changed the Xs */ fail_if(access(dir, R_OK|X_OK|W_OK)); /* test the globbing capability of mkcachedir */ char *dir2; fail_if(asprintf(&dir2, "%s%s", workdir, "/mkcachedir/sub2/wd-XXXXXX") == -1); fail_if(mkcachedir(dir2)); fail_if(strcmp(dir, dir2)); /* mkcachedir should have arrived at the same name */ free(dir2); free(dir); }
/** * Recursively create directory. * * If it is in the format accepted by mkdtemp() the function globs for a * matching name and if not found it uses mkdtemp() to create the path. 'path' * is modified in those two cases. */ int mkcachedir(char *path) { int ret = 1; if (!glob_for_cachedir(path)) return 0; const int len = strlen(path); if (len < 1 || path[0] != '/') return 1; // only absolute pathnames are accepted char *p = solv_strdup(path); if (p[len-1] == '/') p[len-1] = '\0'; if (access(p, X_OK)) { *(strrchr(p, '/')) = '\0'; ret = mkcachedir(p); if (str_endswith(path, "XXXXXX")) { char *retptr = mkdtemp(path); if (retptr == NULL) ret |= 1; } else ret |= mkdir(path, CACHEDIR_PERMISSIONS); } else { ret = 0; } solv_free(p); return ret; }