fs/inode: add common function to get file count from list
common function to get file count from file list Signed-off-by: chao an <anchao@xiaomi.com>
This commit is contained in:
parent
0a567bbae4
commit
3b2c585ab7
@ -98,7 +98,7 @@ static int files_extend(FAR struct filelist *list, size_t row)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (row * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK > OPEN_MAX)
|
if (files_countlist(list) > OPEN_MAX)
|
||||||
{
|
{
|
||||||
return -EMFILE;
|
return -EMFILE;
|
||||||
}
|
}
|
||||||
@ -214,6 +214,7 @@ static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2,
|
|||||||
FAR struct filelist *list;
|
FAR struct filelist *list;
|
||||||
FAR struct file *filep;
|
FAR struct file *filep;
|
||||||
FAR struct file file;
|
FAR struct file file;
|
||||||
|
int count;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (fd1 == fd2)
|
if (fd1 == fd2)
|
||||||
@ -228,15 +229,17 @@ static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2,
|
|||||||
|
|
||||||
list = nxsched_get_files_from_tcb(tcb);
|
list = nxsched_get_files_from_tcb(tcb);
|
||||||
|
|
||||||
|
count = files_countlist(list);
|
||||||
|
|
||||||
/* Get the file descriptor list. It should not be NULL in this context. */
|
/* Get the file descriptor list. It should not be NULL in this context. */
|
||||||
|
|
||||||
if (fd1 < 0 || fd1 >= CONFIG_NFILE_DESCRIPTORS_PER_BLOCK * list->fl_rows ||
|
if (fd1 < 0 || fd1 >= count ||
|
||||||
fd2 < 0)
|
fd2 < 0)
|
||||||
{
|
{
|
||||||
return -EBADF;
|
return -EBADF;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fd2 >= CONFIG_NFILE_DESCRIPTORS_PER_BLOCK * list->fl_rows)
|
if (fd2 >= count)
|
||||||
{
|
{
|
||||||
ret = files_extend(list, fd2 / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + 1);
|
ret = files_extend(list, fd2 / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + 1);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
@ -315,6 +318,28 @@ void files_releaselist(FAR struct filelist *list)
|
|||||||
kmm_free(list->fl_files);
|
kmm_free(list->fl_files);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: files_countlist
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Given a file descriptor, return the corresponding instance of struct
|
||||||
|
* file.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* fd - The file descriptor
|
||||||
|
* filep - The location to return the struct file instance
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
||||||
|
* any failure.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int files_countlist(FAR struct filelist *list)
|
||||||
|
{
|
||||||
|
return list->fl_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK;
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: file_allocate_from_tcb
|
* Name: file_allocate_from_tcb
|
||||||
*
|
*
|
||||||
@ -562,7 +587,7 @@ int fs_getfilep(int fd, FAR struct file **filep)
|
|||||||
return -EAGAIN;
|
return -EAGAIN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fd < 0 || fd >= list->fl_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK)
|
if (fd < 0 || fd >= files_countlist(list))
|
||||||
{
|
{
|
||||||
return -EBADF;
|
return -EBADF;
|
||||||
}
|
}
|
||||||
@ -714,7 +739,7 @@ int nx_close_from_tcb(FAR struct tcb_s *tcb, int fd)
|
|||||||
|
|
||||||
/* Perform the protected close operation */
|
/* Perform the protected close operation */
|
||||||
|
|
||||||
if (fd < 0 || fd >= list->fl_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK)
|
if (fd < 0 || fd >= files_countlist(list))
|
||||||
{
|
{
|
||||||
return -EBADF;
|
return -EBADF;
|
||||||
}
|
}
|
||||||
|
@ -1188,17 +1188,24 @@ static ssize_t proc_groupfd(FAR struct proc_file_s *procfile,
|
|||||||
size_t buflen, off_t offset)
|
size_t buflen, off_t offset)
|
||||||
{
|
{
|
||||||
FAR struct task_group_s *group = tcb->group;
|
FAR struct task_group_s *group = tcb->group;
|
||||||
FAR struct file *file;
|
FAR struct file *filep;
|
||||||
char path[PATH_MAX];
|
char path[PATH_MAX];
|
||||||
size_t remaining;
|
size_t remaining;
|
||||||
size_t linesize;
|
size_t linesize;
|
||||||
size_t copysize;
|
size_t copysize;
|
||||||
size_t totalsize;
|
size_t totalsize;
|
||||||
|
int count;
|
||||||
|
int ret;
|
||||||
int i;
|
int i;
|
||||||
int j;
|
|
||||||
|
|
||||||
DEBUGASSERT(group != NULL);
|
DEBUGASSERT(group != NULL);
|
||||||
|
|
||||||
|
count = files_countlist(&group->tg_filelist);
|
||||||
|
if (count == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
remaining = buflen;
|
remaining = buflen;
|
||||||
totalsize = 0;
|
totalsize = 0;
|
||||||
|
|
||||||
@ -1219,41 +1226,34 @@ static ssize_t proc_groupfd(FAR struct proc_file_s *procfile,
|
|||||||
|
|
||||||
/* Examine each open file descriptor */
|
/* Examine each open file descriptor */
|
||||||
|
|
||||||
for (i = 0; i < group->tg_filelist.fl_rows; i++)
|
for (i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
for (j = 0, file = group->tg_filelist.fl_files[i];
|
ret = fs_getfilep(i, &filep);
|
||||||
j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK;
|
if (ret != OK || filep == NULL)
|
||||||
j++, file++)
|
|
||||||
{
|
{
|
||||||
/* Is there an inode associated with the file descriptor? */
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (file->f_inode == NULL)
|
if (file_ioctl(filep, FIOC_FILEPATH, path) < 0)
|
||||||
{
|
{
|
||||||
continue;
|
path[0] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file_ioctl(file, FIOC_FILEPATH, path) < 0)
|
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
|
||||||
{
|
"%-3d %-7d %-4x %-9ld %s\n",
|
||||||
path[0] = '\0';
|
i, filep->f_oflags,
|
||||||
}
|
INODE_GET_TYPE(filep->f_inode),
|
||||||
|
(long)filep->f_pos, path);
|
||||||
|
copysize = procfs_memcpy(procfile->line, linesize,
|
||||||
|
buffer, remaining, &offset);
|
||||||
|
|
||||||
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
|
totalsize += copysize;
|
||||||
"%-3d %-7d %-4x %-9ld %s\n",
|
buffer += copysize;
|
||||||
i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK
|
remaining -= copysize;
|
||||||
+ j, file->f_oflags,
|
|
||||||
INODE_GET_TYPE(file->f_inode),
|
|
||||||
(long)file->f_pos, path);
|
|
||||||
copysize = procfs_memcpy(procfile->line, linesize,
|
|
||||||
buffer, remaining, &offset);
|
|
||||||
|
|
||||||
totalsize += copysize;
|
if (totalsize >= buflen)
|
||||||
buffer += copysize;
|
{
|
||||||
remaining -= copysize;
|
return totalsize;
|
||||||
|
|
||||||
if (totalsize >= buflen)
|
|
||||||
{
|
|
||||||
return totalsize;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -852,6 +852,19 @@ void files_initlist(FAR struct filelist *list);
|
|||||||
|
|
||||||
void files_releaselist(FAR struct filelist *list);
|
void files_releaselist(FAR struct filelist *list);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: files_countlist
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Get file count from file list
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* file count of file list
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int files_countlist(FAR struct filelist *list);
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: files_duplist
|
* Name: files_duplist
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user