1526fd984Sopenharmony_ci/* 2526fd984Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 3526fd984Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4526fd984Sopenharmony_ci * you may not use this file except in compliance with the License. 5526fd984Sopenharmony_ci * You may obtain a copy of the License at 6526fd984Sopenharmony_ci * 7526fd984Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8526fd984Sopenharmony_ci * 9526fd984Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10526fd984Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11526fd984Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12526fd984Sopenharmony_ci * See the License for the specific language governing permissions and 13526fd984Sopenharmony_ci * limitations under the License. 14526fd984Sopenharmony_ci */ 15526fd984Sopenharmony_ci 16526fd984Sopenharmony_ci#include <dirent.h> 17526fd984Sopenharmony_ci#include <errno.h> 18526fd984Sopenharmony_ci#include <unistd.h> 19526fd984Sopenharmony_ci#include <sys/types.h> 20526fd984Sopenharmony_ci#include <stdio.h> 21526fd984Sopenharmony_ci#include <sys/stat.h> 22526fd984Sopenharmony_ci#include <stdbool.h> 23526fd984Sopenharmony_ci 24526fd984Sopenharmony_ci#include <securec.h> 25526fd984Sopenharmony_ci 26526fd984Sopenharmony_ci/** 27526fd984Sopenharmony_ci * This bin file is used in l1 device triggered when device initializing, in order to support the compatibility for old 28526fd984Sopenharmony_ci * keys with version 1. 29526fd984Sopenharmony_ci*/ 30526fd984Sopenharmony_ci 31526fd984Sopenharmony_ci#define HUKS_SERVICE_UID 12 32526fd984Sopenharmony_ci#define DIR_TYPE 4 33526fd984Sopenharmony_ci#define DEFAULT_PATH_LEN 1024 34526fd984Sopenharmony_ci#define DEFAULT_READ_BUFFER 4096 35526fd984Sopenharmony_ci#define DEFAULT_HUKS_PATH_PERMISSION 0700 36526fd984Sopenharmony_ci 37526fd984Sopenharmony_cistatic void ChangeDirAndFilesPerm(const char *path) 38526fd984Sopenharmony_ci{ 39526fd984Sopenharmony_ci DIR *dir; 40526fd984Sopenharmony_ci struct dirent *ptr; 41526fd984Sopenharmony_ci dir = opendir(path); 42526fd984Sopenharmony_ci if (dir == NULL) { 43526fd984Sopenharmony_ci return; 44526fd984Sopenharmony_ci } 45526fd984Sopenharmony_ci int ret = EOK; 46526fd984Sopenharmony_ci while ((ptr = readdir(dir)) != NULL) { 47526fd984Sopenharmony_ci if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) { 48526fd984Sopenharmony_ci continue; 49526fd984Sopenharmony_ci } 50526fd984Sopenharmony_ci char curPath[DEFAULT_PATH_LEN] = { 0 }; 51526fd984Sopenharmony_ci 52526fd984Sopenharmony_ci ret = strcpy_s(curPath, DEFAULT_PATH_LEN, path); 53526fd984Sopenharmony_ci if (ret != EOK) { 54526fd984Sopenharmony_ci break; 55526fd984Sopenharmony_ci } 56526fd984Sopenharmony_ci ret = strcat_s(curPath, DEFAULT_PATH_LEN, "/"); 57526fd984Sopenharmony_ci if (ret != EOK) { 58526fd984Sopenharmony_ci break; 59526fd984Sopenharmony_ci } 60526fd984Sopenharmony_ci ret = strcat_s(curPath, DEFAULT_PATH_LEN, ptr->d_name); 61526fd984Sopenharmony_ci if (ret != EOK) { 62526fd984Sopenharmony_ci break; 63526fd984Sopenharmony_ci } 64526fd984Sopenharmony_ci 65526fd984Sopenharmony_ci ret = chown(curPath, HUKS_SERVICE_UID, HUKS_SERVICE_UID); 66526fd984Sopenharmony_ci if (ret != EOK) { 67526fd984Sopenharmony_ci break; 68526fd984Sopenharmony_ci } 69526fd984Sopenharmony_ci if ((ptr->d_type != DIR_TYPE)) { 70526fd984Sopenharmony_ci if (chmod(curPath, S_IRUSR | S_IWUSR) < 0) { 71526fd984Sopenharmony_ci break; 72526fd984Sopenharmony_ci } 73526fd984Sopenharmony_ci } else { 74526fd984Sopenharmony_ci if (chmod(curPath, S_IRWXU) < 0) { 75526fd984Sopenharmony_ci break; 76526fd984Sopenharmony_ci } 77526fd984Sopenharmony_ci ChangeDirAndFilesPerm(curPath); 78526fd984Sopenharmony_ci } 79526fd984Sopenharmony_ci } 80526fd984Sopenharmony_ci if (ret != EOK) { 81526fd984Sopenharmony_ci printf("chmod dir and file failed! errno = 0x%x \n", errno); 82526fd984Sopenharmony_ci } 83526fd984Sopenharmony_ci (void)closedir(dir); 84526fd984Sopenharmony_ci} 85526fd984Sopenharmony_ci 86526fd984Sopenharmony_cistatic void MoveOldFileToNew(const char *srcPath, const char *tarPath) 87526fd984Sopenharmony_ci{ 88526fd984Sopenharmony_ci char buffer[DEFAULT_READ_BUFFER]; 89526fd984Sopenharmony_ci FILE *in, *out; 90526fd984Sopenharmony_ci if ((in = fopen(srcPath, "r")) == NULL) { 91526fd984Sopenharmony_ci printf("open source file failed !\n"); 92526fd984Sopenharmony_ci return; 93526fd984Sopenharmony_ci } 94526fd984Sopenharmony_ci if ((out = fopen(tarPath, "w")) == NULL) { 95526fd984Sopenharmony_ci printf("open target file failed !\n"); 96526fd984Sopenharmony_ci (void)fclose(in); 97526fd984Sopenharmony_ci return; 98526fd984Sopenharmony_ci } 99526fd984Sopenharmony_ci int len; 100526fd984Sopenharmony_ci while ((len = fread(buffer, 1, DEFAULT_READ_BUFFER, in)) > 0) { 101526fd984Sopenharmony_ci int size = fwrite(buffer, 1, len, out); 102526fd984Sopenharmony_ci if (size != len) { 103526fd984Sopenharmony_ci printf("move old file to new path failed!"); 104526fd984Sopenharmony_ci (void)fclose(out); 105526fd984Sopenharmony_ci (void)fclose(in); 106526fd984Sopenharmony_ci return; 107526fd984Sopenharmony_ci } 108526fd984Sopenharmony_ci } 109526fd984Sopenharmony_ci (void)fclose(out); 110526fd984Sopenharmony_ci (void)fclose(in); 111526fd984Sopenharmony_ci 112526fd984Sopenharmony_ci (void)remove(srcPath); 113526fd984Sopenharmony_ci} 114526fd984Sopenharmony_ci 115526fd984Sopenharmony_cistatic int ConstructSrcAndTargetPath(char *curPath, char *desPath, struct dirent *ptr, 116526fd984Sopenharmony_ci const char *srcPath, const char *tarPath) 117526fd984Sopenharmony_ci{ 118526fd984Sopenharmony_ci int ret = strcpy_s(curPath, DEFAULT_PATH_LEN, srcPath); 119526fd984Sopenharmony_ci if (ret != EOK) { 120526fd984Sopenharmony_ci return ret; 121526fd984Sopenharmony_ci } 122526fd984Sopenharmony_ci ret = strcat_s(curPath, DEFAULT_PATH_LEN, "/"); 123526fd984Sopenharmony_ci if (ret != EOK) { 124526fd984Sopenharmony_ci return ret; 125526fd984Sopenharmony_ci } 126526fd984Sopenharmony_ci 127526fd984Sopenharmony_ci ret = strcat_s(curPath, DEFAULT_PATH_LEN, ptr->d_name); 128526fd984Sopenharmony_ci if (ret != EOK) { 129526fd984Sopenharmony_ci return ret; 130526fd984Sopenharmony_ci } 131526fd984Sopenharmony_ci 132526fd984Sopenharmony_ci ret = strcpy_s(desPath, DEFAULT_PATH_LEN, tarPath); 133526fd984Sopenharmony_ci if (ret != EOK) { 134526fd984Sopenharmony_ci return ret; 135526fd984Sopenharmony_ci } 136526fd984Sopenharmony_ci ret = strcat_s(desPath, DEFAULT_PATH_LEN, "/"); 137526fd984Sopenharmony_ci if (ret != EOK) { 138526fd984Sopenharmony_ci return ret; 139526fd984Sopenharmony_ci } 140526fd984Sopenharmony_ci 141526fd984Sopenharmony_ci ret = strcat_s(desPath, DEFAULT_PATH_LEN, ptr->d_name); 142526fd984Sopenharmony_ci if (ret != EOK) { 143526fd984Sopenharmony_ci return ret; 144526fd984Sopenharmony_ci } 145526fd984Sopenharmony_ci return EOK; 146526fd984Sopenharmony_ci} 147526fd984Sopenharmony_ci 148526fd984Sopenharmony_cistatic void MoveOldFolderToNew(const char *srcPath, const char *tarPath) 149526fd984Sopenharmony_ci{ 150526fd984Sopenharmony_ci if (!opendir(tarPath)) { 151526fd984Sopenharmony_ci if (mkdir(tarPath, DEFAULT_HUKS_PATH_PERMISSION) != 0) { 152526fd984Sopenharmony_ci printf("mkdir failed! errno = 0x%x \n", errno); 153526fd984Sopenharmony_ci return; 154526fd984Sopenharmony_ci } 155526fd984Sopenharmony_ci } 156526fd984Sopenharmony_ci struct dirent *ptr; 157526fd984Sopenharmony_ci DIR *dir = opendir(srcPath); 158526fd984Sopenharmony_ci int ret = EOK; 159526fd984Sopenharmony_ci while ((ptr = readdir(dir)) != NULL) { 160526fd984Sopenharmony_ci if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) { 161526fd984Sopenharmony_ci continue; 162526fd984Sopenharmony_ci } 163526fd984Sopenharmony_ci char curPath[DEFAULT_PATH_LEN] = { 0 }; 164526fd984Sopenharmony_ci char desPath[DEFAULT_PATH_LEN] = { 0 }; 165526fd984Sopenharmony_ci 166526fd984Sopenharmony_ci ret = ConstructSrcAndTargetPath(curPath, desPath, ptr, srcPath, tarPath); 167526fd984Sopenharmony_ci if (ret != EOK) { 168526fd984Sopenharmony_ci printf("construct src and target path failed!"); 169526fd984Sopenharmony_ci break; 170526fd984Sopenharmony_ci } 171526fd984Sopenharmony_ci if (ptr->d_type == DIR_TYPE) { 172526fd984Sopenharmony_ci MoveOldFolderToNew(curPath, desPath); 173526fd984Sopenharmony_ci } else { 174526fd984Sopenharmony_ci MoveOldFileToNew(curPath, desPath); 175526fd984Sopenharmony_ci } 176526fd984Sopenharmony_ci } 177526fd984Sopenharmony_ci (void)closedir(dir); 178526fd984Sopenharmony_ci if (ret != EOK) { 179526fd984Sopenharmony_ci printf("chmod dir and file failed! errno = 0x%x \n", errno); 180526fd984Sopenharmony_ci } 181526fd984Sopenharmony_ci (void)rmdir(srcPath); 182526fd984Sopenharmony_ci} 183526fd984Sopenharmony_ci 184526fd984Sopenharmony_ciint main(void) 185526fd984Sopenharmony_ci{ 186526fd984Sopenharmony_ci const char *oldPath = "/storage/maindata"; 187526fd984Sopenharmony_ci const char *newPath = "/storage/data/service/el1/public/huks_service/maindata"; 188526fd984Sopenharmony_ci 189526fd984Sopenharmony_ci // move directories and files form old path to new path 190526fd984Sopenharmony_ci MoveOldFolderToNew(oldPath, newPath); 191526fd984Sopenharmony_ci 192526fd984Sopenharmony_ci // change directories and files permission 193526fd984Sopenharmony_ci ChangeDirAndFilesPerm(newPath); 194526fd984Sopenharmony_ci} 195