vfs_shellcmd.c 39.8 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 *
 * 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 "los_config.h"
#include "sys/mount.h"

#ifdef LOSCFG_SHELL

#include "los_typedef.h"
#include "shell.h"
#include "sys/stat.h"
#include "stdlib.h"
#include "unistd.h"
#include "fcntl.h"
#include "sys/statfs.h"
#include "stdio.h"
#include "pthread.h"

#include "shcmd.h"
#include "securec.h"
#include "show.h"
#include "los_syscall.h"

#include "los_process_pri.h"
#include <ctype.h>
W
wangchenyang 已提交
54
#include "fs/fs_operation.h"
W
wenjun 已提交
55

Y
yinjiaming 已提交
56 57 58 59 60 61
typedef enum {
    RM_RECURSIVER,
    RM_FILE,
    RM_DIR,
    CP_FILE,
    CP_COUNT
W
wenjun 已提交
62 63 64
} wildcard_type;

#define ERROR_OUT_IF(condition, message_function, handler) \
Y
yinjiaming 已提交
65 66 67 68
    do { \
        if (condition) { \
            message_function; \
            handler; \
W
wenjun 已提交
69
        } \
Y
yinjiaming 已提交
70
    } while (0)
W
wenjun 已提交
71 72 73

static inline void set_err(int errcode, const char *err_message)
{
Y
yinjiaming 已提交
74 75
    set_errno(errcode);
    perror(err_message);
W
wenjun 已提交
76 77 78 79
}

int osShellCmdDoChdir(const char *path)
{
Y
yinjiaming 已提交
80 81 82 83 84 85
    char *fullpath = NULL;
    char *fullpath_bak = NULL;
    int ret;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
86 87
    }

Y
yinjiaming 已提交
88 89 90 91
    if (path == NULL) {
        LOS_TaskLock();
        PRINTK("%s\n", shell_working_directory);
        LOS_TaskUnlock();
W
wenjun 已提交
92

Y
yinjiaming 已提交
93
        return 0;
W
wenjun 已提交
94 95
    }

Y
yinjiaming 已提交
96
    ERROR_OUT_IF(strlen(path) > PATH_MAX, set_err(ENOTDIR, "cd error"), return -1);
W
wenjun 已提交
97

Y
yinjiaming 已提交
98 99
    ret = vfs_normalize_path(shell_working_directory, path, &fullpath);
    ERROR_OUT_IF(ret < 0, set_err(-ret, "cd error"), return -1);
W
wenjun 已提交
100

Y
yinjiaming 已提交
101 102 103 104 105 106
    fullpath_bak = fullpath;
    ret = chdir(fullpath);
    if (ret < 0) {
        free(fullpath_bak);
        perror("cd");
        return -1;
W
wenjun 已提交
107 108
    }

Y
yinjiaming 已提交
109
    /* copy full path to working directory */
W
wenjun 已提交
110

Y
yinjiaming 已提交
111 112 113 114 115 116
    LOS_TaskLock();
    ret = strncpy_s(shell_working_directory, PATH_MAX, fullpath, strlen(fullpath));
    if (ret != EOK) {
        free(fullpath_bak);
        LOS_TaskUnlock();
        return -1;
W
wenjun 已提交
117
    }
Y
yinjiaming 已提交
118 119
    LOS_TaskUnlock();
    /* release normalize directory path name */
W
wenjun 已提交
120

Y
yinjiaming 已提交
121
    free(fullpath_bak);
W
wenjun 已提交
122

Y
yinjiaming 已提交
123
    return 0;
W
wenjun 已提交
124 125 126 127
}

int osShellCmdLs(int argc, const char **argv)
{
Y
yinjiaming 已提交
128 129 130 131 132 133
    char *fullpath = NULL;
    const char *filename = NULL;
    int ret;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
134 135
    }

Y
yinjiaming 已提交
136
    ERROR_OUT_IF(argc > 1, PRINTK("ls or ls [DIRECTORY]\n"), return -1);
W
wenjun 已提交
137

Y
yinjiaming 已提交
138 139 140
    if (argc == 0) {
        ls(shell_working_directory);
        return 0;
W
wenjun 已提交
141 142
    }

Y
yinjiaming 已提交
143 144 145
    filename = argv[0];
    ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
    ERROR_OUT_IF(ret < 0, set_err(-ret, "ls error"), return -1);
W
wenjun 已提交
146

Y
yinjiaming 已提交
147 148
    ls(fullpath);
    free(fullpath);
W
wenjun 已提交
149

Y
yinjiaming 已提交
150
    return 0;
W
wenjun 已提交
151 152 153 154
}

int osShellCmdCd(int argc, const char **argv)
{
Y
yinjiaming 已提交
155 156 157
    if (argc == 0) {
        (void)osShellCmdDoChdir("/");
        return 0;
W
wenjun 已提交
158 159
    }

Y
yinjiaming 已提交
160
    (void)osShellCmdDoChdir(argv[0]);
W
wenjun 已提交
161

Y
yinjiaming 已提交
162
    return 0;
W
wenjun 已提交
163 164 165 166 167 168 169 170 171
}

#define CAT_BUF_SIZE  512
#define CAT_TASK_PRIORITY  10
#define CAT_TASK_STACK_SIZE  0x3000
pthread_mutex_t g_mutex_cat = PTHREAD_MUTEX_INITIALIZER;

int osShellCmdDoCatShow(UINTPTR arg)
{
Y
yinjiaming 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184
    int ret = 0;
    char buf[CAT_BUF_SIZE];
    size_t size, written, toWrite;
    ssize_t cnt;
    char *fullpath = (char *)arg;
    FILE *ini = NULL;

    (void)pthread_mutex_lock(&g_mutex_cat);
    ini = fopen(fullpath, "r");
    if (ini == NULL) {
        ret = -1;
        perror("cat error");
        goto out;
W
wenjun 已提交
185 186
    }

Y
yinjiaming 已提交
187 188 189 190 191 192 193
    do {
        (void)memset_s(buf, sizeof(buf), 0, CAT_BUF_SIZE);
        size = fread(buf, 1, CAT_BUF_SIZE, ini);
        if ((int)size < 0) {
            ret = -1;
            perror("cat error");
            goto out_with_fclose;
M
mamingshuai 已提交
194 195
        }

Y
yinjiaming 已提交
196 197 198 199 200 201 202 203 204
        for (toWrite = size, written = 0; toWrite > 0;) {
            cnt = write(1, buf + written, toWrite);
            if (cnt == 0) {
                /* avoid task-starvation */
                (void)LOS_TaskDelay(1);
                continue;
            } else if (cnt < 0) {
                perror("cat write error");
                break;
M
mamingshuai 已提交
205 206
            }

Y
yinjiaming 已提交
207 208
            written += cnt;
            toWrite -= cnt;
W
wenjun 已提交
209 210
        }
    }
Y
yinjiaming 已提交
211
    while (size > 0);
W
wenjun 已提交
212

M
mamingshuai 已提交
213
out_with_fclose:
Y
yinjiaming 已提交
214
    (void)fclose(ini);
M
mamingshuai 已提交
215
out:
Y
yinjiaming 已提交
216 217 218
    free(fullpath);
    (void)pthread_mutex_unlock(&g_mutex_cat);
    return ret;
W
wenjun 已提交
219 220 221 222
}

int osShellCmdCat(int argc, const char **argv)
{
Y
yinjiaming 已提交
223 224 225 226 227 228 229 230
    char *fullpath = NULL;
    int ret;
    unsigned int ca_task;
    struct Vnode *vnode = NULL;
    TSK_INIT_PARAM_S init_param;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
231 232
    }

Y
yinjiaming 已提交
233
    ERROR_OUT_IF(argc != 1, PRINTK("cat [FILE]\n"), return -1);
W
wenjun 已提交
234

Y
yinjiaming 已提交
235 236
    ret = vfs_normalize_path(shell_working_directory, argv[0], &fullpath);
    ERROR_OUT_IF(ret < 0, set_err(-ret, "cat error"), return -1);
