vfs_other.c 20.0 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3
 * Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
W
wenjun 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "errno.h"
#include "stdlib.h"
#include "string.h"
#include "dirent.h"
#include "unistd.h"
#include "sys/select.h"
38
#include "sys/mount.h"
W
wenjun 已提交
39
#include "sys/stat.h"
40
#include "sys/statfs.h"
W
wenjun 已提交
41 42
#include "sys/prctl.h"
#include "fs/fd_table.h"
M
mucor 已提交
43
#include "fs/file.h"
W
wenjun 已提交
44 45 46 47
#include "linux/spinlock.h"
#include "los_process_pri.h"
#include "los_task_pri.h"
#include "capability_api.h"
M
mucor 已提交
48
#include "vnode.h"
W
wenjun 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

#define MAX_DIR_ENT 1024
int fstat(int fd, struct stat *buf)
{
    struct file *filep = NULL;

    int ret = fs_getfilep(fd, &filep);
    if (ret < 0) {
        return VFS_ERROR;
    }

    return stat(filep->f_path, buf);
}

int fstat64(int fd, struct stat64 *buf)
{
    struct file *filep = NULL;

    int ret = fs_getfilep(fd, &filep);
    if (ret < 0) {
        return VFS_ERROR;
    }

    return stat64(filep->f_path, buf);
}

int lstat(const char *path, struct stat *buffer)
{
    return stat(path, buffer);
}

W
wangchenyang 已提交
80 81 82 83 84 85 86
int VfsVnodePermissionCheck(const struct Vnode *node, int accMode)
{
    uint fuid = node->uid;
    uint fgid = node->gid;
    uint fileMode = node->mode;
    return VfsPermissionCheck(fuid, fgid, fileMode, accMode);
}
L
lnlan 已提交
87

W
wangchenyang 已提交
88
int VfsPermissionCheck(uint fuid, uint fgid, uint fileMode, int accMode)
W
wenjun 已提交
89 90 91 92 93 94 95 96 97 98 99 100
{
    uint uid = OsCurrUserGet()->effUserID;
    mode_t tmpMode = fileMode;

    if (uid == fuid) {
        tmpMode >>= USER_MODE_SHIFT;
    } else if (LOS_CheckInGroups(fgid)) {
        tmpMode >>= GROUP_MODE_SHIFT;
    }

    tmpMode &= (READ_OP | WRITE_OP | EXEC_OP);

M
mamingshuai 已提交
101
    if (((uint)accMode & tmpMode) == accMode) {
W
wenjun 已提交
102 103 104 105 106
        return 0;
    }

    tmpMode = 0;
    if (S_ISDIR(fileMode)) {
107 108
        if (IsCapPermit(CAP_DAC_EXECUTE)
            || (!((uint)accMode & WRITE_OP) && IsCapPermit(CAP_DAC_READ_SEARCH))) {
W
wenjun 已提交
109 110 111
            tmpMode |= EXEC_OP;
        }
    } else {
112
        if (IsCapPermit(CAP_DAC_EXECUTE) && (fileMode & MODE_IXUGO)) {
W
wenjun 已提交
113 114 115 116
            tmpMode |= EXEC_OP;
        }
    }

117
    if (IsCapPermit(CAP_DAC_WRITE)) {
W
wenjun 已提交
118 119 120
        tmpMode |= WRITE_OP;
    }

121
    if (IsCapPermit(CAP_DAC_READ_SEARCH)) {
W
wenjun 已提交
122 123 124
        tmpMode |= READ_OP;
    }

M
mamingshuai 已提交
125
    if (((uint)accMode & tmpMode) == accMode) {
W
wenjun 已提交
126 127 128 129 130 131 132
        return 0;
    }

    return 1;
}

#ifdef VFS_USING_WORKDIR
M
mamingshuai 已提交
133
static int SetWorkDir(const char *dir, size_t len)
W
wenjun 已提交
134
{
Y
yinjiaming 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147
    errno_t ret;
    uint lock_flags;
    LosProcessCB *curr = OsCurrProcessGet();

    spin_lock_irqsave(&curr->files->workdir_lock, lock_flags);
    ret = strncpy_s(curr->files->workdir, PATH_MAX, dir, len);
    curr->files->workdir[PATH_MAX - 1] = '\0';
    spin_unlock_irqrestore(&curr->files->workdir_lock, lock_flags);
    if (ret != EOK) {
        return -1;
    }

        return 0;
W
wenjun 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
}
#endif

