Skip to content

Commit 1cf97d8

Browse files
GorrayLimysterywolf
authored andcommitted
[components][dfs]add some comments and fix some former comments error for dfs v1.
1 parent 294e441 commit 1cf97d8

File tree

3 files changed

+169
-17
lines changed

3 files changed

+169
-17
lines changed

components/dfs/dfs_v1/src/dfs.c

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2022, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -107,7 +107,8 @@ int dfs_init(void)
107107
INIT_PREV_EXPORT(dfs_init);
108108

109109
/**
110-
* this function will lock device file system.
110+
* @brief this function will lock device file system.
111+
* this lock (fslock) is used for protecting filesystem_operation_table and filesystem_table.
111112
*
112113
* @note please don't invoke it on ISR.
113114
*/
@@ -126,6 +127,12 @@ void dfs_lock(void)
126127
}
127128
}
128129

130+
/**
131+
* @brief this function will lock file descriptors.
132+
* this lock (fdlock) is used for protecting fd table (_fdtab).
133+
*
134+
* @note please don't invoke it on ISR.
135+
*/
129136
void dfs_file_lock(void)
130137
{
131138
rt_err_t result = -RT_EBUSY;
@@ -142,7 +149,7 @@ void dfs_file_lock(void)
142149
}
143150

144151
/**
145-
* this function will lock device file system.
152+
* @brief this function will unlock device file system.
146153
*
147154
* @note please don't invoke it on ISR.
148155
*/
@@ -151,33 +158,56 @@ void dfs_unlock(void)
151158
rt_mutex_release(&fslock);
152159
}
153160

154-
#ifdef DFS_USING_POSIX
155-
161+
/**
162+
* @brief this function will unlock fd table.
163+
*/
156164
void dfs_file_unlock(void)
157165
{
158166
rt_mutex_release(&fdlock);
159167
}
160168