W
wenjun 已提交
237

Y
yinjiaming 已提交
238 239 240
    VnodeHold();
    ret = VnodeLookup(fullpath, &vnode, O_RDONLY);
    if (ret != LOS_OK) {
W
wangchenyang 已提交
241 242 243 244 245
        set_errno(-ret);
        perror("cat error");
        VnodeDrop();
        free(fullpath);
        return -1;
Y
yinjiaming 已提交
246 247
    }
    if (vnode->type != VNODE_TYPE_REG) {
W
wangchenyang 已提交
248 249 250 251 252
        set_errno(EINVAL);
        perror("cat error");
        VnodeDrop();
        free(fullpath);
        return -1;
Y
yinjiaming 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266
    }
    VnodeDrop();
    (void)memset_s(&init_param, sizeof(init_param), 0, sizeof(TSK_INIT_PARAM_S));
    init_param.pfnTaskEntry = (TSK_ENTRY_FUNC)osShellCmdDoCatShow;
    init_param.usTaskPrio   = CAT_TASK_PRIORITY;
    init_param.auwArgs[0]   = (UINTPTR)fullpath;
    init_param.uwStackSize  = CAT_TASK_STACK_SIZE;
    init_param.pcName       = "shellcmd_cat";
    init_param.uwResved     = LOS_TASK_STATUS_DETACHED | OS_TASK_FLAG_SPECIFIES_PROCESS;
    init_param.processID    = 2; /* 2: kProcess */

    ret = (int)LOS_TaskCreate(&ca_task, &init_param);
    if (ret != LOS_OK) {
        free(fullpath);
W
wangchenyang 已提交
267 268
    }

Y
yinjiaming 已提交
269
    return ret;
W
wenjun 已提交
270 271 272 273 274 275 276 277 278 279 280
}

static int nfs_mount_ref(const char *server_ip_and_path, const char *mount_path,
                         unsigned int uid, unsigned int gid) __attribute__((weakref("nfs_mount")));

static unsigned long get_mountflags(const char *options)
{
    unsigned long mountfalgs = 0;
    char *p;
    while ((options != NULL) && (p = strsep((char**)&options, ",")) != NULL) {
        if (strncmp(p, "ro", strlen("ro")) == 0) {
Y
yinjiaming 已提交
281
            mountfalgs |= MS_RDONLY;
W
wenjun 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        } else if (strncmp(p, "rw", strlen("rw")) == 0) {
            mountfalgs &= ~MS_RDONLY;
        } else if (strncmp(p, "nosuid", strlen("nosuid")) == 0) {
            mountfalgs |= MS_NOSUID;
        } else if (strncmp(p, "suid", strlen("suid")) == 0) {
            mountfalgs &= ~MS_NOSUID;
        } else {
            continue;
        }
    }

    return mountfalgs;
}
static inline void print_mount_usage(void)
{
Y
yinjiaming 已提交
297
    PRINTK("mount [DEVICE] [PATH] [NAME]\n");
W
wenjun 已提交
298 299 300 301
}

int osShellCmdMount(int argc, const char **argv)
{
Y
yinjiaming 已提交
302 303 304 305 306 307 308 309 310 311
    int ret;
    char *fullpath = NULL;
    const char *filename = NULL;
    unsigned int gid, uid;
    char *data = NULL;
    char *filessystemtype = NULL;
    unsigned long mountfalgs;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
312 313
    }

Y
yinjiaming 已提交
314
    ERROR_OUT_IF(argc < 3, print_mount_usage(), return OS_FAIL);
W
wenjun 已提交
315

Y
yinjiaming 已提交
316
    if (strncmp(argv[0], "-t", 2) == 0 || strncmp(argv[0], "-o", 2) == 0) // 2: length of "-t"
W
wenjun 已提交
317
    {
Y
yinjiaming 已提交
318 319 320
        if (argc < 4) { // 4: required number of parameters
            PRINTK("mount -t/-o [DEVICE] [PATH] [NAME]\n");
            return -1;
W
wenjun 已提交
321 322
        }

Y
yinjiaming 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335
        filename = argv[2]; // 2: index of file path
        ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
        ERROR_OUT_IF(ret < 0, set_err(-ret, "mount error"), return -1);

        if (strncmp(argv[3], "nfs", 3) == 0) { // 3: index of fs type
            if (argc <= 6) { // 6: arguments include uid or gid
                uid = ((argc >= 5) && (argv[4] != NULL)) ? (unsigned int)strtoul(argv[4], (char **)NULL, 0) : 0;
                gid = ((argc == 6) && (argv[5] != NULL)) ? (unsigned int)strtoul(argv[5], (char **)NULL, 0) : 0;
   
                if (nfs_mount_ref != NULL) {
                    ret = nfs_mount_ref(argv[1], fullpath, uid, gid);
                    if (ret != LOS_OK) {
                        PRINTK("mount -t [DEVICE] [PATH] [NAME]\n");
W
wenjun 已提交
336
                    }
Y
yinjiaming 已提交
337 338
                } else {
                    PRINTK("can't find nfs_mount\n");
W
wenjun 已提交
339
                }
Y
yinjiaming 已提交
340 341
                free(fullpath);
                return 0;
W
wenjun 已提交
342 343 344
            }
        }

Y
yinjiaming 已提交
345 346 347
        filessystemtype = (argc >= 4) ? (char *)argv[3] : NULL; /* 4: specify fs type, 3: fs type */
        mountfalgs = (argc >= 5) ? get_mountflags((const char *)argv[4]) : 0; /* 4: usr option */
        data = (argc >= 6) ? (char *)argv[5] : NULL; /* 5: usr option data */
W
wenjun 已提交
348

Y
yinjiaming 已提交
349 350 351 352
        if (strcmp(argv[1], "0") == 0) {
            ret = mount((const char *)NULL, fullpath, filessystemtype, mountfalgs, data);
        } else {
            ret = mount(argv[1], fullpath, filessystemtype, mountfalgs, data); /* 3: fs type */
W
wenjun 已提交
353
        }
Y
yinjiaming 已提交
354 355 356 357
        if (ret != LOS_OK) {
            perror("mount error");
        } else {
            PRINTK("mount ok\n");
W
wenjun 已提交
358
        }
Y
yinjiaming 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372
    } else {
        filename = argv[1];
        ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
        ERROR_OUT_IF(ret < 0, set_err(-ret, "mount error"), return -1);

        if (strncmp(argv[2], "nfs", 3) == 0) { // 2: index of fs type, 3: length of "nfs"
            if (argc <= 5) { // 5: arguments include gid and uid
                uid = ((argc >= 4) && (argv[3] != NULL)) ? (unsigned int)strtoul(argv[3], (char **)NULL, 0) : 0;
                gid = ((argc == 5) && (argv[4] != NULL)) ? (unsigned int)strtoul(argv[4], (char **)NULL, 0) : 0;

                if (nfs_mount_ref != NULL) {
                    ret = nfs_mount_ref(argv[0], fullpath, uid, gid);
                    if (ret != LOS_OK) {
                        PRINTK("mount [DEVICE] [PATH] [NAME]\n");
W
wenjun 已提交
373
                    }
Y
yinjiaming 已提交
374 375
                } else {
                    PRINTK("can't find nfs_mount\n");
W
wenjun 已提交
376
                }
Y
yinjiaming 已提交
377 378
                free(fullpath);
                return 0;
W
wenjun 已提交
379 380
            }

Y
yinjiaming 已提交
381 382 383
            print_mount_usage();
            free(fullpath);
            return 0;
W
wenjun 已提交
384 385
        }

Y
yinjiaming 已提交
386 387
        mountfalgs = (argc >= 4) ? get_mountflags((const char *)argv[3]) : 0;  /* 3: usr option */
        data = (argc >= 5) ? (char *)argv[4] : NULL; /* 4: usr option data */
W
wenjun 已提交
388

Y
yinjiaming 已提交
389 390 391 392
        if (strcmp(argv[0], "0") == 0) {
            ret = mount((const char *)NULL, fullpath, argv[2], mountfalgs, data);
        } else {
            ret = mount(argv[0], fullpath, argv[2], mountfalgs, data);  /* 2: fs type */
W
wenjun 已提交
393
        }
Y
yinjiaming 已提交
394 395 396 397
        if (ret != LOS_OK) {
            perror("mount error");
        } else {
            PRINTK("mount ok\n");
W
wenjun 已提交
398 399 400
        }
    }