int chdir(const char *path)
{
    int ret;
    char *fullpath = NULL;
    char *fullpath_bak = NULL;
    struct stat statBuff;

    if (!path) {
        set_errno(EFAULT);
        return -1;
    }

    if (!strlen(path)) {
        set_errno(ENOENT);
        return -1;
    }

    if (strlen(path) > PATH_MAX) {
        set_errno(ENAMETOOLONG);
        return -1;
    }

    ret = vfs_normalize_path((const char *)NULL, path, &fullpath);
    if (ret < 0) {
        set_errno(-ret);
        return -1; /* build path failed */
    }
    fullpath_bak = fullpath;
    ret = stat(fullpath, &statBuff);
    if (ret < 0) {
        free(fullpath_bak);
        return -1;
    }

    if (!S_ISDIR(statBuff.st_mode)) {
        set_errno(ENOTDIR);
        free(fullpath_bak);
        return -1;
    }

    if (VfsPermissionCheck(statBuff.st_uid, statBuff.st_gid, statBuff.st_mode, EXEC_OP)) {
        set_errno(EACCES);
        free(fullpath_bak);
        return -1;
    }

#ifdef VFS_USING_WORKDIR
    ret = SetWorkDir(fullpath, strlen(fullpath));
L
lnlan 已提交
199
    if (ret != 0) {
W
wenjun 已提交
200 201
        PRINT_ERR("chdir path error!\n");
        ret = -1;
L
lnlan 已提交
202
    }
W
wenjun 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
#endif

    /* release normalize directory path name */

    free(fullpath_bak);

    return ret;
}

/**
 * this function is a POSIX compliant version, which will return current
 * working directory.
 *
 * @param buf the returned current directory.
 * @param size the buffer size.
 *
 * @return the returned current directory.
 */

char *getcwd(char *buf, size_t n)
{
#ifdef VFS_USING_WORKDIR
    int ret;
    unsigned int len;
    UINTPTR lock_flags;
    LosProcessCB *curr = OsCurrProcessGet();
#endif
    if (buf == NULL) {
        set_errno(EINVAL);
        return buf;
    }
#ifdef VFS_USING_WORKDIR
    spin_lock_irqsave(&curr->files->workdir_lock, lock_flags);
    len = strlen(curr->files->workdir);
L
lnlan 已提交
237
    if (n <= len) {
W
wenjun 已提交
238 239 240
        set_errno(ERANGE);
        spin_unlock_irqrestore(&curr->files->workdir_lock, lock_flags);
        return NULL;
L
lnlan 已提交
241
    }
W
wenjun 已提交
242
    ret = memcpy_s(buf, n, curr->files->workdir, len + 1);
L
lnlan 已提交
243
    if (ret != EOK) {
W
wenjun 已提交
244 245 246
        set_errno(ENAMETOOLONG);
        spin_unlock_irqrestore(&curr->files->workdir_lock, lock_flags);
        return NULL;
L
lnlan 已提交
247
    }
W
wenjun 已提交
248 249 250 251 252 253 254 255 256 257
    spin_unlock_irqrestore(&curr->files->workdir_lock, lock_flags);
#else
    PRINT_ERR("NO_WORKING_DIR\n");
#endif

    return buf;
}

int chmod(const char *path, mode_t mode)
{
258 259 260 261
    struct IATTR attr = {0};
    attr.attr_chg_mode = mode;
    attr.attr_chg_valid = CHG_MODE; /* change mode */
    int ret;
W
wenjun 已提交
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    ret = chattr(path, &attr);
    if (ret < 0) {
        return VFS_ERROR;
    }

    return OK;
}

int chown(const char *pathname, uid_t owner, gid_t group)
{
    struct IATTR attr = {0};
    attr.attr_chg_valid = 0;
    int ret;

    if (owner != (uid_t)-1) {
        attr.attr_chg_uid = owner;
        attr.attr_chg_valid |= CHG_UID;
    }
    if (group != (gid_t)-1) {
        attr.attr_chg_gid = group;
        attr.attr_chg_valid |= CHG_GID;
    }
    ret = chattr(pathname, &attr);
    if (ret < 0) {
W
wenjun 已提交
287 288 289 290 291 292 293 294
        return VFS_ERROR;
    }

    return OK;
}

