1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "symlink.h"
17 #include "file_fs_impl.h"
18
19 #include <cstring>
20 #include <fcntl.h>
21 #include <tuple>
22 #include <unistd.h>
23
24 namespace OHOS {
25 namespace CJSystemapi {
26
27 using namespace std;
28
Symlink(const std::string & target,const std::string & srcPath)29 int SymlinkImpl::Symlink(const std::string& target, const std::string& srcPath)
30 {
31 LOGI("FS_TEST::SymlinkImpl::Symlink start");
32
33 std::unique_ptr<uv_fs_t, decltype(CommonFunc::FsReqCleanup)*> symlink_req = {
34 new (std::nothrow) uv_fs_t, CommonFunc::FsReqCleanup };
35 if (!symlink_req) {
36 LOGE("Failed to request heap memory.");
37 return ENOMEM;
38 }
39 int ret = uv_fs_symlink(nullptr, symlink_req.get(), target.c_str(), srcPath.c_str(), 0, nullptr);
40 if (ret < 0) {
41 LOGE("Failed to create a link for old path");
42 return ret;
43 }
44
45 LOGI("FS_TEST::SymlinkImpl::Symlink success");
46 return ret;
47 }
48
49 } // namespace CJSystemapi
50 } // namespace OHOS
51