Y
yinjiaming 已提交
401 402
    free(fullpath);
    return 0;
W
wenjun 已提交
403 404 405 406
}

int osShellCmdUmount(int argc, const char **argv)
{
Y
yinjiaming 已提交
407 408 409 410 411 412 413 414 415
    int ret;
    const char *filename = NULL;
    char *fullpath = NULL;
    char *target_path = NULL;
    int cmp_num;
    char *work_path = NULL;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
416
    }
Y
yinjiaming 已提交
417
    work_path = shell_working_directory;
W
wenjun 已提交
418

Y
yinjiaming 已提交
419
    ERROR_OUT_IF(argc == 0, PRINTK("umount [PATH]\n"), return 0);
W
wenjun 已提交
420

Y
yinjiaming 已提交
421 422 423
    filename = argv[0];
    ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
    ERROR_OUT_IF(ret < 0, set_err(-ret, "umount error"), return -1);
W
wenjun 已提交
424

Y
yinjiaming 已提交
425 426 427 428 429 430 431 432 433 434
    target_path = fullpath;
    cmp_num = strlen(fullpath);
    ret = strncmp(work_path, target_path, cmp_num);
    if (ret == 0) {
        work_path += cmp_num;
        if (*work_path == '/' || *work_path == '\0') {
            set_errno(EBUSY);
            perror("umount error");
            free(fullpath);
            return -1;
W
wenjun 已提交
435 436 437
        }
    }

Y
yinjiaming 已提交
438 439 440 441 442
    ret = umount(fullpath);
    free(fullpath);
    if (ret != LOS_OK) {
        perror("umount error");
        return 0;
W
wenjun 已提交
443 444
    }

Y
yinjiaming 已提交
445 446
    PRINTK("umount ok\n");
    return 0;
W
wenjun 已提交
447 448 449 450
}

int osShellCmdMkdir(int argc, const char **argv)
{
Y
yinjiaming 已提交
451 452 453 454 455 456
    int ret;
    char *fullpath = NULL;
    const char *filename = NULL;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
457 458
    }

Y
yinjiaming 已提交
459
    ERROR_OUT_IF(argc != 1, PRINTK("mkdir [DIRECTORY]\n"), return 0);
W
wenjun 已提交
460

Y
yinjiaming 已提交
461 462 463
    filename = argv[0];
    ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
    ERROR_OUT_IF(ret < 0, set_err(-ret, "mkdir error"), return -1);
W
wenjun 已提交
464

Y
yinjiaming 已提交
465 466 467
    ret = mkdir(fullpath, S_IRWXU | S_IRWXG | S_IRWXO);
    if (ret == -1) {
        perror("mkdir error");
W
wenjun 已提交
468
    }
Y
yinjiaming 已提交
469 470
    free(fullpath);
    return 0;
W
wenjun 已提交
471 472 473 474
}

int osShellCmdPwd(int argc, const char **argv)
{
Y
yinjiaming 已提交
475 476 477 478 479
    char buf[SHOW_MAX_LEN] = {0};
    DIR *dir = NULL;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
480 481
    }

Y
yinjiaming 已提交
482
    ERROR_OUT_IF(argc > 0, PRINTK("\nUsage: pwd\n"), return -1);
W
wenjun 已提交
483

Y
yinjiaming 已提交
484 485 486 487
    dir = opendir(shell_working_directory);
    if (dir == NULL) {
        perror("pwd error");
        return -1;
W
wenjun 已提交
488 489
    }

Y
yinjiaming 已提交
490 491 492 493 494 495
    LOS_TaskLock();
    if (strncpy_s(buf, SHOW_MAX_LEN, shell_working_directory, SHOW_MAX_LEN - 1) != EOK) {
        LOS_TaskUnlock();
        PRINTK("pwd error: strncpy_s error!\n");
        (void)closedir(dir);
        return -1;
W
wenjun 已提交
496
    }
Y
yinjiaming 已提交
497
    LOS_TaskUnlock();
W
wenjun 已提交
498

Y
yinjiaming 已提交
499 500 501
    PRINTK("%s\n", buf);
    (void)closedir(dir);
    return 0;
W
wenjun 已提交
502 503 504 505
}

static inline void print_statfs_usage(void)
{
Y
yinjiaming 已提交
506 507 508 509 510
    PRINTK("Usage  :\n");
    PRINTK("    statfs <path>\n");
    PRINTK("    path  : Mounted file system path that requires query information\n");
    PRINTK("Example:\n");
    PRINTK("    statfs /ramfs\n");
W
wenjun 已提交
511 512 513 514
}

int osShellCmdStatfs(int argc, const char **argv)
{
Y
yinjiaming 已提交
515 516 517 518 519 520 521 522
    struct statfs sfs;
    int result;
    unsigned long long total_size, free_size;
    char *fullpath = NULL;
    const char *filename = NULL;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
523 524
    }

Y
yinjiaming 已提交
525
    ERROR_OUT_IF(argc != 1, PRINTK("statfs failed! Invalid argument!\n"), return -1);
W
wenjun 已提交
526

Y
yinjiaming 已提交
527
    (void)memset_s(&sfs, sizeof(sfs), 0, sizeof(sfs));
W
wenjun 已提交
528

Y
yinjiaming 已提交
529 530 531
    filename = argv[0];
    result = vfs_normalize_path(shell_working_directory, filename, &fullpath);
    ERROR_OUT_IF(result < 0, set_err(-result, "statfs error"), return -1);
W
wenjun 已提交
532

Y
yinjiaming 已提交
533 534
    result = statfs(fullpath, &sfs);
    free(fullpath);
W
wenjun 已提交
535

Y
yinjiaming 已提交
536 537 538 539
    if (result != 0 || sfs.f_type == 0) {
        PRINTK("statfs failed! Invalid argument!\n");
        print_statfs_usage();
        return -1;
W
wenjun 已提交
540 541
    }

Y
yinjiaming 已提交
542 543
    total_size  = (unsigned long long)sfs.f_bsize * sfs.f_blocks;
    free_size   = (unsigned long long)sfs.f_bsize * sfs.f_bfree;
W
wenjun 已提交
544

Y
yinjiaming 已提交
545 546 547 548
    PRINTK("statfs got:\n f_type     = %d\n cluster_size   = %d\n", sfs.f_type, sfs.f_bsize);
    PRINTK(" total_clusters = %llu\n free_clusters  = %llu\n", sfs.f_blocks, sfs.f_bfree);
    PRINTK(" avail_clusters = %llu\n f_namelen    = %d\n", sfs.f_bavail, sfs.f_namelen);
    PRINTK("\n%s\n total size: %4llu Bytes\n free  size: %4llu Bytes\n", argv[0], total_size, free_size);
W
wenjun 已提交
549

Y
yinjiaming 已提交
550
    return 0;
W
wenjun 已提交
551 552 553 554
}

int osShellCmdTouch(int argc, const char **argv)
{
Y
yinjiaming 已提交
555 556 557 558 559 560 561
    int ret;
    int fd = -1;
    char *fullpath = NULL;
    const char *filename = NULL;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
562 563
    }

Y
yinjiaming 已提交
564
    ERROR_OUT_IF(argc != 1, PRINTK("touch [FILE]\n"), return -1);
W
wenjun 已提交
565

Y
yinjiaming 已提交
566 567 568
    filename = argv[0];
    ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
    ERROR_OUT_IF(ret < 0, set_err(-ret, "touch error"), return -1);