int access(const char *path, int amode)
{
295
    int ret;
W
wenjun 已提交
296
    struct stat buf;
297 298 299 300 301 302 303 304 305
    struct statfs fsBuf;

    ret = statfs(path, &fsBuf);
    if (ret != 0) {
        if (get_errno() != ENOSYS) {
            return VFS_ERROR;
        }
        /* dev has no statfs ops, need devfs to handle this in feature */
    }
W
wenjun 已提交
306

307 308 309 310 311 312 313 314 315
    if ((fsBuf.f_flags & MS_RDONLY) && ((unsigned int)amode & W_OK)) {
        set_errno(EROFS);
        return VFS_ERROR;
    }

    ret = stat(path, &buf);
    if (ret != 0) {
        return VFS_ERROR;
    }
W
wenjun 已提交
316

317 318
    if (VfsPermissionCheck(buf.st_uid, buf.st_gid, buf.st_mode, amode)) {
        set_errno(EACCES);
W
wenjun 已提交
319 320 321 322 323 324 325 326 327 328
        return VFS_ERROR;
    }

    return OK;
}

static struct dirent **scandir_get_file_list(const char *dir, int *num, int(*filter)(const struct dirent *))
{
    DIR *od = NULL;
    int listSize = MAX_DIR_ENT;
M
mamingshuai 已提交
329
    int n = 0;
W
wenjun 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    struct dirent **list = NULL;
    struct dirent **newList = NULL;
    struct dirent *ent = NULL;
    struct dirent *p = NULL;
    int err;

    od = opendir(dir);
    if (od == NULL) {
        return NULL;
    }

    list = (struct dirent **)malloc(listSize * sizeof(struct dirent *));
    if (list == NULL) {
        (void)closedir(od);
        return NULL;
    }

    for (ent = readdir(od); ent != NULL; ent = readdir(od)) {
        if (filter && !filter(ent)) {
            continue;
        }

        if (n == listSize) {
            listSize += MAX_DIR_ENT;
            newList = (struct dirent **)malloc(listSize * sizeof(struct dirent *));
            if (newList == NULL) {
                break;
            }

            err = memcpy_s(newList, listSize * sizeof(struct dirent *), list, n * sizeof(struct dirent *));
            if (err != EOK) {
                free(newList);
                break;
            }
            free(list);
            list = newList;
        }

        p = (struct dirent *)malloc(sizeof(struct dirent));
        if (p == NULL) {
            break;
        }

        (void)memcpy_s((void *)p, sizeof(struct dirent), (void *)ent, sizeof(struct dirent));
        list[n] = p;

        n++;
    }

    if (closedir(od) < 0) {
        while (n--) {
            free(list[n]);
        }
        free(list);
        return NULL;
    }

    *num = n;
    return list;
}

int scandir(const char *dir, struct dirent ***namelist,
            int(*filter)(const struct dirent *),
L
lnlan 已提交
393
            int(*compar)(const struct dirent **, const struct dirent **))
W
wenjun 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
{
    int n = 0;
    struct dirent **list = NULL;

    if ((dir == NULL) || (namelist == NULL)) {
        return -1;
    }

    list = scandir_get_file_list(dir, &n, filter);
    if (list == NULL) {
        return -1;
    }

    /* Change to return to the array size */
    *namelist = (struct dirent **)malloc(n * sizeof(struct dirent *));
    if (*namelist == NULL && n > 0) {
        *namelist = list;
    } else if (*namelist != NULL) {
        (void)memcpy_s(*namelist, n * sizeof(struct dirent *), list, n * sizeof(struct dirent *));
        free(list);
    } else {
        free(list);
    }

    /* Sort array */

    if (compar && *namelist) {
        qsort((void *)*namelist, (size_t)n, sizeof(struct dirent *), (int (*)(const void *, const void *))*compar);
    }

    return n;
}

int alphasort(const struct dirent **a, const struct dirent **b)
{
    return strcoll((*a)->d_name, (*b)->d_name);
}

char *rindex(const char *s, int c)
{
    if (s == NULL) {
        return NULL;
    }

    /* Don't bother tracing - strrchr can do that */
    return (char *)strrchr(s, c);
}

