vfs_utime.c 3.9 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
 *
 * 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.
 */

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include "errno.h"
#include "vfs_config.h"
#include "sys/stat.h"
M
mucor 已提交
39
#include "vnode.h"
40
#include "fs/mount.h"
W
wenjun 已提交
41 42 43
#include "string.h"
#include "stdlib.h"
#include "utime.h"
44
#include <fcntl.h>
W
wenjun 已提交
45 46 47 48 49

/****************************************************************************
 * Global Functions
 ****************************************************************************/

W
wangchenyang 已提交
50
int utime(const char *path, const struct utimbuf *ptimes)
W
wenjun 已提交
51
{
W
wangchenyang 已提交
52
    int ret;
W
wenjun 已提交
53
    char *fullpath = NULL;
W
wangchenyang 已提交
54
    struct Vnode *vnode = NULL;
W
wenjun 已提交
55
    time_t cur_sec;
W
wangchenyang 已提交
56
    struct IATTR attr = {0};
W
wenjun 已提交
57 58 59 60

    /* Sanity checks */

    if (path == NULL) {
W
wangchenyang 已提交
61
        ret = -EINVAL;
W
wenjun 已提交
62 63 64 65
        goto errout;
    }

    if (!path[0]) {
W
wangchenyang 已提交
66
        ret = -ENOENT;
W
wenjun 已提交
67 68 69 70 71 72 73 74
        goto errout;
    }

    ret = vfs_normalize_path((const char *)NULL, path, &fullpath);
    if (ret < 0) {
        goto errout;
    }

W
wangchenyang 已提交
75 76 77 78 79
    /* Get the vnode for this file */
    VnodeHold();
    ret = VnodeLookup(fullpath, &vnode, 0);
    if (ret != LOS_OK) {
        VnodeDrop();
W
wenjun 已提交
80 81 82
        goto errout_with_path;
    }

83 84 85 86 87 88
    if ((vnode->originMount) && (vnode->originMount->mountFlags & MS_RDONLY)) {
        VnodeDrop();
        ret = -EROFS;
        goto errout_with_path;
    }

W
wangchenyang 已提交
89 90 91 92 93 94
    if (vnode->vop && vnode->vop->Chattr) {
        if (ptimes == NULL) {
            /* get current seconds */
            cur_sec = time(NULL);
            attr.attr_chg_atime = cur_sec;
            attr.attr_chg_mtime = cur_sec;
W
wenjun 已提交
95
        } else {
W
wangchenyang 已提交
96 97
            attr.attr_chg_atime = ptimes->actime;
            attr.attr_chg_mtime = ptimes->modtime;
W
wenjun 已提交
98
        }
W
wangchenyang 已提交
99 100 101 102 103 104 105 106 107 108
        attr.attr_chg_valid = CHG_ATIME | CHG_MTIME;
        ret = vnode->vop->Chattr(vnode, &attr);
        if (ret != OK) {
            VnodeDrop();
            goto errout_with_path;
        }
    } else {
        ret = -ENOSYS;
        VnodeDrop();
        goto errout_with_path;
W
wenjun 已提交
109
    }
W
wangchenyang 已提交
110
    VnodeDrop();
W
wenjun 已提交
111 112 113

    /* Successfully stat'ed the file */
    free(fullpath);
W
wangchenyang 已提交
114

W
wenjun 已提交
115 116 117 118 119 120 121
    return OK;

    /* Failure conditions always set the errno appropriately */

errout_with_path:
    free(fullpath);
errout:
W
wangchenyang 已提交
122

W
wenjun 已提交
123
    if (ret != 0) {
W
wangchenyang 已提交
124
        set_errno(-ret);
W
wenjun 已提交
125 126 127
    }
    return VFS_ERROR;
}