W
wenjun 已提交
569

Y
yinjiaming 已提交
570 571 572 573 574
    fd = open(fullpath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
    free(fullpath);
    if (fd == -1) {
        perror("touch error");
        return -1;
W
wenjun 已提交
575 576
    }

Y
yinjiaming 已提交
577 578
    (void)close(fd);
    return 0;
W
wenjun 已提交
579 580 581 582 583 584 585
}

#define CP_BUF_SIZE 4096
pthread_mutex_t g_mutex_cp = PTHREAD_MUTEX_INITIALIZER;

static int os_shell_cmd_do_cp(const char *src_filepath, const char *dst_filename)
{
Y
yinjiaming 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    int  ret;
    char *src_fullpath = NULL;
    char *dst_fullpath = NULL;
    const char *src_filename = NULL;
    char *dst_filepath = NULL;
    char *buf = NULL;
    const char *filename = NULL;
    ssize_t r_size, w_size;
    int src_fd = -1;
    int dst_fd = -1;
    struct stat stat_buf;
    mode_t src_mode;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
601 602
    }

Y
yinjiaming 已提交
603 604 605 606
    buf = (char *)malloc(CP_BUF_SIZE);
    if (buf == NULL) {
        PRINTK("cp error: Out of memory!\n");
        return -1;
W
wenjun 已提交
607 608
    }

Y
yinjiaming 已提交
609
    /* Get source fullpath. */
W
wenjun 已提交
610

Y
yinjiaming 已提交
611 612 613 614 615 616
    ret = vfs_normalize_path(shell_working_directory, src_filepath, &src_fullpath);
    if (ret < 0) {
        set_errno(-ret);
        PRINTK("cp error: %s\n", strerror(errno));
        free(buf);
        return -1;
W
wenjun 已提交
617 618
    }

Y
yinjiaming 已提交
619
    /* Is source path exist? */
W
wenjun 已提交
620

Y
yinjiaming 已提交
621 622 623 624
    ret = stat(src_fullpath, &stat_buf);
    if (ret == -1) {
        PRINTK("cp %s error: %s\n", src_fullpath, strerror(errno));
        goto errout_with_srcpath;
W
wenjun 已提交
625
    }
Y
yinjiaming 已提交
626 627
    src_mode = stat_buf.st_mode;
    /* Is source path a directory? */
W
wenjun 已提交
628

Y
yinjiaming 已提交
629 630 631
    if (S_ISDIR(stat_buf.st_mode)) {
        PRINTK("cp %s error: Source file can't be a directory.\n", src_fullpath);
        goto errout_with_srcpath;
W
wenjun 已提交
632 633
    }

Y
yinjiaming 已提交
634
    /* Get dest fullpath. */
W
wenjun 已提交
635

Y
yinjiaming 已提交
636 637 638 639
    dst_fullpath = strdup(dst_filename);
    if (dst_fullpath == NULL) {
        PRINTK("cp error: Out of memory.\n");
        goto errout_with_srcpath;
W
wenjun 已提交
640 641
    }

Y
yinjiaming 已提交
642
    /* Is dest path exist? */
W
wenjun 已提交
643

Y
yinjiaming 已提交
644 645 646 647 648 649 650 651 652 653 654 655
    ret = stat(dst_fullpath, &stat_buf);
    if (ret == 0) {
        /* Is dest path a directory? */

        if (S_ISDIR(stat_buf.st_mode)) {
            /* Get source file name without '/'. */

            src_filename = src_filepath;
            while (1) {
                filename = strchr(src_filename, '/');
                if (filename == NULL) {
                    break;
W
wenjun 已提交
656
                }
Y
yinjiaming 已提交
657
                src_filename = filename + 1;
W
wenjun 已提交
658 659
            }

Y
yinjiaming 已提交
660
            /* Add the source file after dest path. */
W
wenjun 已提交
661

Y
yinjiaming 已提交
662 663 664 665 666
            ret = vfs_normalize_path(dst_fullpath, src_filename, &dst_filepath);
            if (ret < 0) {
                set_errno(-ret);
                PRINTK("cp error. %s.\n", strerror(errno));
                goto errout_with_path;
W
wenjun 已提交
667
            }
Y
yinjiaming 已提交
668 669
            free(dst_fullpath);
            dst_fullpath = dst_filepath;
W
wenjun 已提交
670 671 672
        }
    }

Y
yinjiaming 已提交
673
    /* Is dest file same as source file? */
W
wenjun 已提交
674

Y
yinjiaming 已提交
675 676 677
    if (strcmp(src_fullpath, dst_fullpath) == 0) {
        PRINTK("cp error: '%s' and '%s' are the same file\n", src_fullpath, dst_fullpath);
        goto errout_with_path;
W
wenjun 已提交
678 679
    }

Y
yinjiaming 已提交
680
    /* Copy begins. */
W
wenjun 已提交
681

Y
yinjiaming 已提交
682 683 684 685 686
    (void)pthread_mutex_lock(&g_mutex_cp);
    src_fd = open(src_fullpath, O_RDONLY);
    if (src_fd < 0) {
        PRINTK("cp error: can't open %s. %s.\n", src_fullpath, strerror(errno));
        goto errout_with_mutex;
W
wenjun 已提交
687 688
    }

Y
yinjiaming 已提交
689 690 691 692
    dst_fd = open(dst_fullpath, O_CREAT | O_WRONLY | O_TRUNC, src_mode);
    if (dst_fd < 0) {
        PRINTK("cp error: can't create %s. %s.\n", dst_fullpath, strerror(errno));
        goto errout_with_srcfd;
W
wenjun 已提交
693 694
    }

Y
yinjiaming 已提交
695 696 697 698 699 700
    do {
        (void)memset_s(buf, CP_BUF_SIZE, 0, CP_BUF_SIZE);
        r_size = read(src_fd, buf, CP_BUF_SIZE);
        if (r_size < 0) {
            PRINTK("cp %s %s failed. %s.\n", src_fullpath, dst_fullpath, strerror(errno));
            goto errout_with_fd;
W
wenjun 已提交
701
        }
Y
yinjiaming 已提交
702 703 704 705
        w_size = write(dst_fd, buf, r_size);
        if (w_size != r_size) {
            PRINTK("cp %s %s failed. %s.\n", src_fullpath, dst_fullpath, strerror(errno));
            goto errout_with_fd;
W
wenjun 已提交
706
        }
Y
yinjiaming 已提交
707
    } while (r_size == CP_BUF_SIZE);
W
wenjun 已提交
708

Y
yinjiaming 已提交
709
    /* Release resource. */
W
wenjun 已提交
710

Y
yinjiaming 已提交
711 712 713 714 715 716 717
    free(buf);
    free(src_fullpath);
    free(dst_fullpath);
    (void)close(src_fd);
    (void)close(dst_fd);
    (void)pthread_mutex_unlock(&g_mutex_cp);
    return LOS_OK;
W
wenjun 已提交
718 719

errout_with_fd:
Y
yinjiaming 已提交
720
    (void)close(dst_fd);
W
wenjun 已提交
721
errout_with_srcfd:
Y
yinjiaming 已提交
722
    (void)close(src_fd);
W
wenjun 已提交
723
errout_with_mutex:
Y
yinjiaming 已提交
724
    (void)pthread_mutex_unlock(&g_mutex_cp);
W
wenjun 已提交
725
errout_with_path:
Y
yinjiaming 已提交
726
    free(dst_fullpath);
W
wenjun 已提交
727
errout_with_srcpath:
Y
yinjiaming 已提交
728 729 730
    free(src_fullpath);
    free(buf);
    return -1;
W
wenjun 已提交
731 732 733 734 735 736 737 738
}

/* The separator and EOF for a directory fullpath: '/'and '\0' */

#define SEPARATOR_EOF_LEN 2