static char *ls_get_fullpath(const char *path, struct dirent *pdirent)
{
    char *fullpath = NULL;
L
lnlan 已提交
445
    int ret;
W
wenjun 已提交
446 447

    if (path[1] != '\0') {
L
lnlan 已提交
448
        /* 2, The position of the path character: / and the end character '/0' */
W
wenjun 已提交
449 450 451 452 453
        fullpath = (char *)malloc(strlen(path) + strlen(pdirent->d_name) + 2);
        if (fullpath == NULL) {
            goto exit_with_nomem;
        }

L
lnlan 已提交
454
        /* 2, The position of the path character: / and the end character '/0' */
W
wenjun 已提交
455 456 457 458 459 460 461 462
        ret = snprintf_s(fullpath, strlen(path) + strlen(pdirent->d_name) + 2,
                         strlen(path) + strlen(pdirent->d_name) + 1, "%s/%s", path, pdirent->d_name);
        if (ret < 0) {
            free(fullpath);
            set_errno(ENAMETOOLONG);
            return NULL;
        }
    } else {
L
lnlan 已提交
463
        /* 2, The position of the path character: / and the end character '/0' */
W
wenjun 已提交
464 465 466 467 468
        fullpath = (char *)malloc(strlen(pdirent->d_name) + 2);
        if (fullpath == NULL) {
            goto exit_with_nomem;
        }

L
lnlan 已提交
469
        /* 2, The position of the path character: / and the end character '/0' */
W
wenjun 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
        ret = snprintf_s(fullpath, strlen(pdirent->d_name) + 2, strlen(pdirent->d_name) + 1,
                         "/%s", pdirent->d_name);
        if (ret < 0) {
            free(fullpath);
            set_errno(ENAMETOOLONG);
            return NULL;
        }
    }
    return fullpath;

exit_with_nomem:
    set_errno(ENOSPC);
    return (char *)NULL;
}

485
static void PrintFileInfo64(const struct stat64 *stat64Info, const char *name, const char *linkName)
W
wenjun 已提交
486 487 488 489 490 491 492
{
    mode_t mode;
    char str[UGO_NUMS][UGO_NUMS + 1] = {0};
    char dirFlag;
    int i;

    for (i = 0; i < UGO_NUMS; i++) {
M
mamingshuai 已提交
493
        mode = stat64Info->st_mode >> (uint)(USER_MODE_SHIFT - i * UGO_NUMS);
W
wenjun 已提交
494 495 496 497 498
        str[i][0] = (mode & READ_OP) ? 'r' : '-';
        str[i][1] = (mode & WRITE_OP) ? 'w' : '-';
        str[i][UGO_NUMS - 1] = (mode & EXEC_OP) ? 'x' : '-';
    }

C
chenjing 已提交
499 500 501 502 503 504 505
    if (S_ISDIR(stat64Info->st_mode)) {
        dirFlag = 'd';
    } else if (S_ISLNK(stat64Info->st_mode)) {
        dirFlag = 'l';
    } else {
        dirFlag = '-';
    }
W
wenjun 已提交
506

507 508 509 510 511 512 513 514 515
    if (S_ISLNK(stat64Info->st_mode)) {
        PRINTK("%c%s%s%s %-8lld u:%-5d g:%-5d %-10s -> %s\n", dirFlag,
               str[0], str[1], str[UGO_NUMS - 1], stat64Info->st_size,
               stat64Info->st_uid, stat64Info->st_gid, name, linkName);
    } else {
        PRINTK("%c%s%s%s %-8lld u:%-5d g:%-5d %-10s\n", dirFlag,
               str[0], str[1], str[UGO_NUMS - 1], stat64Info->st_size,
               stat64Info->st_uid, stat64Info->st_gid, name);
    }
W
wenjun 已提交
516 517
}

