/** * this function is a POSIX compliant version, which will open a file and return * a file descriptor. * * @param file the path name of file. * @param flags the file open flags. * @param mode * * @return the non-negative integer on successful open, others for failed. */ int open(const char *file, int flags, int mode) { int fd, result; struct dfs_fd *d; /* allocate a fd */ fd = fd_new(); if (fd < 0) { rt_set_errno(-DFS_STATUS_ENOMEM); return -1; } d = fd_get(fd); result = dfs_file_open(d, file, flags); if (result < 0) { /* release the ref-count of fd */ fd_put(d); fd_put(d); rt_set_errno(result); return -1; } /* release the ref-count of fd */ fd_put(d); return fd; }
static struct fd_info *file_open(struct file_info *file) { int fd; char *path; path = dir_path(file->parent, file->name); fd = open(path, O_RDWR); CHECK(fd != -1); free(path); return fd_new(file, fd); }
int socket(int domain, int type, int protocol) { int internal_socket = destiny_socket(domain, type, protocol); if (internal_socket < 0) { errno = ENFILE; return -1; } return fd_new(internal_socket, flagless_recv, flagless_send, destiny_socket_close); }
int main(){ FDict *fd = fd_new(); fd_insert(fd, 102, 19); fd_insert(fd, 10, 2); fd_insert(fd, 89, 2); fd_insert(fd, 350, 2); fd_insert(fd, 5, 2); fd_each(fd, pn); puts("~~~~~~~~~~~~~~~~~~~~~~~~~~"); fd_remove(fd, 5); fd_each(fd, pn); return 0; }
int socket(int domain, int type, int protocol) { int res = 0; socket_t *s; mutex_lock(&_pool_mutex); s = _get_free_socket(); if (s == NULL) { errno = ENFILE; mutex_unlock(&_pool_mutex); return -1; } switch (domain) { case AF_INET: case AF_INET6: s->domain = domain; s->type = type; if ((s->protocol = _choose_ipproto(type, protocol)) < 0) { res = -1; } break; default: (void)type; (void)protocol; errno = EAFNOSUPPORT; res = -1; } if (res == 0) { /* TODO: add read and write */ int fd = fd_new(s - _pool, socket_read, socket_write, socket_close); if (fd < 0) { errno = ENFILE; res = -1; } else { s->fd = res = fd; } } s->bound = false; s->src_port = 0; mutex_unlock(&_pool_mutex); return res; }
static struct file_info *file_new(struct dir_info *parent, const char *name) { struct file_info *file = NULL; char *path; mode_t mode; int fd; size_t sz; struct dir_entry_info *entry; CHECK(parent != NULL); path = dir_path(parent, name); mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; fd = open(path, O_CREAT | O_EXCL | O_RDWR, mode); if (fd == -1) { CHECK(errno == ENOSPC); free(path); full = 1; return NULL; } free(path); sz = sizeof(struct file_info); file = (struct file_info *) malloc(sz); CHECK(file != NULL); memset(file, 0, sz); file->name = copy_string(name); file->parent = parent; fd_new(file, fd); entry = dir_entry_new(); entry->type = 'f'; entry->entry.file = file; entry->next = parent->first; parent->first = entry; parent->number_of_entries += 1; return file; }