169+
#ifdef DFS_USING_POSIX
170+
/**
171+
* @brief Expand the file descriptor table to accommodate a specific file descriptor.
172+
*
173+
* This function ensures that the file descriptor table in the given `dfs_fdtable` structure
174+
* has sufficient capacity to include the specified file descriptor `fd`. If the table
175+
* needs to be expanded, it reallocates memory and initializes new slots to `NULL`.
176+
*
177+
* @param fdt Pointer to the `dfs_fdtable` structure representing the file descriptor table.
178+
* @param fd The file descriptor that the table must accommodate.
179+
* @return int
180+
* - The input file descriptor `fd` if it is within the current or newly expanded table's capacity.
181+
* - `-1` if the requested file descriptor exceeds `DFS_FD_MAX` or memory allocation fails.
182+
*/
161183
static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
162184
{
163185
int nr;
164186
int index;
165187
struct dfs_file **fds = NULL;
166188

189+
/* If the file descriptor is already within the current capacity, no expansion is needed.*/
167190
if (fd < fdt->maxfd)
168191
{
169192
return fd;
170193
}
194+
195+
/* If the file descriptor exceeds the maximum allowable limit, return an error.*/
171196
if (fd >= DFS_FD_MAX)
172197
{
173198
return -1;
174199
}
175200

201+
/* Calculate the new capacity, rounding up to the nearest multiple of 4.*/
176202
nr = ((fd + 4) & ~3);
203+
204+
/* Ensure the new capacity does not exceed the maximum limit.*/
177205
if (nr > DFS_FD_MAX)
178206
{
179207
nr = DFS_FD_MAX;
180208
}
209+
210+
/* Attempt to reallocate the file descriptor table to the new capacity.*/
181211
fds = (struct dfs_file **)rt_realloc(fdt->fds, nr * sizeof(struct dfs_file *));
182212
if (!fds)
183213
{
@@ -189,12 +219,23 @@ static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
189219
{
190220
fds[index] = NULL;
191221
}
222+
223+
/* Update the file descriptor table and its capacity.*/
192224
fdt->fds = fds;
193225
fdt->maxfd = nr;
194226

195227
return fd;
196228
}
197229

230+
/**
231+
* @brief Allocate a file descriptor slot starting from a specified index.
232+
*
233+
* @param fdt fdt Pointer to the `dfs_fdtable` structure representing the file descriptor table.
234+
* @param startfd The starting index for the search for an empty slot.
235+
* @return int
236+
* - The index of the first available slot if successful.
237+
* - `-1` if no slot is available or if table expansion fails
238+
*/
198239
static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
199240
{
200241
int idx;
@@ -219,6 +260,17 @@ static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
219260
}
220261
return idx;
221262
}
263+
264+
/**
265+
* @brief Allocate a new file descriptor and associate it with a newly allocated `struct dfs_file`.
266+
*
267+
* @param fdt Pointer to the `dfs_fdtable` structure representing the file descriptor table.
268+
* @param startfd The starting index for searching an available file descriptor slot.
269+
*
270+
* @return
271+
* - The index of the allocated file descriptor if successful.
272+
* - `-1` if no slot is available or memory allocation fails.
273+
*/
222274
static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
223275
{
224276
int idx;
@@ -323,7 +375,11 @@ struct dfs_file *fd_get(int fd)
323375
/**
324376
* @ingroup Fd
325377
*
326-
* This function will put the file descriptor.
378+
* @brief This function will release the file descriptor.
379+
*
380+
* This function releases a file descriptor slot in the file descriptor table, decrements reference
381+
* counts, and cleans up resources associated with the `dfs_file` and `dfs_vnode` structures when applicable.
382+
*
327383
*/
328384
void fdt_fd_release(struct dfs_fdtable* fdt, int fd)
329385
{
@@ -378,6 +434,20 @@ void fd_release(int fd)
378434
fdt_fd_release(fdt, fd);
379435
}
380436

437+
/**
438+
* @brief Duplicates a file descriptor.
439+
*
440+
* This function duplicates an existing file descriptor (`oldfd`) and returns
441+
* a new file descriptor that refers to the same underlying file object.
442+
*
443+
* @param oldfd The file descriptor to duplicate. It must be a valid file
444+
* descriptor within the range of allocated descriptors.
445+
*
446+
* @return The new file descriptor if successful, or a negative value
447+
* (e.g., -1) if an error occurs.
448+
*
449+
* @see sys_dup2()
450+
*/
381451
rt_err_t sys_dup(int oldfd)
382452
{
383453
int newfd = -1;
@@ -470,6 +540,23 @@ int fd_is_open(const char *pathname)
470540
return -1;
471541
}
472542

543+
/**
544+
* @brief Duplicates a file descriptor to a specified file descriptor.
545+
*
546+
* This function duplicates an existing file descriptor (`oldfd`) and assigns it
547+
* to the specified file descriptor (`newfd`).
548+
*
549+
* @param oldfd The file descriptor to duplicate. It must be a valid and open file
550+
* descriptor within the range of allocated descriptors.
551+
* @param newfd The target file descriptor. If `newfd` is already in use, it will
552+
* be closed before duplication. If `newfd` exceeds the current file
553+
* descriptor table size, the table will be expanded to accommodate it.
554+
*
555+
* @return The value of `newfd` on success, or a negative value (e.g., -1) if an
556+
* error occurs.
557+
*
558+
* @see sys_dup()
559+
*/
473560
rt_err_t sys_dup2(int oldfd, int newfd)
474561
{
475562
struct dfs_fdtable *fdt = NULL;
@@ -550,6 +637,10 @@ static int fd_get_fd_index_form_fdt(struct dfs_fdtable *fdt, struct dfs_file *fi
550637
return fd;
551638
}
552639

640+
/**
641+
* @brief get fd (index) by dfs file object.
642+
*
643+
*/
553644
int fd_get_fd_index(struct dfs_file *file)
554645
{
555646
struct dfs_fdtable *fdt;
@@ -558,6 +649,21 @@ int fd_get_fd_index(struct dfs_file *file)
558649
return fd_get_fd_index_form_fdt(fdt, file);
559650
}
560651

652+
/**
653+
* @brief Associates a file descriptor with a file object.
654+
*
655+
* This function associates a given file descriptor (`fd`) with a specified
656+
* file object (`file`) in the file descriptor table (`fdt`).
657+
*
658+
* @param fdt The file descriptor table to operate on. It must be a valid
659+
* and initialized `dfs_fdtable` structure.
660+
* @param fd The file descriptor to associate. It must be within the range
661+
* of allocated file descriptors and currently unoccupied.
662+
* @param file The file object to associate with the file descriptor. It must
663+
* be a valid and initialized `dfs_file` structure.
664+
*
665+
* @return The value of `fd` on success, or -1 if an error occurs.
666+
*/
561667
int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_file *file)
562668
{
563669
int retfd = -1;
@@ -591,6 +697,10 @@ int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_file *file)
591697
return retfd;
592698
}
593699

