Skip to content

Commit

Permalink
fs/inode: add common function to get file count from list
Browse files Browse the repository at this point in the history
common function to get file count from file list

Signed-off-by: chao an <[email protected]>
  • Loading branch information
anchao committed Nov 12, 2023
1 parent 0e25e9a commit 0e6526f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 40 deletions.
35 changes: 30 additions & 5 deletions fs/inode/fs_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static int files_extend(FAR struct filelist *list, size_t row)
return 0;
}

if (row * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK > OPEN_MAX)
if (files_countlist(list) > OPEN_MAX)
{
return -EMFILE;
}
Expand Down Expand Up @@ -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 file *filep;
FAR struct file file;
int count;
int ret;

if (fd1 == fd2)
Expand All @@ -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);

count = files_countlist(list);

/* 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)
{
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);
if (ret < 0)
Expand Down Expand Up @@ -315,6 +318,28 @@ void files_releaselist(FAR struct filelist *list)
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
*
Expand Down Expand Up @@ -562,7 +587,7 @@ int fs_getfilep(int fd, FAR struct file **filep)
return -EAGAIN;
}

if (fd < 0 || fd >= list->fl_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK)
if (fd < 0 || fd >= files_countlist(list))
{
return -EBADF;
}
Expand Down Expand Up @@ -716,7 +741,7 @@ int nx_close_from_tcb(FAR struct tcb_s *tcb, int fd)

/* If the file was properly opened, there should be an inode assigned */

if (fd < 0 || fd >= list->fl_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK ||
if (fd < 0 || fd >= files_countlist(list) ||
files_fget(list, fd)->f_inode == NULL)
{
return -EBADF;
Expand Down
70 changes: 35 additions & 35 deletions fs/procfs/fs_procfsproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1188,17 +1188,24 @@ static ssize_t proc_groupfd(FAR struct proc_file_s *procfile,
size_t buflen, off_t offset)
{
FAR struct task_group_s *group = tcb->group;
FAR struct file *file;
FAR struct file *filep;
char path[PATH_MAX];
size_t remaining;
size_t linesize;
size_t copysize;
size_t totalsize;
int count;
int ret;
int i;
int j;

DEBUGASSERT(group != NULL);

count = files_countlist(&group->tg_filelist);
if (count == 0)
{
return 0;
}

remaining = buflen;
totalsize = 0;

Expand All @@ -1219,41 +1226,34 @@ static ssize_t proc_groupfd(FAR struct proc_file_s *procfile,

/* 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];
j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK;
j++, file++)
ret = fs_getfilep(i, &filep);
if (ret != OK || filep == NULL)
{
continue;
}

if (file_ioctl(filep, FIOC_FILEPATH, path) < 0)
{
path[0] = '\0';
}

linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-3d %-7d %-4x %-9ld %s\n",
i, filep->f_oflags,
INODE_GET_TYPE(filep->f_inode),
(long)filep->f_pos, path);
copysize = procfs_memcpy(procfile->line, linesize,
buffer, remaining, &offset);

totalsize += copysize;
buffer += copysize;
remaining -= copysize;

if (totalsize >= buflen)
{
/* Is there an inode associated with the file descriptor? */

if (file->f_inode == NULL)
{
continue;
}

if (file_ioctl(file, FIOC_FILEPATH, path) < 0)
{
path[0] = '\0';
}

linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-3d %-7d %-4x %-9ld %s\n",
i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK
+ 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;
buffer += copysize;
remaining -= copysize;

if (totalsize >= buflen)
{
return totalsize;
}
return totalsize;
}
}

Expand Down
13 changes: 13 additions & 0 deletions include/nuttx/fs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,19 @@ void files_initlist(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
*
Expand Down

0 comments on commit 0e6526f

Please sign in to comment.