Skip to content

Commit 6aa0777

Browse files
authored
Merge pull request #3105 from zhangjun1996/master
[components]添加ftruncate、flock、getuid、umask接口
2 parents 67bb485 + d08f533 commit 6aa0777

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

components/dfs/filesystems/elmfat/dfs_elm.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,33 @@ int dfs_elm_close(struct dfs_fd *file)
478478

479479
int dfs_elm_ioctl(struct dfs_fd *file, int cmd, void *args)
480480
{
481+
switch (cmd)
482+
{
483+
case RT_FIOFTRUNCATE:
484+
{
485+
FIL *fd;
486+
FSIZE_t fptr, length;
487+
FRESULT result = FR_OK;
488+
fd = (FIL *)(file->data);
489+
RT_ASSERT(fd != RT_NULL);
490+
491+
/* save file read/write point */
492+
fptr = fd->fptr;
493+
length = *(off_t*)args;
494+
if (length <= fd->obj.objsize)
495+
{
496+
fd->fptr = length;
497+
result = f_truncate(fd);
498+
}
499+
else
500+
{
501+
result = f_lseek(fd, length);
502+
}
503+
/* restore file read/write point */
504+
fd->fptr = fptr;
505+
return elm_result_to_dfs(result);
506+
}
507+
}
481508
return -ENOSYS;
482509
}
483510

components/dfs/include/dfs_file.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ int dfs_file_lseek(struct dfs_fd *fd, off_t offset);
6666

6767
int dfs_file_stat(const char *path, struct stat *buf);
6868
int dfs_file_rename(const char *oldpath, const char *newpath);
69+
int dfs_file_ftruncate(struct dfs_fd *fd, off_t length);
70+
71+
/* 0x5254 is just a magic number to make these relatively unique ("RT") */
72+
#define RT_FIOFTRUNCATE 0x52540000U
6973

7074
#ifdef __cplusplus
7175
}

components/dfs/include/dfs_posix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ int fstat(int fildes, struct stat *buf);
5858
int fsync(int fildes);
5959
int fcntl(int fildes, int cmd, ...);
6060
int ioctl(int fildes, int cmd, ...);
61+
int ftruncate(int fd, off_t length);
6162

6263
/* directory api*/
6364
int rmdir(const char *path);

components/dfs/src/dfs_file.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,35 @@ int dfs_file_rename(const char *oldpath, const char *newpath)
482482
return result;
483483
}
484484

485+
/**
486+
* this function is will cause the regular file referenced by fd
487+
* to be truncated to a size of precisely length bytes.
488+
*
489+
* @param fd the file descriptor.
490+
* @param length the length to be truncated.
491+
*
492+
* @return the status of truncated.
493+
*/
494+
int dfs_file_ftruncate(struct dfs_fd *fd, off_t length)
495+
{
496+
int result;
497+
498+
/* fd is null or not a regular file system fd, or length is invalid */
499+
if (fd == NULL || fd->type != FT_REGULAR || length < 0)
500+
return -EINVAL;
501+
502+
if (fd->fops->ioctl == NULL)
503+
return -ENOSYS;
504+
505+
result = fd->fops->ioctl(fd, RT_FIOFTRUNCATE, (void*)&length);
506+
507+
/* update current size */
508+
if (result == 0)
509+
fd->size = length;
510+
511+
return result;
512+
}
513+
485514
#ifdef RT_USING_FINSH
486515
#include <finsh.h>
487516

components/dfs/src/dfs_posix.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,52 @@ int ioctl(int fildes, int cmd, ...)
471471
}
472472
RTM_EXPORT(ioctl);
473473

474+
/**
475+
*
476+
* this function is a POSIX compliant version, which cause the regular file
477+
* referenced by fd to be truncated to a size of precisely length bytes.
478+
* @param fd the file descriptor.
479+
* @param length the length to be truncated.
480+
*
481+
* @return Upon successful completion, ftruncate() shall return 0;
482+
* otherwise, -1 shall be returned and errno set to indicate the error.
483+
*/
484+
int ftruncate(int fd, off_t length)
485+
{
486+
int result;
487+
struct dfs_fd *d;
488+
489+
d = fd_get(fd);
490+
if (d == NULL)
491+
{
492+
rt_set_errno(-EBADF);
493+
494+
return -1;
495+
}
496+
497+
if (length < 0)
498+
{
499+
fd_put(d);
500+
rt_set_errno(-EINVAL);
501+
502+
return -1;
503+
}
504+
result = dfs_file_ftruncate(d, length);
505+
if (result < 0)
506+
{
507+
fd_put(d);
508+
rt_set_errno(result);
509+
510+
return -1;
511+
}
512+
513+
/* release the ref-count of fd */
514+
fd_put(d);
515+
516+
return 0;
517+
}
518+
RTM_EXPORT(ftruncate);
519+
474520
/**
475521
* this function is a POSIX compliant version, which will return the
476522
* information about a mounted file system.

components/libc/compilers/newlib/syscalls.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,18 @@ void abort(void)
442442

443443
while (1);
444444
}
445+
446+
uid_t getuid(void)
447+
{
448+
return 0;
449+
}
450+
451+
mode_t umask(mode_t mask)
452+
{
453+
return 022;
454+
}
455+
456+
int flock(int fd, int operation)
457+
{
458+
return 0;
459+
}

0 commit comments

Comments
 (0)