518
static void PrintFileInfo(const struct stat *statInfo, const char *name, const char *linkName)
W
wenjun 已提交
519 520 521 522 523 524 525
{
    mode_t mode;
    char str[UGO_NUMS][UGO_NUMS + 1] = {0};
    char dirFlag;
    int i;

    for (i = 0; i < UGO_NUMS; i++) {
M
mamingshuai 已提交
526
        mode = statInfo->st_mode >> (uint)(USER_MODE_SHIFT - i * UGO_NUMS);
W
wenjun 已提交
527 528 529 530 531
        str[i][0] = (mode & READ_OP) ? 'r' : '-';
        str[i][1] = (mode & WRITE_OP) ? 'w' : '-';
        str[i][UGO_NUMS - 1] = (mode & EXEC_OP) ? 'x' : '-';
    }

C
chenjing 已提交
532 533 534 535 536 537 538
    if (S_ISDIR(statInfo->st_mode)) {
        dirFlag = 'd';
    } else if (S_ISLNK(statInfo->st_mode)) {
        dirFlag = 'l';
    } else {
        dirFlag = '-';
    }
W
wenjun 已提交
539

540 541 542 543 544 545 546 547 548
    if (S_ISLNK(statInfo->st_mode)) {
        PRINTK("%c%s%s%s %-8lld u:%-5d g:%-5d %-10s -> %s\n", dirFlag,
               str[0], str[1], str[UGO_NUMS - 1], statInfo->st_size,
               statInfo->st_uid, statInfo->st_gid, name, linkName);
    } else {
        PRINTK("%c%s%s%s %-8lld u:%-5d g:%-5d %-10s\n", dirFlag,
               str[0], str[1], str[UGO_NUMS - 1], statInfo->st_size,
               statInfo->st_uid, statInfo->st_gid, name);
    }
W
wenjun 已提交
549
}
550 551

int LsFile(const char *path)
W
wenjun 已提交
552
{
553 554
    struct stat64 stat64Info;
    struct stat statInfo;
555
    char linkName[NAME_MAX] = { 0 };
556 557

    if (stat64(path, &stat64Info) == 0) {
558 559 560 561
        if (S_ISLNK(stat64Info.st_mode)) {
            readlink(path, linkName, NAME_MAX);
        }
        PrintFileInfo64(&stat64Info, path, (const char *)linkName);
562
    } else if (stat(path, &statInfo) == 0) {
563 564 565 566
        if (S_ISLNK(statInfo.st_mode)) {
            readlink(path, linkName, NAME_MAX);
        }
        PrintFileInfo(&statInfo, path, (const char *)linkName);
567 568 569 570 571 572 573 574 575 576 577
    } else {
        return -1;
    }

    return 0;
}

int LsDir(const char *path)
{
    struct stat statInfo = { 0 };
    struct stat64 stat64Info = { 0 };
578
    char linkName[NAME_MAX] = { 0 };
579
    DIR *d = NULL;
W
wenjun 已提交
580 581
    char *fullpath = NULL;
    char *fullpath_bak = NULL;
582 583 584 585 586 587 588 589 590 591 592 593
    struct dirent *pdirent = NULL;

    d = opendir(path);
    if (d == NULL) {
        return -1;
    }

    PRINTK("Directory %s:\n", path);
    do {
        pdirent = readdir(d);
        if (pdirent == NULL) {
            break;
594 595 596 597 598 599 600 601 602 603 604 605
        }
        if (!strcmp(pdirent->d_name, ".") || !strcmp(pdirent->d_name, "..")) {
            continue;
        }
        (void)memset_s(&statInfo, sizeof(struct stat), 0, sizeof(struct stat));
        (void)memset_s(&stat64Info, sizeof(struct stat), 0, sizeof(struct stat));
        (void)memset_s(&linkName, sizeof(linkName), 0, sizeof(linkName));
        fullpath = ls_get_fullpath(path, pdirent);
        if (fullpath == NULL) {
            (void)closedir(d);
            return -1;
        }
606

607 608 609 610
        fullpath_bak = fullpath;
        if (stat64(fullpath, &stat64Info) == 0) {
            if (S_ISLNK(stat64Info.st_mode)) {
                readlink(fullpath, linkName, NAME_MAX);
611
            }
612 613 614 615 616 617 618 619
            PrintFileInfo64(&stat64Info, pdirent->d_name, linkName);
        } else if (stat(fullpath, &statInfo) == 0) {
            if (S_ISLNK(statInfo.st_mode)) {
                readlink(fullpath, linkName, NAME_MAX);
            }
            PrintFileInfo(&statInfo, pdirent->d_name, linkName);
        } else {
            PRINTK("BAD file: %s\n", pdirent->d_name);
620
        }
621
        free(fullpath_bak);
622 623 624 625 626 627 628 629 630 631
    } while (1);
    (void)closedir(d);

    return 0;
}