static int os_shell_cmd_do_rmdir(const char *pathname)
{
Y
yinjiaming 已提交
739 740 741 742 743 744 745 746 747
    struct dirent *dirent = NULL;
    struct stat stat_info;
    DIR *d = NULL;
    char *fullpath = NULL;
    int ret;

    (void)memset_s(&stat_info, sizeof(stat_info), 0, sizeof(struct stat));
    if (stat(pathname, &stat_info) != 0) {
        return -1;
W
wenjun 已提交
748 749
    }

Y
yinjiaming 已提交
750 751
    if (S_ISREG(stat_info.st_mode) || S_ISLNK(stat_info.st_mode)) {
        return remove(pathname);
W
wenjun 已提交
752
    }
Y
yinjiaming 已提交
753 754 755
    d = opendir(pathname);
    if (d == NULL) {
        return -1;
756
    }
Y
yinjiaming 已提交
757 758 759 760
    while (1) {
        dirent = readdir(d);
        if (dirent == NULL) {
            break;
W
wenjun 已提交
761
        }
Y
yinjiaming 已提交
762 763 764 765 766 767
        if (strcmp(dirent->d_name, "..") && strcmp(dirent->d_name, ".")) {
            size_t fullpath_buf_size = strlen(pathname) + strlen(dirent->d_name) + SEPARATOR_EOF_LEN;
            if (fullpath_buf_size <= 0) {
                PRINTK("buffer size is invalid!\n");
                (void)closedir(d);
                return -1;
M
mamingshuai 已提交
768
            }
Y
yinjiaming 已提交
769 770 771 772 773
            fullpath = (char *)malloc(fullpath_buf_size);
            if (fullpath == NULL) {
                PRINTK("malloc failure!\n");
                (void)closedir(d);
                return -1;
W
wenjun 已提交
774
            }
Y
yinjiaming 已提交
775 776 777 778 779 780
            ret = snprintf_s(fullpath, fullpath_buf_size, fullpath_buf_size - 1, "%s/%s", pathname, dirent->d_name);
            if (ret < 0) {
                PRINTK("name is too long!\n");
                free(fullpath);
                (void)closedir(d);
                return -1;
W
wenjun 已提交
781
            }
Y
yinjiaming 已提交
782 783
            (void)os_shell_cmd_do_rmdir(fullpath);
            free(fullpath);
W
wenjun 已提交
784 785
        }
    }
Y
yinjiaming 已提交
786 787
    (void)closedir(d);
    return rmdir(pathname);
W
wenjun 已提交
788 789 790 791 792 793
}

/*  Wildcard matching operations  */

static int os_wildcard_match(const char *src, const char *filename)
{
Y
yinjiaming 已提交
794
    int ret;
W
wenjun 已提交
795

Y
yinjiaming 已提交
796 797 798 799
    if (*src != '\0') {
        if (*filename == '*') {
            while ((*filename == '*') || (*filename == '?')) {
                filename++;
W
wenjun 已提交
800 801
            }

Y
yinjiaming 已提交
802 803
            if (*filename == '\0') {
                return 0;
W
wenjun 已提交
804 805
            }

Y
yinjiaming 已提交
806 807
            while (*src != '\0' && !(*src == *filename)) {
                src++;
W
wenjun 已提交
808 809
            }

Y
yinjiaming 已提交
810 811
            if (*src == '\0') {
                return -1;
W
wenjun 已提交
812 813
            }

Y
yinjiaming 已提交
814
            ret = os_wildcard_match(src, filename);
W
wenjun 已提交
815

Y
yinjiaming 已提交
816 817 818
            while ((ret != 0) && (*(++src) != '\0')) {
                if (*src == *filename) {
                    ret = os_wildcard_match(src, filename);
W
wenjun 已提交
819 820
                }
            }
Y
yinjiaming 已提交
821 822 823 824
            return ret;
        } else {
            if ((*src == *filename) || (*filename == '?')) {
                return os_wildcard_match(++src, ++filename);
W
wenjun 已提交
825
            }
Y
yinjiaming 已提交
826
            return -1;
W
wenjun 已提交
827 828 829
        }
    }

Y
yinjiaming 已提交
830 831 832
    while (*filename != '\0') {
        if (*filename != '*') {
            return -1;
W
wenjun 已提交
833
        }
Y
yinjiaming 已提交
834
        filename++;
W
wenjun 已提交
835
    }
Y
yinjiaming 已提交
836
    return 0;
W
wenjun 已提交
837 838 839 840 841 842
}

/*   To determine whether a wildcard character exists in a path   */

static int os_is_containers_wildcard(const char *filename)
{
Y
yinjiaming 已提交
843 844 845
    while (*filename != '\0') {
        if ((*filename == '*') || (*filename == '?')) {
            return 1;
W
wenjun 已提交
846
        }
Y
yinjiaming 已提交
847
        filename++;
W
wenjun 已提交
848
    }
Y
yinjiaming 已提交
849
    return 0;
W
wenjun 已提交
850 851 852 853 854 855
}

/*  Delete a matching file or directory  */

static int os_wildcard_delete_file_or_dir(const char *fullpath, wildcard_type mark)
{
Y
yinjiaming 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
    int ret;

    switch (mark) {
        case RM_RECURSIVER:
            ret = os_shell_cmd_do_rmdir(fullpath);
            break;
        case RM_FILE:
            ret = unlink(fullpath);
            break;
        case RM_DIR:
            ret = rmdir(fullpath);
            break;
        default:
            return VFS_ERROR;
    }
    if (ret == -1) {
        PRINTK("%s  ", fullpath);
        perror("rm/rmdir error!");
        return ret;
    }

    PRINTK("%s match successful!delete!\n", fullpath);
    return 0;
W
wenjun 已提交
879 880 881 882 883 884
}

/*  Split the path with wildcard characters  */

static char* os_wildcard_split_path(char *fullpath, char **handle, char **wait)
{
Y
yinjiaming 已提交
885 886 887 888 889 890 891 892 893 894 895
    int n = 0;
    int a = 0;
    int b = 0;
    int len  = strlen(fullpath);

    for (n = 0; n < len; n++) {
        if (fullpath[n] == '/') {
            if (b != 0) {
                fullpath[n] = '\0';
                *wait = fullpath + n + 1;
                break;
W
wenjun 已提交
896
            }
Y
yinjiaming 已提交
897 898 899 900 901 902 903
            a = n;
        } else if (fullpath[n] == '*' || fullpath[n] == '?') {
            b = n;
            fullpath[a] = '\0';
            if (a == 0) {
                *handle = fullpath + a + 1;
                continue;
W
wenjun 已提交
904
            }
Y
yinjiaming 已提交
905
            *handle = fullpath + a + 1;
W
wenjun 已提交
906 907
        }
    }
Y
yinjiaming 已提交
908
    return fullpath;
W
wenjun 已提交
909 910 911 912 913 914
}

/*  Handling entry of the path with wildcard characters  */

