// Combine two parts of a paths into a new path. // The new path starts with a '/' only when the first part does. // The first path may (but doesn't have to) end on a '/', or may be empty. // Pre: the second path doesn't start with a '/' char * joinPaths(const char *first, const char *second) { char *result, *resPtr; size_t firstLen, secondLen; if (first[0] == '\0') return uio_strdup(second); firstLen = strlen(first); if (first[firstLen - 1] == '/') firstLen--; secondLen = strlen(second); result = uio_malloc(firstLen + secondLen + 2); resPtr = result; memcpy(resPtr, first, firstLen); resPtr += firstLen; *resPtr = '/'; resPtr++; memcpy(resPtr, second, secondLen); resPtr += secondLen; *resPtr = '\0'; return result; }
match_Result match_prepareSuffix(const char *pattern, match_SuffixContext **contextPtr) { *contextPtr = match_newSuffixContext( uio_strdup(pattern), strlen(pattern)); return match_OK; }
// if wantedID = 0, just pick one // if wantedID != 0, 0 will be returned if that id wasn't available // a copy of 'name' is made uio_FileSystemID uio_registerFileSystem(uio_FileSystemID wantedID, const char *name, uio_FileSystemHandler *handler) { uio_FileSystemInfo **ptr; if (!uio_validFileSystemHandler(handler)) { errno = EINVAL; return -1; } if (wantedID == 0) { // Search for the first free id >= uio_FIRST_CUSTOM_ID // it is put in wantedID for (ptr = &uio_fileSystems; *ptr != NULL; ptr = &(*ptr)->next) if ((*ptr)->id >= uio_FS_FIRST_CUSTOM_ID) break; wantedID = uio_FS_FIRST_CUSTOM_ID; while (*ptr != NULL) { if ((*ptr)->id != wantedID) { // wantedID is not in use break; } wantedID++; ptr = &(*ptr)->next; } // wantedID contains the new ID } else { // search for the place in the list where to insert the wanted // id, keeping the list sorted for (ptr = &uio_fileSystems; *ptr != NULL; ptr = &(*ptr)->next) { if ((*ptr)->id <= wantedID) { if ((*ptr)->id == wantedID) return 0; break; } } } // ptr points to the place where the new link can inserted if (handler->init != NULL && handler->init() == -1) { // errno is set return -1; } { uio_FileSystemInfo *newInfo; newInfo = uio_FileSystemInfo_new(wantedID, handler, uio_strdup(name)); newInfo->next = *ptr; *ptr = newInfo; return wantedID; } }
UIO_API uio_object_t uio_object_create(uio_object_t parent, int size, const char* name, int priv_size) { assert(size>=0); int total = sizeof(struct UioObject)+size; struct UioObject* Obj = uio_zalloc(total); assert(Obj); if (Obj) { Obj->size = size; Obj->name = uio_strdup(name); Obj->ref_count = 0; if (priv_size) { Obj->priv_size = priv_size; Obj->priv_data = uio_zalloc(priv_size); assert(Obj->priv_data); } if (parent) uio_object_hold(parent); Obj->parent = parent; } return uio_object_of(Obj); }
match_Result match_prepareGlob(const char *pattern, match_GlobContext **contextPtr) { *contextPtr = match_newGlobContext(uio_strdup(pattern)); return match_OK; }
match_Result match_prepareSubString(const char *pattern, match_SubStringContext **contextPtr) { *contextPtr = match_newSubStringContext(uio_strdup(pattern)); return match_OK; }
match_Result match_preparePrefix(const char *pattern, match_PrefixContext **contextPtr) { *contextPtr = match_newPrefixContext(uio_strdup(pattern)); return match_OK; }
match_Result match_prepareLitteral(const char *pattern, match_LitteralContext **contextPtr) { *contextPtr = match_newLitteralContext(uio_strdup(pattern)); return match_OK; }
uio_StdioAccessHandle * uio_getStdioAccess(uio_DirHandle *dir, const char *path, int flags, uio_DirHandle *tempDir) { int res; uio_MountHandle *mountHandle; const char *name; char *newPath; char *tempDirName; uio_DirHandle *newDir; uio_FileSystemID fsID; res = uio_getFileLocation(dir, path, flags, &mountHandle, &newPath); if (res == -1) { // errno is set return NULL; } fsID = uio_getMountFileSystemType(mountHandle); if (fsID == uio_FSTYPE_STDIO) { // Current location is usable. return uio_StdioAccessHandle_new(NULL, NULL, NULL, NULL, newPath); } uio_free(newPath); { uio_uint32 dirNum; int i; // Current location is not usable. Create a directory with a // generated name, as a temporary location to store a copy of // the file. dirNum = (uio_uint32) time(NULL); tempDirName = uio_malloc(sizeof "01234567"); for (i = 0; ; i++) { #ifdef NUM_TEMP_RETRIES if (i >= NUM_TEMP_RETRIES) { // Using ENOSPC to report that we couldn't create a // temporary dir, getting EEXIST. uio_free(tempDirName); errno = ENOSPC; return NULL; } #endif sprintf(tempDirName, "%08lx", (unsigned long) dirNum + i); res = uio_mkdir(tempDir, tempDirName, 0700); if (res == -1) { int savedErrno; if (errno == EEXIST) continue; savedErrno = errno; #ifdef DEBUG fprintf(stderr, "Error: Could not create temporary dir: %s\n", strerror(errno)); #endif uio_free(tempDirName); errno = savedErrno; return NULL; } break; } newDir = uio_openDirRelative(tempDir, tempDirName, 0); if (newDir == NULL) { #ifdef DEBUG fprintf(stderr, "Error: Could not open temporary dir: %s\n", strerror(errno)); #endif res = uio_rmdir(tempDir, tempDirName); #ifdef DEBUG if (res == -1) fprintf(stderr, "Warning: Could not remove temporary dir: " "%s.\n", strerror(errno)); #endif uio_free(tempDirName); errno = EIO; return NULL; } // Get the last component of path. This should be the file to // access. name = strrchr(path, '/'); if (name == NULL) name = path; // Copy the file res = uio_copyFile(dir, path, newDir, name); if (res == -1) { int savedErrno = errno; #ifdef DEBUG fprintf(stderr, "Error: Could not copy file to temporary dir: " "%s\n", strerror(errno)); #endif uio_closeDir(newDir); uio_free(tempDirName); errno = savedErrno; return NULL; } } res = uio_getFileLocation(newDir, name, flags, &mountHandle, &newPath); if (res == -1) { int savedErrno = errno; fprintf(stderr, "Error: uio_getStdioAccess: Could not get location " "of temporary dir: %s.\n", strerror(errno)); uio_closeDir(newDir); uio_free(tempDirName); errno = savedErrno; return NULL; } fsID = uio_getMountFileSystemType(mountHandle); if (fsID != uio_FSTYPE_STDIO) { // Temp dir isn't on a stdio fs either. fprintf(stderr, "Error: uio_getStdioAccess: Temporary file location " "isn't on a stdio filesystem.\n"); uio_closeDir(newDir); uio_free(tempDirName); uio_free(newPath); // errno = EXDEV; errno = EINVAL; return NULL; } uio_DirHandle_ref(tempDir); return uio_StdioAccessHandle_new(tempDir, tempDirName, newDir, uio_strdup(name), newPath); }