700+
/**
701+
* @brief initialize a dfs file object.
702+
*
703+
*/
594704
void fd_init(struct dfs_file *fd)
595705
{
596706
if (fd)

components/dfs/dfs_v1/src/dfs_file.c

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -18,10 +18,12 @@
1818

1919
#define DFS_VNODE_HASH_NR 128
2020

21+
/*dfs vnode manager, for saving and searching vnodes.*/
2122
struct dfs_vnode_mgr
2223
{
23-
struct rt_mutex lock;
24-
rt_list_t head[DFS_VNODE_HASH_NR];
24+
struct rt_mutex lock; /* mutex for protecting dfs vnode lists */
25+
rt_list_t head[DFS_VNODE_HASH_NR]; /* a group of dfs vnode lists, the dfs vnode is inserted to one of the lists
26+
according to path string's hash-value mod DFS_VNODE_HASH_NR. */
2527
};
2628

2729
static struct dfs_vnode_mgr dfs_fm;
@@ -36,6 +38,10 @@ void dfs_fm_unlock(void)
3638
rt_mutex_release(&dfs_fm.lock);
3739
}
3840

41+
/**
42+
* @brief Initialize dfs vnode manager structure, including a lock and hash tables for vnode.
43+
*
44+
*/
3945
void dfs_vnode_mgr_init(void)
4046
{
4147
int i = 0;
@@ -47,6 +53,23 @@ void dfs_vnode_mgr_init(void)
4753
}
4854
}
4955

