/* Find a device file */ static const struct fsDevEntry *fsFindDevEntry(const char *name) { size_t i; for (i = 0; i < sizeof(fsDevEntries) / sizeof(struct fsDevEntry); i++) { if (!strcmp(fsDevEntries[i].name, name)) return fsDevEntries + i; } fsSetError(FS_ERR_NOENT); return NULL; }
/* Find entry in the prefix table */ static const struct fsPrefixEntry *fsFindPrefixEntry(const char *path) { size_t i; for (i = 0; i < sizeof(prefixEntries) / sizeof(struct fsPrefixEntry); i++) { size_t prefixlen = strlen(prefixEntries[i].prefix); if (strlen(path) > prefixlen && !strncmp(path, prefixEntries[i].prefix, prefixlen)) { return prefixEntries + i; } } fsSetError(FS_ERR_NOENT); return NULL; }
static void *blkOpen(const char *path, fsMode_t mode) { struct fsFileControlBlock *fcb; if (mode & FS_RDWR || !(mode & (FS_READ | FS_APPEND)) || (mode & FS_READ && mode & FS_APPEND)) { fsSetError(FS_ERR_INVAL); return NULL; } fcb = fsBlockOpenFile(path, mode & FS_APPEND); if (fcb) { struct fsBlockHandle *handle = allocHandle(fcb); if (handle) { handle->mode = mode; mos_mutex_lock(&fcb->mutex); if (mode & FS_READ) /* Read mode */ { handle->pos = 0; handle->curr = fcb->first; handle->readEnd = 0; } else /* Write mode */ { /* Traverse blocks until the last one */ seekBlock(handle, fcb->size); handle->pos = fcb->size; } mos_mutex_unlock(&fcb->mutex); } else fsBlockCloseFile(fcb); return handle; } else return NULL; }
/* Allocate a new file handle */ static int8_t fsAllocHandle(const struct fsOperations *ops) { int8_t i; mos_mutex_lock(&openFilesMutex); for (i = 0; i < FS_MAX_OPEN_FILES; i++) { if (!openFiles[i].ops) { openFiles[i].ops = ops; break; } } mos_mutex_unlock(&openFilesMutex); if (i == FS_MAX_OPEN_FILES) { fsSetError(FS_ERR_NOMEM); return -1; } return i; }