void ls(const char *pathname)
{
    struct stat statInfo = { 0 };
    char *path = NULL;
W
wenjun 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
    int ret;

    if (pathname == NULL) {
#ifdef VFS_USING_WORKDIR
        UINTPTR lock_flags;
        LosProcessCB *curr = OsCurrProcessGet();

        /* open current working directory */

        spin_lock_irqsave(&curr->files->workdir_lock, lock_flags);
        path = strdup(curr->files->workdir);
        spin_unlock_irqrestore(&curr->files->workdir_lock, lock_flags);
#else
        path = strdup("/");
#endif
        if (path == NULL) {
            return;
        }
    } else {
        ret = vfs_normalize_path(NULL, pathname, &path);
        if (ret < 0) {
            set_errno(-ret);
            return;
        }
    }

658 659
    ret = stat(path, &statInfo);
    if (ret < 0) {
W
wenjun 已提交
660
        perror("ls error");
661 662 663
        free(path);
        return;
    }
W
wenjun 已提交
664

665 666
    if (statInfo.st_mode & S_IFDIR) { /* list all directory and file */
        ret = LsDir((pathname == NULL) ? path : pathname);
667
    } else { /* show the file information */
668 669 670 671
        ret = LsFile(path);
    }
    if (ret < 0) {
        perror("ls error");
W
wenjun 已提交
672 673
    }

674
    free(path);
W
wenjun 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    return;
}


char *realpath(const char *path, char *resolved_path)
{
    int ret, result;
    char *new_path = NULL;
    struct stat buf;

    ret = vfs_normalize_path(NULL, path, &new_path);
    if (ret < 0) {
        ret = -ret;
        set_errno(ret);
        return NULL;
    }

    result = stat(new_path, &buf);

    if (resolved_path == NULL) {
        if (result != ENOERR) {
            free(new_path);
            return NULL;
        }
        return new_path;
    }

    ret = strcpy_s(resolved_path, PATH_MAX, new_path);
    if (ret != EOK) {
        ret = -ret;
        set_errno(ret);
        free(new_path);
        return NULL;
    }

    free(new_path);
    if (result != ENOERR) {
        return NULL;
    }
    return resolved_path;
}

void lsfd(void)
{
W
wangchenyang 已提交
719
    struct filelist *f_list = NULL;
W
wenjun 已提交
720 721
    unsigned int i = 3; /* file start fd */
    int ret;
W
wangchenyang 已提交
722
    struct Vnode *node = NULL;
W
wenjun 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

    f_list = &tg_filelist;

    PRINTK("   fd    filename\n");
    ret = sem_wait(&f_list->fl_sem);
    if (ret < 0) {
        PRINTK("sem_wait error, ret=%d\n", ret);
        return;
    }

    while (i < CONFIG_NFILE_DESCRIPTORS) {
        node = files_get_openfile(i);
        if (node) {
            PRINTK("%5d   %s\n", i, f_list->fl_files[i].f_path);
        }
        i++;
    }
    (void)sem_post(&f_list->fl_sem);
}

mode_t GetUmask(void)
{
    return OsCurrProcessGet()->umask;
}

mode_t SysUmask(mode_t mask)
{
    UINT32 intSave;
    mode_t umask;
    mode_t oldUmask;
    umask = mask & UMASK_FULL;
    SCHEDULER_LOCK(intSave);
    oldUmask = OsCurrProcessGet()->umask;
    OsCurrProcessGet()->umask = umask;
    SCHEDULER_UNLOCK(intSave);
    return oldUmask;
}
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

#ifdef LOSCFG_CHROOT
int chroot(const char *path)
{
    int ret;
    struct Vnode *vnode = NULL;

    if (!path) {
        set_errno(EFAULT);
        return VFS_ERROR;
    }

    if (!strlen(path)) {
        set_errno(ENOENT);
        return VFS_ERROR;
    }

    if (strlen(path) > PATH_MAX) {
        set_errno(ENAMETOOLONG);
        return VFS_ERROR;
    }
    VnodeHold();
    ret = VnodeLookup(path, &vnode, 0);
    if (ret != LOS_OK) {
        VnodeDrop();
        return ret;
    }

    LosProcessCB *curr = OsCurrProcessGet();
    if ((curr->files == NULL) || (curr->files->rootVnode == NULL)) {
        VnodeDrop();
        return VFS_ERROR;
    }
    if (curr->files->rootVnode->useCount > 0) {
        curr->files->rootVnode->useCount--;
    }
    vnode->useCount++;
    curr->files->rootVnode = vnode;

    VnodeDrop();
    return LOS_OK;
}
#endif