56+
/**
57+
* @brief Initialize a DFS vnode structure.
58+
*
59+
* @param vnode Pointer to the DFS vnode structure to be initialized.
60+
* The caller must ensure this is a valid, allocated structure.
61+
* @param type The type of the vnode, representing its role or category (e.g., regular file, directory).
62+
* @param fops Pointer to the file operations structure associated with this vnode.
63+
* This structure defines the behavior of the vnode for operations such as open, read, write, etc.
64+
* If `fops` is NULL, the vnode will have no associated file operations.
65+
*
66+
* @return 0 on success, or a negative error code on failure.
67+
*
68+
* @note The caller should ensure that:
69+
* - The `vnode` pointer is valid and properly allocated.
70+
* - The `fops` pointer (if not NULL) points to a valid `struct dfs_file_ops`
71+
* instance, where all necessary function pointers are properly set.
72+
*/
5073
int dfs_vnode_init(struct dfs_vnode *vnode, int type, const struct dfs_file_ops *fops)
5174
{
5275
if (vnode)
@@ -64,7 +87,7 @@ int dfs_vnode_init(struct dfs_vnode *vnode, int type, const struct dfs_file_ops
6487
/* BKDR Hash Function */
6588
static unsigned int bkdr_hash(const char *str)
6689
{
67-
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
90+
unsigned int seed = 131; /* 31 131 1313 13131 131313 etc..*/
6891
unsigned int hash = 0;
6992

7093
while (*str)
@@ -75,6 +98,22 @@ static unsigned int bkdr_hash(const char *str)
7598
return (hash % DFS_VNODE_HASH_NR);
7699
}
77100

101+
/**
102+
* @brief Find a DFS vnode by its path.
103+
*
104+
* This function searches for a vnode in the vnode hash table using the specified path.
105+
* If found, it returns a pointer to the vnode and updates the hash head if required.
106+
*
107+
* @param path The file path to search for. This should be a valid null-terminated string.
108+
* @param hash_head Pointer to a location where the hash table head associated with the vnode
109+
* can be stored. This can be NULL if the hash head is not needed.
110+
*
111+
* @return Pointer to the DFS vnode if found, or NULL if no vnode matches the specified path.
112+
*
113+
* @note The caller must ensure that:
114+
* - The `path` pointer is valid and points to a properly null-terminated string.
115+
* - If `hash_head` is not NULL, it points to a valid location to store the hash head.
116+
*/
78117
static struct dfs_vnode *dfs_vnode_find(const char *path, rt_list_t **hash_head)
79118
{
80119
struct dfs_vnode *vnode = NULL;
@@ -329,11 +368,12 @@ int dfs_file_close(struct dfs_file *fd)
329368
}
330369

331370
/**
332-
* this function will perform a io control on a file descriptor.
371+
* this function will perform an io control on a file descriptor.
333372
*
334373
* @param fd the file descriptor.
335374
* @param cmd the command to send to file descriptor.
336375
* @param args the argument to send to file descriptor.
376+
* - When `cmd` is `F_SETFL`, an additional integer argument specifies the new status flags.
337377
*
338378
* @return 0 on successful, -1 on failed.
339379
*/
@@ -1026,14 +1066,14 @@ void copy(const char *src, const char *dst)
10261066
flag |= FLAG_DST_IS_FILE;
10271067
}
10281068

1029-
//2. check status
1069+
/*2. check status*/
10301070
if ((flag & FLAG_SRC_IS_DIR) && (flag & FLAG_DST_IS_FILE))
10311071
{
10321072
rt_kprintf("cp faild, cp dir to file is not permitted!\n");
10331073
return ;
10341074
}
10351075

1036-
//3. do copy
1076+
/*3. do copy*/
10371077
if (flag & FLAG_SRC_IS_FILE)
10381078
{
10391079
if (flag & FLAG_DST_IS_DIR)
@@ -1053,7 +1093,7 @@ void copy(const char *src, const char *dst)
10531093
copyfile(src, dst);
10541094
}
10551095
}
1056-
else //flag & FLAG_SRC_IS_DIR
1096+
else /*flag & FLAG_SRC_IS_DIR*/
10571097
{
10581098
if (flag & FLAG_DST_IS_DIR)
10591099
{

components/dfs/dfs_v1/src/dfs_fs.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -529,7 +529,8 @@ int dfs_mount_device(rt_device_t dev)
529529
{
530530
int index = 0;
531531

532-
if(dev == RT_NULL) {
532+
if(dev == RT_NULL)
533+
{
533534
rt_kprintf("the device is NULL to be mounted.\n");
534535
return -RT_ERROR;
535536
}
@@ -538,7 +539,8 @@ int dfs_mount_device(rt_device_t dev)
538539
{
539540
if (mount_table[index].path == NULL) break;
540541

541-
if(strcmp(mount_table[index].device_name, dev->parent.name) == 0) {
542+
if(strcmp(mount_table[index].device_name, dev->parent.name) == 0)
543+
{
542544
if (dfs_mount(mount_table[index].device_name,
543545
mount_table[index].path,
544546
mount_table[index].filesystemtype,

0 commit comments

Comments
 (0)