static int os_wildcard_extract_directory(char *fullpath, void *dst, wildcard_type mark)
{
Y
yinjiaming 已提交
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
    char separator[] = "/";
    char src[PATH_MAX] = {0};
    struct dirent *dirent = NULL;
    char *f = NULL;
    char *s = NULL;
    char *t = NULL;
    int ret = 0;
    DIR *d = NULL;
    struct stat stat_buf;
    int deleteFlag = 0;

    f = os_wildcard_split_path(fullpath, &s, &t);

    if (s == NULL) {
        if (mark == CP_FILE) {
            ret = os_shell_cmd_do_cp(fullpath, dst);
        } else if (mark == CP_COUNT) {
            ret = stat(fullpath, &stat_buf);
            if (ret == 0 && (S_ISREG(stat_buf.st_mode) || S_ISLNK(stat_buf.st_mode))) {
                (*(int *)dst)++;
W
wenjun 已提交
935
            }
Y
yinjiaming 已提交
936 937
        } else {
            ret = os_wildcard_delete_file_or_dir(fullpath, mark);
W
wenjun 已提交
938
        }
Y
yinjiaming 已提交
939
        return ret;
W
wenjun 已提交
940 941
    }

Y
yinjiaming 已提交
942 943 944 945
    d = (*f == '\0') ? opendir("/") : opendir(f);
    if (d == NULL) {
        perror("opendir error");
        return VFS_ERROR;
W
wenjun 已提交
946 947
    }

Y
yinjiaming 已提交
948 949 950 951
    while (1) {
        dirent = readdir(d);
        if (dirent == NULL) {
            break;
W
wenjun 已提交
952 953
        }

Y
yinjiaming 已提交
954 955 956
        ret = strcpy_s(src, PATH_MAX, f);
        if (ret != EOK) {
            goto closedir_out;
W
wenjun 已提交
957 958
        }

Y
yinjiaming 已提交
959 960 961 962 963
        ret = os_wildcard_match(dirent->d_name, s);
        if (ret == 0) {
            ret = strcat_s(src, sizeof(src), separator);
            if (ret != EOK) {
                goto closedir_out;
W
wenjun 已提交
964
            }
Y
yinjiaming 已提交
965 966 967
            ret = strcat_s(src, sizeof(src), dirent->d_name);
            if (ret != EOK) {
                goto closedir_out;
W
wenjun 已提交
968
            }
Y
yinjiaming 已提交
969 970 971 972 973 974 975 976 977
            if (t == NULL) {
                if (mark == CP_FILE) {
                    ret = os_shell_cmd_do_cp(src, dst);
                } else if (mark == CP_COUNT) {
                    ret = stat(src, &stat_buf);
                    if (ret == 0 && (S_ISREG(stat_buf.st_mode) || S_ISLNK(stat_buf.st_mode))) {
                        (*(int *)dst)++;
                        if ((*(int *)dst) > 1) {
                            break;
W
wenjun 已提交
978 979
                        }
                    }
Y
yinjiaming 已提交
980 981 982 983
                } else {
                    ret = os_wildcard_delete_file_or_dir(src, mark);
                    if (ret == 0) {
                        deleteFlag = 1;
W
wenjun 已提交
984 985
                    }
                }
Y
yinjiaming 已提交
986 987 988 989
            } else {
                ret = strcat_s(src, sizeof(src), separator);
                if (ret != EOK) {
                    goto closedir_out;
W
wenjun 已提交
990
                }
Y
yinjiaming 已提交
991 992 993
                ret = strcat_s(src, sizeof(src), t);
                if (ret != EOK) {
                    goto closedir_out;
W
wenjun 已提交
994
                }
Y
yinjiaming 已提交
995 996 997
                ret = os_wildcard_extract_directory(src, dst, mark);
                if (mark == CP_COUNT && (*(int *)dst) > 1) {
                    break;
W
wenjun 已提交
998 999 1000 1001
                }
            }
        }
    }
Y
yinjiaming 已提交
1002 1003 1004
    (void)closedir(d);
    if (deleteFlag == 1) {
        ret = 0;
W
wenjun 已提交
1005
    }
Y
yinjiaming 已提交
1006
    return ret;
W
wenjun 已提交
1007
closedir_out:
Y
yinjiaming 已提交
1008 1009
    (void)closedir(d);
    return VFS_ERROR;
W
wenjun 已提交
1010 1011 1012 1013
}

int osShellCmdCp(int argc, const char **argv)
{
Y
yinjiaming 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    int  ret;
    const char *src = NULL;
    const char *dst = NULL;
    char *src_fullpath = NULL;
    char *dst_fullpath = NULL;
    struct stat stat_buf;
    int count = 0;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
1024 1025
    }

Y
yinjiaming 已提交
1026
    ERROR_OUT_IF(argc < 2, PRINTK("cp [SOURCEFILE] [DESTFILE]\n"), return -1);
W
wenjun 已提交
1027

Y
yinjiaming 已提交
1028 1029
    src = argv[0];
    dst = argv[1];
W
wenjun 已提交
1030

Y
yinjiaming 已提交
1031
    /* Get source fullpath. */
W
wenjun 已提交
1032

Y
yinjiaming 已提交
1033 1034 1035 1036 1037
    ret = vfs_normalize_path(shell_working_directory, src, &src_fullpath);
    if (ret < 0) {
        set_errno(-ret);
        PRINTK("cp error:%s\n", strerror(errno));
        return -1;
W
wenjun 已提交
1038 1039
    }

Y
yinjiaming 已提交
1040 1041 1042
    if (src[strlen(src) - 1] == '/') {
        PRINTK("cp %s error: Source file can't be a directory.\n", src);
        goto errout_with_srcpath;
W
wenjun 已提交
1043 1044
    }

Y
yinjiaming 已提交
1045
    /* Get dest fullpath. */
W
wenjun 已提交
1046

Y
yinjiaming 已提交
1047 1048 1049 1050 1051
    ret = vfs_normalize_path(shell_working_directory, dst, &dst_fullpath);
    if (ret < 0) {
        set_errno(-ret);
        PRINTK("cp error: can't open %s. %s\n", dst, strerror(errno));
        goto errout_with_srcpath;
W
wenjun 已提交
1052 1053
    }

Y
yinjiaming 已提交
1054
    /* Is dest path exist? */
W
wenjun 已提交
1055

Y
yinjiaming 已提交
1056 1057 1058
    ret = stat(dst_fullpath, &stat_buf);
    if (ret < 0) {
        /* Is dest path a directory? */
W
wenjun 已提交
1059

Y
yinjiaming 已提交
1060 1061 1062
        if (dst[strlen(dst) - 1] == '/') {
            PRINTK("cp error: %s, %s.\n", dst_fullpath, strerror(errno));
            goto errout_with_path;
W
wenjun 已提交
1063
        }
Y
yinjiaming 已提交
1064 1065 1066 1067
    } else {
        if ((S_ISREG(stat_buf.st_mode) || S_ISLNK(stat_buf.st_mode)) && dst[strlen(dst) - 1] == '/') {
            PRINTK("cp error: %s is not a directory.\n", dst_fullpath);
            goto errout_with_path;
W
wenjun 已提交
1068 1069 1070
        }
    }

Y
yinjiaming 已提交
1071 1072 1073 1074 1075 1076
    if (os_is_containers_wildcard(src_fullpath)) {
        if (ret < 0 || S_ISREG(stat_buf.st_mode) || S_ISLNK(stat_buf.st_mode)) {
            char *src_copy = strdup(src_fullpath);
            if (src_copy == NULL) {
                PRINTK("cp error : Out of memory.\n");
                goto errout_with_path;
W
wenjun 已提交
1077
            }
Y
yinjiaming 已提交
1078 1079 1080 1081 1082
            (void)os_wildcard_extract_directory(src_copy, &count, CP_COUNT);
            free(src_copy);
            if (count > 1) {
                PRINTK("cp error : %s is not a directory.\n", dst_fullpath);
                goto errout_with_path;
W
wenjun 已提交
1083 1084
            }
        }
Y
yinjiaming 已提交
1085 1086 1087
        ret = os_wildcard_extract_directory(src_fullpath, dst_fullpath, CP_FILE);
    } else {
        ret = os_shell_cmd_do_cp(src_fullpath, dst_fullpath);
W
wenjun 已提交
1088
    }
Y
yinjiaming 已提交
1089 1090 1091
    free(dst_fullpath);
    free(src_fullpath);
    return ret;
W
wenjun 已提交
1092 1093

errout_with_path:
Y
yinjiaming 已提交
1094
    free(dst_fullpath);
W
wenjun 已提交
1095
errout_with_srcpath:
Y
yinjiaming 已提交
1096 1097
    free(src_fullpath);
    return VFS_ERROR;
W
wenjun 已提交
1098 1099 1100 1101
}

static inline void print_rm_usage(void)
{
Y
yinjiaming 已提交
1102
    PRINTK("rm [FILE] or rm [-r/-R] [FILE]\n");
W
wenjun 已提交
1103 1104 1105 1106
}

