diff --git a/components/dfs/filesystems/cromfs/dfs_cromfs.c b/components/dfs/filesystems/cromfs/dfs_cromfs.c index da01e669f2abe666f5cfcb6439aba33e04e3181c..0365ce4d8ef6e489508a40361b30f2764222f0f5 100644 --- a/components/dfs/filesystems/cromfs/dfs_cromfs.c +++ b/components/dfs/filesystems/cromfs/dfs_cromfs.c @@ -929,7 +929,7 @@ static int dfs_cromfs_open(struct dfs_fd *file) int is_dir = 0; rt_err_t result = RT_EOK; - if (file->fnode->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR)) + if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR)) { return -EINVAL; } @@ -954,7 +954,7 @@ static int dfs_cromfs_open(struct dfs_fd *file) /* entry is a directory file type */ if (is_dir) { - if (!(file->fnode->flags & O_DIRECTORY)) + if (!(file->flags & O_DIRECTORY)) { ret = -ENOENT; goto end; @@ -963,7 +963,7 @@ static int dfs_cromfs_open(struct dfs_fd *file) else { /* entry is a file, but open it as a directory */ - if (file->fnode->flags & O_DIRECTORY) + if (file->flags & O_DIRECTORY) { ret = -ENOENT; goto end; diff --git a/components/dfs/filesystems/devfs/devfs.c b/components/dfs/filesystems/devfs/devfs.c index 364618376b2ac227622b6dc5e909786c4bc2e1e3..305ae372a493d1735d21e5aa8a8e8c79e147393e 100644 --- a/components/dfs/filesystems/devfs/devfs.c +++ b/components/dfs/filesystems/devfs/devfs.c @@ -127,7 +127,7 @@ int dfs_device_fs_open(struct dfs_fd *file) /* open root directory */ if ((file->fnode->path[0] == '/') && (file->fnode->path[1] == '\0') && - (file->fnode->flags & O_DIRECTORY)) + (file->flags & O_DIRECTORY)) { struct rt_object *object; struct rt_list_node *node; diff --git a/components/dfs/filesystems/elmfat/dfs_elm.c b/components/dfs/filesystems/elmfat/dfs_elm.c index c989a04cb42d68d7bdbfd1f688eef3ef4d04bfb4..2aa2e575b3e356554eae0aa4d663c81680beaa04 100644 --- a/components/dfs/filesystems/elmfat/dfs_elm.c +++ b/components/dfs/filesystems/elmfat/dfs_elm.c @@ -351,11 +351,11 @@ int dfs_elm_open(struct dfs_fd *file) drivers_fn = file->fnode->path; #endif - if (file->fnode->flags & O_DIRECTORY) + if (file->flags & O_DIRECTORY) { DIR *dir; - if (file->fnode->flags & O_CREAT) + if (file->flags & O_CREAT) { result = f_mkdir(drivers_fn); if (result != FR_OK) @@ -394,26 +394,26 @@ int dfs_elm_open(struct dfs_fd *file) { mode = FA_READ; - if (file->fnode->flags & O_WRONLY) + if (file->flags & O_WRONLY) { mode |= FA_WRITE; } - if ((file->fnode->flags & O_ACCMODE) & O_RDWR) + if ((file->flags & O_ACCMODE) & O_RDWR) { mode |= FA_WRITE; } /* Opens the file, if it is existing. If not, a new file is created. */ - if (file->fnode->flags & O_CREAT) + if (file->flags & O_CREAT) { mode |= FA_OPEN_ALWAYS; } /* Creates a new file. If the file is existing, it is truncated and overwritten. */ - if (file->fnode->flags & O_TRUNC) + if (file->flags & O_TRUNC) { mode |= FA_CREATE_ALWAYS; } /* Creates a new file. The function fails if the file is already existing. */ - if (file->fnode->flags & O_EXCL) + if (file->flags & O_EXCL) { mode |= FA_CREATE_NEW; } @@ -438,7 +438,7 @@ int dfs_elm_open(struct dfs_fd *file) file->fnode->size = f_size(fd); file->data = fd; - if (file->fnode->flags & O_APPEND) + if (file->flags & O_APPEND) { /* seek to the end of file */ f_lseek(fd, f_size(fd)); diff --git a/components/dfs/filesystems/jffs2/dfs_jffs2.c b/components/dfs/filesystems/jffs2/dfs_jffs2.c index 0a6130de0b7f4d866ac7657398a9954c1f077f22..b208ffb67fca9ac96f1409d65dc535565cb01030 100644 --- a/components/dfs/filesystems/jffs2/dfs_jffs2.c +++ b/components/dfs/filesystems/jffs2/dfs_jffs2.c @@ -223,7 +223,7 @@ static int dfs_jffs2_open(struct dfs_fd* file) struct dfs_filesystem *fs; struct cyg_mtab_entry * mte; - oflag = file->fnode->flags; + oflag = file->flags; fs = file->fnode->fs; RT_ASSERT(fs != RT_NULL); @@ -330,7 +330,7 @@ static int dfs_jffs2_close(struct dfs_fd* file) RT_ASSERT(file->fnode->data != NULL); jffs2_file = (cyg_file *)(file->fnode->data); - if (file->fnode->flags & O_DIRECTORY) /* operations about dir */ + if (file->flags & O_DIRECTORY) /* operations about dir */ { rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER); result = jffs2_dir_colse(jffs2_file); diff --git a/components/dfs/filesystems/nfs/dfs_nfs.c b/components/dfs/filesystems/nfs/dfs_nfs.c index 4b17222ef0c09c741cb2460e43641ddaeeeadac3..1da3940dd524c559eb44d34ff8110613761ca3df 100644 --- a/components/dfs/filesystems/nfs/dfs_nfs.c +++ b/components/dfs/filesystems/nfs/dfs_nfs.c @@ -747,19 +747,25 @@ int nfs_open(struct dfs_fd *file) nfs = (struct nfs_filesystem *)(dfs_nfs->data); RT_ASSERT(nfs != NULL); - if (file->fnode->flags & O_DIRECTORY) + if (file->flags & O_DIRECTORY) { nfs_dir *dir; - if (file->fnode->flags & O_CREAT) + if (file->flags & O_CREAT) { if (nfs_mkdir(nfs, file->fnode->path, 0755) < 0) + { return -EAGAIN; + } } /* open directory */ dir = nfs_opendir(nfs, file->fnode->path); - if (dir == NULL) return -ENOENT; + if (dir == NULL) + { + return -ENOENT; + } + file->fnode->type = FT_DIRECTORY; nfs->data = dir; } else @@ -768,10 +774,12 @@ int nfs_open(struct dfs_fd *file) nfs_fh3 *handle; /* create file */ - if (file->fnode->flags & O_CREAT) + if (file->flags & O_CREAT) { if (nfs_create(nfs, file->fnode->path, 0664) < 0) + { return -EAGAIN; + } } /* open file (get file handle ) */ @@ -796,7 +804,7 @@ int nfs_open(struct dfs_fd *file) xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); rt_free(handle); - if (file->fnode->flags & O_APPEND) + if (file->flags & O_APPEND) { fp->offset = fp->size; } diff --git a/components/dfs/filesystems/ramfs/dfs_ramfs.c b/components/dfs/filesystems/ramfs/dfs_ramfs.c index 656f971194d0c6e86d1fa5d5ac76bb5023e16296..516f195139a3dabf0f74ab790bb31aab57b8cfc3 100644 --- a/components/dfs/filesystems/ramfs/dfs_ramfs.c +++ b/components/dfs/filesystems/ramfs/dfs_ramfs.c @@ -182,9 +182,9 @@ int dfs_ramfs_open(struct dfs_fd *file) ramfs = (struct dfs_ramfs *)fs->data; RT_ASSERT(ramfs != NULL); - if (file->fnode->flags & O_DIRECTORY) + if (file->flags & O_DIRECTORY) { - if (file->fnode->flags & O_CREAT) + if (file->flags & O_CREAT) { return -ENOSPC; } @@ -195,7 +195,7 @@ int dfs_ramfs_open(struct dfs_fd *file) return -ENOENT; if (dirent == &(ramfs->root)) /* it's root directory */ { - if (!(file->fnode->flags & O_DIRECTORY)) + if (!(file->flags & O_DIRECTORY)) { return -ENOENT; } @@ -211,7 +211,7 @@ int dfs_ramfs_open(struct dfs_fd *file) if (dirent == NULL) { - if (file->fnode->flags & O_CREAT || file->fnode->flags & O_WRONLY) + if (file->flags & O_CREAT || file->flags & O_WRONLY) { char *name_ptr; @@ -227,7 +227,9 @@ int dfs_ramfs_open(struct dfs_fd *file) /* remove '/' separator */ name_ptr = file->fnode->path; while (*name_ptr == '/' && *name_ptr) + { name_ptr++; + } strncpy(dirent->name, name_ptr, RAMFS_NAME_MAX); rt_list_init(&(dirent->list)); @@ -245,7 +247,7 @@ int dfs_ramfs_open(struct dfs_fd *file) /* Creates a new file. * If the file is existing, it is truncated and overwritten. */ - if (file->fnode->flags & O_TRUNC) + if (file->flags & O_TRUNC) { dirent->size = 0; if (dirent->data != NULL) @@ -258,10 +260,14 @@ int dfs_ramfs_open(struct dfs_fd *file) file->fnode->data = dirent; file->fnode->size = dirent->size; - if (file->fnode->flags & O_APPEND) + if (file->flags & O_APPEND) + { file->pos = file->fnode->size; + } else + { file->pos = 0; + } return 0; } diff --git a/components/dfs/filesystems/romfs/dfs_romfs.c b/components/dfs/filesystems/romfs/dfs_romfs.c index aae8c10644ad714f641e485b7ede8ac3b11e1968..1184150e6a08e699792f49cb0f6a56ca0a0208e2 100644 --- a/components/dfs/filesystems/romfs/dfs_romfs.c +++ b/components/dfs/filesystems/romfs/dfs_romfs.c @@ -165,6 +165,10 @@ int dfs_romfs_lseek(struct dfs_fd *file, off_t offset) int dfs_romfs_close(struct dfs_fd *file) { + if (file->fnode->ref_count > 1) + { + return RT_EOK; + } file->fnode->data = NULL; return RT_EOK; } @@ -176,30 +180,44 @@ int dfs_romfs_open(struct dfs_fd *file) struct romfs_dirent *root_dirent; struct dfs_filesystem *fs; + if (file->fnode->ref_count > 1) + { + return RT_EOK; + } fs = file->fnode->fs; root_dirent = (struct romfs_dirent *)fs->data; if (check_dirent(root_dirent) != 0) + { return -EIO; + } - if (file->fnode->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR)) + if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR)) + { return -EINVAL; + } dirent = dfs_romfs_lookup(root_dirent, file->fnode->path, &size); if (dirent == NULL) + { return -ENOENT; + } /* entry is a directory file type */ if (dirent->type == ROMFS_DIRENT_DIR) { - if (!(file->fnode->flags & O_DIRECTORY)) + if (!(file->flags & O_DIRECTORY)) + { return -ENOENT; + } } else { /* entry is a file, but open it as a directory */ - if (file->fnode->flags & O_DIRECTORY) + if (file->flags & O_DIRECTORY) + { return -ENOENT; + } } file->fnode->data = dirent; @@ -219,7 +237,9 @@ int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) dirent = dfs_romfs_lookup(root_dirent, path, &size); if (dirent == NULL) + { return -ENOENT; + } st->st_dev = 0; st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | @@ -246,7 +266,9 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) dirent = (struct romfs_dirent *)file->fnode->data; if (check_dirent(dirent) != 0) + { return -EIO; + } RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR); /* enter directory */ @@ -255,7 +277,9 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) /* make integer count */ count = (count / sizeof(struct dirent)); if (count == 0) + { return -EINVAL; + } index = 0; for (index = 0; index < count && file->pos < file->fnode->size; index++) diff --git a/components/dfs/filesystems/uffs/dfs_uffs.c b/components/dfs/filesystems/uffs/dfs_uffs.c index f3b588e142f4dadaffaaefbad1edbc12fdd78aa5..ce60a1fc2fd67b2f9e800fcc62b4759aa3e55390 100644 --- a/components/dfs/filesystems/uffs/dfs_uffs.c +++ b/components/dfs/filesystems/uffs/dfs_uffs.c @@ -281,7 +281,7 @@ static int dfs_uffs_open(struct dfs_fd *file) int oflag, mode; char *file_path; - oflag = file->fnode->flags; + oflag = file->flags; if (oflag & O_DIRECTORY) /* operations about dir */ { uffs_DIR *dir; @@ -356,7 +356,7 @@ static int dfs_uffs_close(struct dfs_fd *file) int oflag; int fd; - oflag = file->fnode->flags; + oflag = file->flags; if (oflag & O_DIRECTORY) { /* operations about dir */ diff --git a/components/dfs/include/dfs_file.h b/components/dfs/include/dfs_file.h index da45ec20960caa037d5d88e1df13b8c99c82ad32..af82a1e1796e8e9f351eeb1fd33073be09093243 100644 --- a/components/dfs/include/dfs_file.h +++ b/components/dfs/include/dfs_file.h @@ -48,8 +48,8 @@ struct dfs_fnode struct dfs_filesystem *fs; const struct dfs_file_ops *fops; + uint32_t flags; /* self flags, is dir etc.. */ - uint32_t flags; /* Descriptor flags */ size_t size; /* Size in bytes */ void *data; /* Specific file system data */ }; @@ -57,6 +57,7 @@ struct dfs_fnode struct dfs_fd { uint16_t magic; /* file descriptor magic number */ + uint32_t flags; /* Descriptor flags */ int ref_count; /* Descriptor reference count */ off_t pos; /* Current file position */ struct dfs_fnode *fnode; /* file node struct */ diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index d08a8cd52b730a5f1122b98691f8bdc7bffe2d75..5f5691e501581e3f4dfb0b36763a201851bf37e2 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -186,7 +186,7 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) /* initialize the fd item */ fnode->type = FT_REGULAR; - fnode->flags = flags; + fnode->flags = 0; if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) { @@ -224,6 +224,8 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) rt_list_insert_after(hash_head, &fnode->list); } + fd->flags = flags; + if ((result = fnode->fops->open(fd)) < 0) { fnode->ref_count--; @@ -247,11 +249,11 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) return result; } - fnode->flags |= DFS_F_OPEN; + fd->flags |= DFS_F_OPEN; if (flags & O_DIRECTORY) { - fnode->type = FT_DIRECTORY; - fnode->flags |= DFS_F_DIRECTORY; + fd->fnode->type = FT_DIRECTORY; + fd->flags |= DFS_F_DIRECTORY; } dfs_fm_unlock(); @@ -340,15 +342,15 @@ int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args) switch (cmd) { case F_GETFL: - return fd->fnode->flags; /* return flags */ + return fd->flags; /* return flags */ case F_SETFL: { int flags = (int)(rt_base_t)args; int mask = O_NONBLOCK | O_APPEND; flags &= mask; - fd->fnode->flags &= ~mask; - fd->fnode->flags |= flags; + fd->flags &= ~mask; + fd->flags |= flags; } return 0; } @@ -388,7 +390,7 @@ int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len) if ((result = fd->fnode->fops->read(fd, buf, len)) < 0) { - fd->fnode->flags |= DFS_F_EOF; + fd->flags |= DFS_F_EOF; } return result; diff --git a/components/drivers/include/ipc/pipe.h b/components/drivers/include/ipc/pipe.h index 93ba95cd2c1234cb68df4b16b0bbe8e618f45b94..9048f82dba7224f02ac70342ae21e23007d5db2e 100644 --- a/components/drivers/include/ipc/pipe.h +++ b/components/drivers/include/ipc/pipe.h @@ -30,9 +30,6 @@ struct rt_pipe_device struct rt_ringbuffer *fifo; rt_uint16_t bufsz; - rt_uint8_t readers; - rt_uint8_t writers; - rt_wqueue_t reader_queue; rt_wqueue_t writer_queue; diff --git a/components/drivers/serial/serial.c b/components/drivers/serial/serial.c index 86e89e3c5125b79b1c85c77ae87218fdca49a91e..3b3405af927d20a2d6c508817397ededf2f2e1e8 100644 --- a/components/drivers/serial/serial.c +++ b/components/drivers/serial/serial.c @@ -68,7 +68,7 @@ static int serial_fops_open(struct dfs_fd *fd) device = (rt_device_t)fd->fnode->data; RT_ASSERT(device != RT_NULL); - switch (fd->fnode->flags & O_ACCMODE) + switch (fd->flags & O_ACCMODE) { case O_RDONLY: LOG_D("fops open: O_RDONLY!"); @@ -83,14 +83,19 @@ static int serial_fops_open(struct dfs_fd *fd) flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDWR; break; default: - LOG_E("fops open: unknown mode - %d!", fd->fnode->flags & O_ACCMODE); + LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE); break; } - if ((fd->fnode->flags & O_ACCMODE) != O_WRONLY) + if ((fd->flags & O_ACCMODE) != O_WRONLY) + { rt_device_set_rx_indicate(device, serial_fops_rx_ind); + } ret = rt_device_open(device, flags); - if (ret == RT_EOK) return 0; + if (ret == RT_EOK) + { + return 0; + } return ret; } @@ -136,7 +141,7 @@ static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count) size = rt_device_read(device, -1, buf, count); if (size <= 0) { - if (fd->fnode->flags & O_NONBLOCK) + if (fd->flags & O_NONBLOCK) { break; } @@ -177,7 +182,7 @@ static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req) serial = (struct rt_serial_device *)device; /* only support POLLIN */ - flags = fd->fnode->flags & O_ACCMODE; + flags = fd->flags & O_ACCMODE; if (flags == O_RDONLY || flags == O_RDWR) { rt_base_t level; diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c index efc66e14814dd6460c3c526e7432ab20ad647399..8b286930ff662afb91dc1b580f4de5f2d58a7969 100644 --- a/components/drivers/src/pipe.c +++ b/components/drivers/src/pipe.c @@ -20,16 +20,17 @@ static int pipe_fops_open(struct dfs_fd *fd) { int rc = 0; - rt_device_t device; rt_pipe_t *pipe; pipe = (rt_pipe_t *)fd->fnode->data; - if (!pipe) return -1; + if (!pipe) + { + return -1; + } - device = &(pipe->parent); - rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER); + rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER); - if (device->ref_count == 0) + if (fd->fnode->ref_count == 1) { pipe->fifo = rt_ringbuffer_create(pipe->bufsz); if (pipe->fifo == RT_NULL) @@ -39,23 +40,8 @@ static int pipe_fops_open(struct dfs_fd *fd) } } - switch (fd->fnode->flags & O_ACCMODE) - { - case O_RDONLY: - pipe->readers++; - break; - case O_WRONLY: - pipe->writers++; - break; - case O_RDWR: - pipe->readers++; - pipe->writers++; - break; - } - device->ref_count++; - __exit: - rt_mutex_release(&(pipe->lock)); + rt_mutex_release(&pipe->lock); return rc; } @@ -66,46 +52,26 @@ static int pipe_fops_close(struct dfs_fd *fd) rt_pipe_t *pipe; pipe = (rt_pipe_t *)fd->fnode->data; - if (!pipe) return -1; - - device = &(pipe->parent); - rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER); - - switch (fd->fnode->flags & O_ACCMODE) + if (!pipe) { - case O_RDONLY: - pipe->readers--; - break; - case O_WRONLY: - pipe->writers--; - break; - case O_RDWR: - pipe->readers--; - pipe->writers--; - break; - } - - if (pipe->writers == 0) - { - rt_wqueue_wakeup(&(pipe->reader_queue), (void*)(POLLIN | POLLERR | POLLHUP)); + return -1; } - if (pipe->readers == 0) - { - rt_wqueue_wakeup(&(pipe->writer_queue), (void*)(POLLOUT | POLLERR | POLLHUP)); - } + device = &pipe->parent; + rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER); - if (device->ref_count == 1) + if (fd->fnode->ref_count == 1) { if (pipe->fifo != RT_NULL) + { rt_ringbuffer_destroy(pipe->fifo); + } pipe->fifo = RT_NULL; } - device->ref_count--; - rt_mutex_release(&(pipe->lock)); + rt_mutex_release(&pipe->lock); - if (device->ref_count == 0 && pipe->is_named == RT_FALSE) + if (fd->fnode->ref_count == 1 && pipe->is_named == RT_FALSE) { /* delete the unamed pipe */ rt_pipe_delete(device->parent.name); @@ -145,18 +111,10 @@ static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count) pipe = (rt_pipe_t *)fd->fnode->data; /* no process has the pipe open for writing, return end-of-file */ - if (pipe->writers == 0) - return 0; - - rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER); + rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER); while (1) { - if (pipe->writers == 0) - { - goto out; - } - len = rt_ringbuffer_get(pipe->fifo, buf, count); if (len > 0) @@ -165,21 +123,21 @@ static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count) } else { - if (fd->fnode->flags & O_NONBLOCK) + if (fd->flags & O_NONBLOCK) { len = -EAGAIN; goto out; } rt_mutex_release(&pipe->lock); - rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT); - rt_wqueue_wait(&(pipe->reader_queue), 0, -1); - rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER); + rt_wqueue_wakeup(&pipe->writer_queue, (void*)POLLOUT); + rt_wqueue_wait(&pipe->reader_queue, 0, -1); + rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER); } } /* wakeup writer */ - rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT); + rt_wqueue_wakeup(&pipe->writer_queue, (void*)POLLOUT); out: rt_mutex_release(&pipe->lock); @@ -196,27 +154,16 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count) pipe = (rt_pipe_t *)fd->fnode->data; - if (pipe->readers == 0) - { - ret = -EPIPE; - goto out; - } - if (count == 0) + { return 0; + } pbuf = (uint8_t*)buf; rt_mutex_take(&pipe->lock, -1); while (1) { - if (pipe->readers == 0) - { - if (ret == 0) - ret = -EPIPE; - break; - } - len = rt_ringbuffer_put(pipe->fifo, pbuf, count - ret); ret += len; pbuf += len; @@ -228,7 +175,7 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count) } else { - if (fd->fnode->flags & O_NONBLOCK) + if (fd->flags & O_NONBLOCK) { if (ret == 0) { @@ -240,19 +187,18 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count) } rt_mutex_release(&pipe->lock); - rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN); + rt_wqueue_wakeup(&pipe->reader_queue, (void*)POLLIN); /* pipe full, waiting on suspended write list */ - rt_wqueue_wait(&(pipe->writer_queue), 0, -1); + rt_wqueue_wait(&pipe->writer_queue, 0, -1); rt_mutex_take(&pipe->lock, -1); } rt_mutex_release(&pipe->lock); if (wakeup) { - rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN); + rt_wqueue_wakeup(&pipe->reader_queue, (void*)POLLIN); } -out: return ret; } @@ -263,10 +209,10 @@ static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req) int mode = 0; pipe = (rt_pipe_t *)fd->fnode->data; - rt_poll_add(&(pipe->reader_queue), req); - rt_poll_add(&(pipe->writer_queue), req); + rt_poll_add(&pipe->reader_queue, req); + rt_poll_add(&pipe->writer_queue, req); - switch (fd->fnode->flags & O_ACCMODE) + switch (fd->flags & O_ACCMODE) { case O_RDONLY: mode = 1; @@ -285,10 +231,6 @@ static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req) { mask |= POLLIN; } - if (pipe->writers == 0) - { - mask |= POLLHUP; - } } if (mode & 2) @@ -297,10 +239,6 @@ static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req) { mask |= POLLOUT; } - if (pipe->readers == 0) - { - mask |= POLLERR; - } } return mask; @@ -330,8 +268,8 @@ rt_err_t rt_pipe_open (rt_device_t device, rt_uint16_t oflag) ret = -RT_EINVAL; goto __exit; } - - rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER); + + rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER); if (pipe->fifo == RT_NULL) { @@ -342,7 +280,7 @@ rt_err_t rt_pipe_open (rt_device_t device, rt_uint16_t oflag) } } - rt_mutex_release(&(pipe->lock)); + rt_mutex_release(&pipe->lock); __exit: return ret; @@ -352,16 +290,16 @@ rt_err_t rt_pipe_close (rt_device_t device) { rt_pipe_t *pipe = (rt_pipe_t *)device; - if (device == RT_NULL) return -RT_EINVAL; - rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER); - - if (device->ref_count == 1) + if (device == RT_NULL) { - rt_ringbuffer_destroy(pipe->fifo); - pipe->fifo = RT_NULL; + return -RT_EINVAL; } + rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER); - rt_mutex_release(&(pipe->lock)); + rt_ringbuffer_destroy(pipe->fifo); + pipe->fifo = RT_NULL; + + rt_mutex_release(&pipe->lock); return RT_EOK; } @@ -377,15 +315,21 @@ rt_size_t rt_pipe_read (rt_device_t device, rt_off_t pos, void *buffer, rt_siz rt_set_errno(-EINVAL); return 0; } - if (count == 0) return 0; + if (count == 0) + { + return 0; + } pbuf = (uint8_t*)buffer; - rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER); + rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER); while (read_bytes < count) { int len = rt_ringbuffer_get(pipe->fifo, &pbuf[read_bytes], count - read_bytes); - if (len <= 0) break; + if (len <= 0) + { + break; + } read_bytes += len; } @@ -405,7 +349,10 @@ rt_size_t rt_pipe_write (rt_device_t device, rt_off_t pos, const void *buffer, rt_set_errno(-EINVAL); return 0; } - if (count == 0) return 0; + if (count == 0) + { + return 0; + } pbuf = (uint8_t*)buffer; rt_mutex_take(&pipe->lock, -1); @@ -413,7 +360,10 @@ rt_size_t rt_pipe_write (rt_device_t device, rt_off_t pos, const void *buffer, while (write_bytes < count) { int len = rt_ringbuffer_put(pipe->fifo, &pbuf[write_bytes], count - write_bytes); - if (len <= 0) break; + if (len <= 0) + { + break; + } write_bytes += len; } @@ -449,14 +399,14 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz) rt_memset(pipe, 0, sizeof(rt_pipe_t)); pipe->is_named = RT_TRUE; /* initialize as a named pipe */ - rt_mutex_init(&(pipe->lock), name, RT_IPC_FLAG_FIFO); - rt_wqueue_init(&(pipe->reader_queue)); - rt_wqueue_init(&(pipe->writer_queue)); + rt_mutex_init(&pipe->lock, name, RT_IPC_FLAG_FIFO); + rt_wqueue_init(&pipe->reader_queue); + rt_wqueue_init(&pipe->writer_queue); RT_ASSERT(bufsz < 0xFFFF); pipe->bufsz = bufsz; - dev = &(pipe->parent); + dev = &pipe->parent; dev->type = RT_Device_Class_Pipe; #ifdef RT_USING_DEVICE_OPS dev->ops = &pipe_ops; @@ -472,7 +422,7 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz) dev->rx_indicate = RT_NULL; dev->tx_complete = RT_NULL; - if (rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE) != 0) + if (rt_device_register(&pipe->parent, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE) != 0) { rt_free(pipe); return RT_NULL; @@ -496,18 +446,13 @@ int rt_pipe_delete(const char *name) { rt_pipe_t *pipe; - if (device->ref_count != 0) - { - return -RT_EBUSY; - } - pipe = (rt_pipe_t *)device; - rt_mutex_detach(&(pipe->lock)); + rt_mutex_detach(&pipe->lock); rt_device_unregister(device); /* close fifo ringbuffer */ - if (pipe->fifo) + if (pipe->fifo) { rt_ringbuffer_destroy(pipe->fifo); pipe->fifo = RT_NULL; diff --git a/components/lwp/lwp_console.c b/components/lwp/lwp_console.c index ec3ca787a4644d972523262f5153be4d40e220ea..f1ffdb3609807bf3ab3379949e9f0ad1b52dc3a5 100644 --- a/components/lwp/lwp_console.c +++ b/components/lwp/lwp_console.c @@ -219,7 +219,7 @@ static int console_fops_open(struct dfs_fd *fd) if (fd->fnode->ref_count == 1) { - ret = rt_device_open(device, fd->fnode->flags); + ret = rt_device_open(device, fd->flags); } return ret; } @@ -264,7 +264,7 @@ static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count) { break; } - if (fd->fnode->flags & O_NONBLOCK) + if (fd->flags & O_NONBLOCK) { break; } diff --git a/components/lwp/lwp_ipc.c b/components/lwp/lwp_ipc.c index c6141b42b8ac15b2fb6e192f0ce58f2e4c14d25b..ea96db71c0cd0b90366142f951d3d2f86386aab8 100644 --- a/components/lwp/lwp_ipc.c +++ b/components/lwp/lwp_ipc.c @@ -926,7 +926,7 @@ int lwp_channel_open(int fdt_type, const char *name, int flags) d->fnode->fops = &channel_fops; - d->fnode->flags = O_RDWR; /* set flags as read and write */ + d->flags = O_RDWR; /* set flags as read and write */ d->fnode->size = 0; d->pos = 0; d->fnode->ref_count = 1; diff --git a/components/net/sal_socket/socket/net_sockets.c b/components/net/sal_socket/socket/net_sockets.c index 6fa8569a663fd538c9db79ab1c09cfb152dd4691..9f97d75cfb34aec00f62b1d3d831abba55ccdbe3 100644 --- a/components/net/sal_socket/socket/net_sockets.c +++ b/components/net/sal_socket/socket/net_sockets.c @@ -57,7 +57,7 @@ int accept(int s, struct sockaddr *addr, socklen_t *addrlen) d->fnode->fullpath = NULL; d->fnode->ref_count = 1; d->fnode->fops = dfs_net_get_fops(); - d->fnode->flags = O_RDWR; /* set flags as read and write */ + d->flags = O_RDWR; /* set flags as read and write */ d->fnode->size = 0; d->pos = 0; @@ -238,7 +238,7 @@ int socket(int domain, int type, int protocol) d->fnode->ref_count = 1; d->fnode->fops = dfs_net_get_fops(); - d->fnode->flags = O_RDWR; /* set flags as read and write */ + d->flags = O_RDWR; /* set flags as read and write */ d->fnode->size = 0; d->pos = 0;