int ensureDirectory (const char *path) { if (testDirectoryPath(path)) return 1; if (errno == EEXIST) { logMessage(LOG_ERR, "not a directory: %s", path); } else if (errno != ENOENT) { logMessage(LOG_ERR, "cannot access directory: %s: %s", path, strerror(errno)); } else { { char *directory = getPathDirectory(path); if (!directory) return 0; { int exists = ensureDirectory(directory); free(directory); if (!exists) return 0; } } if (createDirectory(path)) { logMessage(LOG_NOTICE, "directory created: %s", path); return 1; } } return 0; }
const char * getWritableDirectory (void) { if (writableDirectory && *writableDirectory) if (ensureDirectory(writableDirectory)) return writableDirectory; return NULL; }
FILE * openDataFile (const char *path, const char *mode, int optional) { const char *name = locatePathName(path); const char *overrideDirectory = getOverrideDirectory(); char *overridePath; FILE *file; if (!overrideDirectory) { overridePath = NULL; } else if ((overridePath = makePath(overrideDirectory, name))) { if (testFilePath(overridePath)) { file = openFile(overridePath, mode, optional); goto done; } } if (!(file = openFile(path, mode, optional))) { if ((*mode == 'w') || (*mode == 'a')) { if (errno == ENOENT) { char *directory = getPathDirectory(path); if (directory) { int exists = ensureDirectory(directory); free(directory); if (exists) { file = openFile(path, mode, optional); goto done; } } } if (((errno == EACCES) || (errno == EROFS)) && overridePath) { if (ensureDirectory(overrideDirectory)) { file = openFile(overridePath, mode, optional); goto done; } } } } done: if (overridePath) free(overridePath); return file; }
static char * usbGetFileSystem (const char *type, const FileSystemCandidate *candidates, MountPointTester test, FileSystemVerifier verify) { if (candidates) { const FileSystemCandidate *candidate = candidates; while (candidate->path) { logMessage(LOG_DEBUG, "verifying file system path: %s: %s", type, candidate->path); if (candidate->verify(candidate->path)) { char *path = strdup(candidate->path); if (path) return path; logMallocError(); } candidate += 1; } } if (test) { char *path = findMountPoint(test); if (path) return path; } if (verify) { char *directory = makeWritablePath(type); if (directory) { if (ensureDirectory(directory)) { if (verify(directory)) return directory; { const char *strings[] = {PACKAGE_NAME, "-", type}; char *name = joinStrings(strings, ARRAY_COUNT(strings)); if (makeMountPoint(directory, name, type)) return directory; } } free(directory); } } return NULL; }
/** * Creates/open a shared memory region * * The rootname will uniquely identify the shared memory region, * and is valid across different JVM instance. * * The shared memory region should persist across process, until OS reboots * or destroy call is being made. * * @param[in] portLibrary The port Library * @param[out] handle This handle is required for further attach/destroy of the memory region * @param[in] rootname Shared name for the region, which used to identify the region. * @param[in] size Size of the region in bytes * @param[in] perm permission for the region. * * @return * \arg HYPORT_ERROR_SHMEM_OPFAILED Failure - Cannot open the shared memory region * \arg HYPORT_INFO_SHMEM_OPENED Success - Existing memory region has been opened * \arg HYPORT_INFO_SHMEM_CREATED Success - A new shared memory region has been created * */ IDATA VMCALL hyshmem_open (HyPortLibrary * portLibrary, struct hyshmem_handle **handle, const char *rootname, I_32 size, I_32 perm) { /*TODO: Do we need the length to be longer? */ char controlFile[HYSH_MAXPATH]; IDATA retryCount, exist; key_t fkey; void *region; int retry = RETRY_COUNT; Trc_PRT_shmem_hyshmem_open_Entry (rootname, size, perm); if (ensureDirectory (portLibrary) == FAILED) { portLibrary->error_set_last_error (portLibrary, errno, HYPORT_ERROR_SHMEM_DATA_DIRECTORY_FAILED); Trc_PRT_shmem_hyshmem_open_Exit3 (); return HYPORT_ERROR_SHSEM_OPFAILED; } getControlFilePath (portLibrary, controlFile, HYSH_MAXPATH, rootname); while (retry) { I_32 rc; rc = portLibrary->file_attr (portLibrary, controlFile); if (HyIsFile != rc) { Trc_PRT_shmem_hyshmem_open_Event1 (controlFile); rc = createSharedMemory (portLibrary, handle, controlFile, size, perm); } else { Trc_PRT_shmem_hyshmem_open_Event2 (controlFile); rc = openSharedMemory (portLibrary, handle, controlFile); } switch (rc) { case RETRY: Trc_PRT_shmem_hyshmem_open_Event3 (retry); retry--; usleep (100); continue; case FAILED: Trc_PRT_shmem_hyshmem_open_Exit1 (); return HYPORT_ERROR_SHMEM_OPFAILED; default: Trc_PRT_shmem_hyshmem_open_Exit (rc, *handle); return rc; } } /* max number of retry count reach, return failure */ portLibrary->file_unlink (portLibrary, controlFile); Trc_PRT_shmem_hyshmem_open_Exit2 (); return HYPORT_ERROR_SHMEM_OPFAILED; }