int osShellCmdRm(int argc, const char **argv)
{
Y
yinjiaming 已提交
1107 1108 1109 1110 1111 1112
    int  ret = 0;
    char *fullpath = NULL;
    const char *filename = NULL;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
1113 1114
    }

Y
yinjiaming 已提交
1115
    ERROR_OUT_IF(argc != 1 && argc != 2, print_rm_usage(), return -1);
W
wenjun 已提交
1116

Y
yinjiaming 已提交
1117 1118
    if (argc == 2) { // 2: arguments include "-r" or "-R"
        ERROR_OUT_IF(strcmp(argv[0], "-r") != 0 && strcmp(argv[0], "-R") != 0, print_rm_usage(), return -1);
W
wenjun 已提交
1119

Y
yinjiaming 已提交
1120 1121 1122
        filename = argv[1];
        ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
        ERROR_OUT_IF(ret < 0, set_err(-ret, "rm error"), return -1);
W
wenjun 已提交
1123

Y
yinjiaming 已提交
1124 1125 1126 1127
        if (os_is_containers_wildcard(fullpath)) {
            ret = os_wildcard_extract_directory(fullpath, NULL, RM_RECURSIVER);
        } else {
            ret = os_shell_cmd_do_rmdir(fullpath);
W
wenjun 已提交
1128
        }
Y
yinjiaming 已提交
1129 1130 1131 1132
    } else {
        filename = argv[0];
        ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
        ERROR_OUT_IF(ret < 0, set_err(-ret, "rm error"), return -1);
W
wenjun 已提交
1133

Y
yinjiaming 已提交
1134 1135 1136 1137
        if (os_is_containers_wildcard(fullpath)) {
            ret = os_wildcard_extract_directory(fullpath, NULL, RM_FILE);
        } else {
            ret = unlink(fullpath);
W
wenjun 已提交
1138 1139
        }
    }
Y
yinjiaming 已提交
1140 1141
    if (ret == -1) {
        perror("rm error");
W
wenjun 已提交
1142
    }
Y
yinjiaming 已提交
1143 1144
    free(fullpath);
    return 0;
W
wenjun 已提交
1145 1146 1147 1148
}

int osShellCmdRmdir(int argc, const char **argv)
{
Y
yinjiaming 已提交
1149 1150 1151 1152 1153 1154
    int  ret;
    char *fullpath = NULL;
    const char *filename = NULL;
    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
1155 1156
    }

Y
yinjiaming 已提交
1157
    ERROR_OUT_IF(argc == 0, PRINTK("rmdir [DIRECTORY]\n"), return -1);
W
wenjun 已提交
1158

Y
yinjiaming 已提交
1159 1160 1161
    filename = argv[0];
    ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
    ERROR_OUT_IF(ret < 0, set_err(-ret, "rmdir error"), return -1);
W
wenjun 已提交
1162

Y
yinjiaming 已提交
1163 1164 1165 1166
    if (os_is_containers_wildcard(fullpath)) {
        ret = os_wildcard_extract_directory(fullpath, NULL, RM_DIR);
    } else {
        ret = rmdir(fullpath);
W
wenjun 已提交
1167
    }
Y
yinjiaming 已提交
1168 1169
    if (ret == -1) {
        PRINTK("rmdir %s failed. Error: %s.\n", fullpath, strerror(errno));
W
wenjun 已提交
1170
    }
Y
yinjiaming 已提交
1171
    free(fullpath);
W
wenjun 已提交
1172

Y
yinjiaming 已提交
1173
    return 0;
W
wenjun 已提交
1174 1175 1176 1177
}

int osShellCmdSync(int argc, const char **argv)
{
Y
yinjiaming 已提交
1178
    ERROR_OUT_IF(argc > 0, PRINTK("\nUsage: sync\n"), return -1);
W
wenjun 已提交
1179

Y
yinjiaming 已提交
1180 1181
    sync();
    return 0;
W
wenjun 已提交
1182 1183 1184 1185
}

int osShellCmdLsfd(int argc, const char **argv)
{
Y
yinjiaming 已提交
1186
    ERROR_OUT_IF(argc > 0, PRINTK("\nUsage: lsfd\n"), return -1);
W
wenjun 已提交
1187

Y
yinjiaming 已提交
1188
    lsfd();
W
wenjun 已提交
1189

Y
yinjiaming 已提交
1190
    return 0;
W
wenjun 已提交
1191 1192 1193 1194
}

int checkNum(const char *arg)
{
Y
yinjiaming 已提交
1195 1196 1197
    int i = 0;
    if (arg == NULL) {
        return -1;
W
wenjun 已提交
1198
    }
Y
yinjiaming 已提交
1199 1200
    if (arg[0] == '-') {
        /* exclude the '-' */
W
wenjun 已提交
1201

Y
yinjiaming 已提交
1202
        i = 1;
W
wenjun 已提交
1203
    }
Y
yinjiaming 已提交
1204 1205 1206
    for (; arg[i] != 0; i++) {
        if (!isdigit(arg[i])) {
            return -1;
W
wenjun 已提交
1207 1208
        }
    }
Y
yinjiaming 已提交
1209
    return 0;
W
wenjun 已提交
1210 1211
}

Y
YOUR_NAME 已提交
1212
#ifdef LOSCFG_KERNEL_SYSCALL
W
wenjun 已提交
1213 1214
int osShellCmdSu(int argc, const char **argv)
{
Y
yinjiaming 已提交
1215 1216
    int su_uid;
    int su_gid;
W
wenjun 已提交
1217

Y
yinjiaming 已提交
1218 1219
    if (argc == 0) {
        /* for su root */
W
wenjun 已提交
1220

Y
yinjiaming 已提交
1221 1222 1223 1224 1225 1226
        su_uid = 0;
        su_gid = 0;
    } else {
        ERROR_OUT_IF((argc != 2), PRINTK("su [uid_num] [gid_num]\n"), return -1);
        ERROR_OUT_IF((checkNum(argv[0]) != 0) || (checkNum(argv[1]) != 0), /* check argv is digit */
        PRINTK("check uid_num and gid_num is digit\n"), return -1);
W
wenjun 已提交
1227

Y
yinjiaming 已提交
1228 1229
        su_uid = atoi(argv[0]);
        su_gid = atoi(argv[1]);
W
wenjun 已提交
1230

Y
yinjiaming 已提交
1231 1232
        ERROR_OUT_IF((su_uid < 0) || (su_uid > 60000) || (su_gid < 0) ||
            (su_gid > 60000), PRINTK("uid_num or gid_num out of range!they should be [0~60000]\n"), return -1);
W
wenjun 已提交
1233 1234
    }

Y
yinjiaming 已提交
1235 1236 1237
    SysSetUserID(su_uid);
    SysSetGroupID(su_gid);
    return 0;
W
wenjun 已提交
1238
}
Y
YOUR_NAME 已提交
1239
#endif
W
wenjun 已提交
1240 1241 1242

int osShellCmdChmod(int argc, const char **argv)
{
Y
yinjiaming 已提交
1243 1244 1245 1246 1247 1248 1249 1250
    int i = 0;
    int mode = 0;
    int ret;
    char *fullpath = NULL;
    const char *filename = NULL;
    struct IATTR attr = {0};
    char *shell_working_directory = NULL;
    const char *p = NULL;
W
wenjun 已提交
1251 1252
#define MODE_BIT 3 /* 3 bits express 1 mode */

Y
yinjiaming 已提交
1253
    ERROR_OUT_IF((argc != 2), PRINTK("Usage: chmod <MODE> [FILE]\n"), return -1);
W
wenjun 已提交
1254

Y
yinjiaming 已提交
1255 1256 1257 1258 1259 1260 1261
    p = argv[0];
    while (p[i]) {
        if ((p[i] <= '7') && (p[i] >= '0')) {
            mode = ((uint)mode << MODE_BIT) | (uint)(p[i] - '0');
        } else {
            PRINTK("check the input <MODE>\n");
            return -1;
W
wenjun 已提交
1262
        }
Y
yinjiaming 已提交
1263
        i++;
W
wenjun 已提交
1264
    }
Y
yinjiaming 已提交
1265
    filename = argv[1];
W
wenjun 已提交
1266

Y
yinjiaming 已提交
1267 1268 1269
    shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
1270
    }
