37#include "sys/select.h"
40#include "sys/statfs.h"
50#define MAX_DIR_ENT 1024
51int fstat(
int fd,
struct stat *buf)
53 struct file *filep = NULL;
55 int ret = fs_getfilep(fd, &filep);
60 return stat(filep->
f_path, buf);
65 struct file *filep = NULL;
67 int ret = fs_getfilep(fd, &filep);
72 return stat64(filep->
f_path, buf);
75int lstat(
const char *path,
struct stat *buffer)
77 return stat(path, buffer);
82 uint fuid = node->
uid;
83 uint fgid = node->
gid;
84 uint fileMode = node->
mode;
93 tmpMode >>= USER_MODE_SHIFT;
95 tmpMode >>= GROUP_MODE_SHIFT;
98 tmpMode &= (READ_OP | WRITE_OP | EXEC_OP);
100 if (((uint)accMode & tmpMode) == accMode) {
105 if (S_ISDIR(fileMode)) {
107 || (!((uint)accMode & WRITE_OP) &&
IsCapPermit(CAP_DAC_READ_SEARCH))) {
111 if (
IsCapPermit(CAP_DAC_EXECUTE) && (fileMode & MODE_IXUGO)) {
124 if (((uint)accMode & tmpMode) == accMode) {
131#ifdef VFS_USING_WORKDIR
139 ret = strncpy_s(curr->
files->
workdir, PATH_MAX, dir, len);
153 char *fullpath = NULL;
154 char *fullpath_bak = NULL;
155 struct stat statBuff;
168 if (strlen(path) > PATH_MAX) {
169 set_errno(ENAMETOOLONG);
178 fullpath_bak = fullpath;
179 ret = stat(fullpath, &statBuff);
185 if (!S_ISDIR(statBuff.st_mode)) {
191 if (
VfsPermissionCheck(statBuff.st_uid, statBuff.st_gid, statBuff.st_mode, EXEC_OP)) {
197#ifdef VFS_USING_WORKDIR
200 PRINT_ERR(
"chdir path error!\n");
224#ifdef VFS_USING_WORKDIR
234#ifdef VFS_USING_WORKDIR
244 set_errno(ENAMETOOLONG);
250 PRINT_ERR(
"NO_WORKING_DIR\n");
258 struct IATTR attr = {0};
263 ret =
chattr(path, &attr);
271int chown(
const char *pathname, uid_t owner, gid_t group)
273 struct IATTR attr = {0};
277 if (owner != (uid_t)-1) {
281 if (group != (gid_t)-1) {
285 ret =
chattr(pathname, &attr);
299 ret = statfs(path, &fsBuf);
301 if (get_errno() != ENOSYS) {
307 if ((fsBuf.f_flags & MS_RDONLY) && ((
unsigned int)amode & W_OK)) {
312 ret = stat(path, &buf);
328 int listSize = MAX_DIR_ENT;
330 struct dirent **list = NULL;
331 struct dirent **newList = NULL;
332 struct dirent *ent = NULL;
333 struct dirent *p = NULL;
341 list = (
struct dirent **)
malloc(listSize *
sizeof(
struct dirent *));
347 for (ent = readdir(od); ent != NULL; ent = readdir(od)) {
348 if (filter && !filter(ent)) {
353 listSize += MAX_DIR_ENT;
354 newList = (
struct dirent **)
malloc(listSize *
sizeof(
struct dirent *));
355 if (newList == NULL) {
359 err = memcpy_s(newList, listSize *
sizeof(
struct dirent *), list, n *
sizeof(
struct dirent *));
368 p = (
struct dirent *)
malloc(
sizeof(
struct dirent));
373 (
void)memcpy_s((
void *)p,
sizeof(
struct dirent), (
void *)ent,
sizeof(
struct dirent));
379 if (closedir(od) < 0) {
391int scandir(
const char *dir,
struct dirent ***namelist,
392 int(*filter)(
const struct dirent *),
393 int(*compar)(
const struct dirent **,
const struct dirent **))
396 struct dirent **list = NULL;
398 if ((dir == NULL) || (namelist == NULL)) {
408 *namelist = (
struct dirent **)
malloc(n *
sizeof(
struct dirent *));
409 if (*namelist == NULL && n > 0) {
411 }
else if (*namelist != NULL) {
412 (
void)memcpy_s(*namelist, n *
sizeof(
struct dirent *), list, n *
sizeof(
struct dirent *));
420 if (compar && *namelist) {
421 qsort((
void *)*namelist, (
size_t)n,
sizeof(
struct dirent *), (
int (*)(
const void *,
const void *))*compar);
427int alphasort(
const struct dirent **a,
const struct dirent **b)
429 return strcoll((*a)->d_name, (*b)->d_name);
439 return (
char *)strrchr(s, c);
444 char *fullpath = NULL;
447 if (path[1] !=
'\0') {
449 fullpath = (
char *)
malloc(strlen(path) + strlen(pdirent->d_name) + 2);
450 if (fullpath == NULL) {
451 goto exit_with_nomem;
455 ret = snprintf_s(fullpath, strlen(path) + strlen(pdirent->d_name) + 2,
456 strlen(path) + strlen(pdirent->d_name) + 1,
"%s/%s", path, pdirent->d_name);
459 set_errno(ENAMETOOLONG);
464 fullpath = (
char *)
malloc(strlen(pdirent->d_name) + 2);
465 if (fullpath == NULL) {
466 goto exit_with_nomem;
470 ret = snprintf_s(fullpath, strlen(pdirent->d_name) + 2, strlen(pdirent->d_name) + 1,
471 "/%s", pdirent->d_name);
474 set_errno(ENAMETOOLONG);
488 char str[UGO_NUMS][UGO_NUMS + 1] = {0};
492 for (i = 0; i < UGO_NUMS; i++) {
493 mode = stat64Info->st_mode >> (uint)(USER_MODE_SHIFT - i * UGO_NUMS);
494 str[i][0] = (mode & READ_OP) ?
'r' :
'-';
495 str[i][1] = (mode & WRITE_OP) ?
'w' :
'-';
496 str[i][UGO_NUMS - 1] = (mode & EXEC_OP) ?
'x' :
'-';
499 if (S_ISDIR(stat64Info->st_mode)) {
501 }
else if (S_ISLNK(stat64Info->st_mode)) {
507 PRINTK(
"%c%s%s%s %-8lld u:%-5d g:%-5d %-10s\n", dirFlag,
508 str[0], str[1], str[UGO_NUMS - 1], stat64Info->st_size, stat64Info->st_uid, stat64Info->st_gid, name);
514 char str[UGO_NUMS][UGO_NUMS + 1] = {0};
518 for (i = 0; i < UGO_NUMS; i++) {
519 mode = statInfo->st_mode >> (uint)(USER_MODE_SHIFT - i * UGO_NUMS);
520 str[i][0] = (mode & READ_OP) ?
'r' :
'-';
521 str[i][1] = (mode & WRITE_OP) ?
'w' :
'-';
522 str[i][UGO_NUMS - 1] = (mode & EXEC_OP) ?
'x' :
'-';
525 if (S_ISDIR(statInfo->st_mode)) {
527 }
else if (S_ISLNK(statInfo->st_mode)) {
533 PRINTK(
"%c%s%s%s %-8lld u:%-5d g:%-5d %-10s\n", dirFlag,
534 str[0], str[1], str[UGO_NUMS - 1], statInfo->st_size, statInfo->st_uid, statInfo->st_gid, name);
539 struct stat64 stat64Info;
540 struct stat statInfo;
542 if (stat64(path, &stat64Info) == 0) {
544 }
else if (stat(path, &statInfo) == 0) {
555 struct stat statInfo = { 0 };
556 struct stat64 stat64Info = { 0 };
558 char *fullpath = NULL;
559 char *fullpath_bak = NULL;
560 struct dirent *pdirent = NULL;
567 PRINTK(
"Directory %s:\n", path);
569 pdirent = readdir(d);
570 if (pdirent == NULL) {
573 if (!strcmp(pdirent->d_name,
".") || !strcmp(pdirent->d_name,
"..")) {
576 (
void)memset_s(&statInfo,
sizeof(
struct stat), 0,
sizeof(
struct stat));
577 (
void)memset_s(&stat64Info,
sizeof(
struct stat), 0,
sizeof(
struct stat));
579 if (fullpath == NULL) {
584 fullpath_bak = fullpath;
585 if (stat64(fullpath, &stat64Info) == 0) {
587 }
else if (stat(fullpath, &statInfo) == 0) {
590 PRINTK(
"BAD file: %s\n", pdirent->d_name);
600void ls(
const char *pathname)
602 struct stat statInfo = { 0 };
606 if (pathname == NULL) {
607#ifdef VFS_USING_WORKDIR
630 ret = stat(path, &statInfo);
637 if (statInfo.st_mode & S_IFDIR) {
638 ret =
LsDir((pathname == NULL) ? path : pathname);
651char *
realpath(
const char *path,
char *resolved_path)
654 char *new_path = NULL;
664 result = stat(new_path, &buf);
666 if (resolved_path == NULL) {
667 if (result != ENOERR) {
674 ret = strcpy_s(resolved_path, PATH_MAX, new_path);
683 if (result != ENOERR) {
686 return resolved_path;
691 struct filelist *f_list = NULL;
694 struct Vnode *node = NULL;
696 f_list = &tg_filelist;
698 PRINTK(
" fd filename\n");
701 PRINTK(
"sem_wait error, ret=%d\n", ret);
705 while (i < CONFIG_NFILE_DESCRIPTORS) {
706 node = files_get_openfile(i);
708 PRINTK(
"%5d %s\n", i, f_list->fl_files[i].f_path);
725 umask = mask & UMASK_FULL;
726 SCHEDULER_LOCK(intSave);
729 SCHEDULER_UNLOCK(intSave);
BOOL IsCapPermit(UINT32 capIndex)
int chattr(const char *pathname, struct IATTR *attr)
int vfs_normalize_path(const char *directory, const char *filename, char **pathname)
LITE_OS_SEC_TEXT BOOL LOS_CheckInGroups(UINT32 gid)
STATIC INLINE User * OsCurrUserGet(VOID)
STATIC INLINE LosProcessCB * OsCurrProcessGet(VOID)
void * malloc(size_t size)
动态分配内存块大小
void free(void *ptr)
释放ptr所指向的内存空间
int sem_post(sem_t *sem)
增加信号量计数
int sem_wait(sem_t *sem)
获取信号量
unsigned attr_chg_uid
用户ID
unsigned attr_chg_mode
确定了文件的类型,以及它的所有者、它的group、其它用户访问此文件的权限 (S_IWUSR | ...)
unsigned int attr_chg_valid
节点改变有效性 (CHG_MODE | CHG_UID | ... )
struct files_struct * files
mode_t umask
umask(user file-creatiopn mode mask)为用户文件创建掩码,是创建文件或文件夹时默认权限的基础。
vnode并不包含文件名,因为 vnode和文件名是 1:N 的关系
spinlock_t workdir_lock
工作区目录自旋锁
char workdir[PATH_MAX]
工作区路径,最大 256个字符
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 ARG_NUM_0 ARG_NUM_2 ARG_NUM_1 ARG_NUM_2 ARG_NUM_3 ARG_NUM_7 ARG_NUM_2 ARG_NUM_3 ARG_NUM_2 ARG_NUM_4 ARG_NUM_5 ARG_NUM_6 ARG_NUM_3 ARG_NUM_5 ARG_NUM_7 ARG_NUM_1 ARG_NUM_4 ARG_NUM_5 ARG_NUM_4 ARG_NUM_7 ARG_NUM_2 ARG_NUM_3 ARG_NUM_7 ARG_NUM_7 ARG_NUM_3 ARG_NUM_3 ARG_NUM_3 ARG_NUM_7 ARG_NUM_3 ARG_NUM_2 char ARG_NUM_2 ARG_NUM_1 ARG_NUM_0 ARG_NUM_0 ARG_NUM_3 void ARG_NUM_1 ARG_NUM_0 unsigned ARG_NUM_0 ARG_NUM_2 ARG_NUM_3 ARG_NUM_2 ARG_NUM_5 ARG_NUM_3 ARG_NUM_3 ARG_NUM_4 ARG_NUM_1 ARG_NUM_1 ARG_NUM_3 ARG_NUM_2 mode_t
char * realpath(const char *path, char *resolved_path)
int scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
int chdir(const char *path)
static struct dirent ** scandir_get_file_list(const char *dir, int *num, int(*filter)(const struct dirent *))
int fstat(int fd, struct stat *buf)
void ls(const char *pathname)
list directory contents.
int VfsPermissionCheck(uint fuid, uint fgid, uint fileMode, int accMode)
static int SetWorkDir(const char *dir, size_t len)
int lstat(const char *path, struct stat *buffer)
int fstat64(int fd, struct stat64 *buf)
char * rindex(const char *s, int c)
locate character in string.
char * getcwd(char *buf, size_t n)
static char * ls_get_fullpath(const char *path, struct dirent *pdirent)
int LsFile(const char *path)
static void PrintFileInfo64(const struct stat64 *stat64Info, const char *name)
int chown(const char *pathname, uid_t owner, gid_t group)
mode_t GetUmask(void)
获取用户创建文件掩码
int alphasort(const struct dirent **a, const struct dirent **b)
int VfsVnodePermissionCheck(const struct Vnode *node, int accMode)
int access(const char *path, int amode)
int chmod(const char *path, mode_t mode)
int LsDir(const char *path)
static void PrintFileInfo(const struct stat *statInfo, const char *name)
mode_t SysUmask(mode_t mask)
给当前进程设置系统创建文件掩码,并返回进程旧掩码