Y
yinjiaming 已提交
1271 1272
    ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
    ERROR_OUT_IF(ret < 0, set_err(-ret, "chmod error\n"), return -1);
W
wenjun 已提交
1273

Y
yinjiaming 已提交
1274 1275 1276 1277 1278 1279 1280
    attr.attr_chg_mode = mode;
    attr.attr_chg_valid = CHG_MODE; /* change mode */
    ret = chattr(fullpath, &attr);
    if (ret < 0) {
        free(fullpath);
        PRINTK("chmod error! %s\n", strerror(errno));
        return ret;
W
wenjun 已提交
1281 1282
    }

Y
yinjiaming 已提交
1283 1284
    free(fullpath);
    return 0;
W
wenjun 已提交
1285 1286 1287 1288
}

int osShellCmdChown(int argc, const char **argv)
{
Y
yinjiaming 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    int ret;
    char *fullpath = NULL;
    const char *filename = NULL;
    struct IATTR attr;
    uid_t owner = -1;
    gid_t group = -1;
    attr.attr_chg_valid = 0;

    ERROR_OUT_IF(((argc != 2) && (argc != 3)), PRINTK("Usage: chown [OWNER] [GROUP] FILE\n"), return -1);
    if (argc == 2) { // 2: chown owner of file
        ERROR_OUT_IF((checkNum(argv[0]) != 0), PRINTK("check OWNER is digit\n"), return -1);
        owner = atoi(argv[0]);
        filename = argv[1];
    }
    if (argc == 3) { // 3: chown both owner and group
        ERROR_OUT_IF((checkNum(argv[0]) != 0), PRINTK("check OWNER is digit\n"), return -1);
        ERROR_OUT_IF((checkNum(argv[1]) != 0), PRINTK("check GROUP is digit\n"), return -1);
        owner = atoi(argv[0]);
        group = atoi(argv[1]);
        filename = argv[2];
    }

    if (group != -1) {
        attr.attr_chg_gid = group;
        attr.attr_chg_valid |= CHG_GID;
    }
    if (owner != -1) {
        attr.attr_chg_uid = owner;
        attr.attr_chg_valid |= CHG_UID;
    }

    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
W
wenjun 已提交
1323
    }
Y
yinjiaming 已提交
1324 1325
    ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
    ERROR_OUT_IF(ret < 0, set_err(-ret, "chown error\n"), return -1);
W
wenjun 已提交
1326

Y
yinjiaming 已提交
1327 1328 1329 1330 1331
    ret = chattr(fullpath, &attr);
    if (ret < 0) {
        free(fullpath);
        PRINTK("chown error! %s\n", strerror(errno));
        return ret;
W
wenjun 已提交
1332 1333
    }

Y
yinjiaming 已提交
1334 1335
    free(fullpath);
    return 0;
W
wenjun 已提交
1336 1337 1338 1339
}

int osShellCmdChgrp(int argc, const char **argv)
{
Y
yinjiaming 已提交
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
    int ret;
    char *fullpath = NULL;
    const char *filename = NULL;
    struct IATTR attr;
    gid_t group;
    attr.attr_chg_valid = 0;
    ERROR_OUT_IF((argc != 2), PRINTK("Usage: chgrp GROUP FILE\n"), return -1);
    ERROR_OUT_IF((checkNum(argv[0]) != 0), PRINTK("check GROUP is digit\n"), return -1);
    group = atoi(argv[0]);
    filename = argv[1];

    if (group != -1) {
        attr.attr_chg_gid = group;
        attr.attr_chg_valid |= CHG_GID;
    }

    char *shell_working_directory = OsShellGetWorkingDirectory();
    if (shell_working_directory == NULL) {
        return -1;
    }
    ret = vfs_normalize_path(shell_working_directory, filename, &fullpath);
    ERROR_OUT_IF(ret < 0, set_err(-ret, "chmod error"), return -1);
W
wenjun 已提交
1362

Y
yinjiaming 已提交
1363 1364 1365 1366 1367 1368
    ret = chattr(fullpath, &attr);
    if (ret < 0) {
        free(fullpath);
        PRINTK("chgrp error! %s\n", strerror(errno));
        return ret;
    }
W
wenjun 已提交
1369

Y
yinjiaming 已提交
1370 1371
    free(fullpath);
    return 0;
W
wenjun 已提交
1372 1373 1374 1375 1376 1377
}

#ifdef LOSCFG_SHELL_CMD_DEBUG
SHELLCMD_ENTRY(lsfd_shellcmd, CMD_TYPE_EX, "lsfd", XARGS, (CmdCallBackFunc)osShellCmdLsfd);
SHELLCMD_ENTRY(statfs_shellcmd, CMD_TYPE_EX, "statfs", XARGS, (CmdCallBackFunc)osShellCmdStatfs);
SHELLCMD_ENTRY(touch_shellcmd, CMD_TYPE_EX, "touch", XARGS, (CmdCallBackFunc)osShellCmdTouch);
Y
YOUR_NAME 已提交
1378
#ifdef LOSCFG_KERNEL_SYSCALL
W
wenjun 已提交
1379 1380
SHELLCMD_ENTRY(su_shellcmd, CMD_TYPE_EX, "su", XARGS, (CmdCallBackFunc)osShellCmdSu);
#endif
Y
YOUR_NAME 已提交
1381
#endif
G
Guangyao Ma 已提交
1382
SHELLCMD_ENTRY(sync_shellcmd, CMD_TYPE_EX, "sync", XARGS, (CmdCallBackFunc)osShellCmdSync);
W
wenjun 已提交
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
SHELLCMD_ENTRY(ls_shellcmd, CMD_TYPE_EX, "ls", XARGS, (CmdCallBackFunc)osShellCmdLs);
SHELLCMD_ENTRY(pwd_shellcmd, CMD_TYPE_EX, "pwd", XARGS, (CmdCallBackFunc)osShellCmdPwd);
SHELLCMD_ENTRY(cd_shellcmd, CMD_TYPE_EX, "cd", XARGS, (CmdCallBackFunc)osShellCmdCd);
SHELLCMD_ENTRY(cat_shellcmd, CMD_TYPE_EX, "cat", XARGS, (CmdCallBackFunc)osShellCmdCat);
SHELLCMD_ENTRY(rm_shellcmd, CMD_TYPE_EX, "rm", XARGS, (CmdCallBackFunc)osShellCmdRm);
SHELLCMD_ENTRY(rmdir_shellcmd, CMD_TYPE_EX, "rmdir", XARGS, (CmdCallBackFunc)osShellCmdRmdir);
SHELLCMD_ENTRY(mkdir_shellcmd, CMD_TYPE_EX, "mkdir", XARGS, (CmdCallBackFunc)osShellCmdMkdir);
SHELLCMD_ENTRY(chmod_shellcmd, CMD_TYPE_EX, "chmod", XARGS, (CmdCallBackFunc)osShellCmdChmod);
SHELLCMD_ENTRY(chown_shellcmd, CMD_TYPE_EX, "chown", XARGS, (CmdCallBackFunc)osShellCmdChown);
SHELLCMD_ENTRY(chgrp_shellcmd, CMD_TYPE_EX, "chgrp", XARGS, (CmdCallBackFunc)osShellCmdChgrp);
SHELLCMD_ENTRY(mount_shellcmd, CMD_TYPE_EX, "mount", XARGS, (CmdCallBackFunc)osShellCmdMount);
SHELLCMD_ENTRY(umount_shellcmd, CMD_TYPE_EX, "umount", XARGS, (CmdCallBackFunc)osShellCmdUmount);
SHELLCMD_ENTRY(cp_shellcmd, CMD_TYPE_EX, "cp", XARGS, (CmdCallBackFunc)osShellCmdCp);
#endif