xref: /test/xts/tools/lite/checksum/src/main.c (revision 31c75014)
1/*
2 * Copyright (c) 2021 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/*
17 * Description: 计算单个文件或目录下多文件进行计算SHA256校验和
18 */
19
20#include <dirent.h>
21#include <errno.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
28#include "../include/checksum_file.h"
29
30#define BUFFER_SIZE 4096
31
32int main(int argc, char *argv[])
33{
34    int count = 2;
35    char *filePath = argv[1];
36    struct stat sBuf;
37
38    if (argc != count) {
39        printf("error: the argc is 2, but argc is %d.\n", argc);
40    }
41
42    int result = stat(filePath, &sBuf); // 获取文件信息,把信息放到sBuf中
43    if (result == -1) {
44        printf("error: fail to get the stat of %s: %s.\n", filePath, strerror(errno));
45        return RESULT_FAILED;
46    }
47
48    if (S_ISDIR(sBuf.st_mode)) {
49        CalcMultiFilesSha256(filePath); // 输入的文件路径为目录
50    } else {
51        CalcSingleFileSha256(filePath); // 输入的文件路径是普通文件
52    }
53
54    int fileNum = GetFileTotalNum();
55    printf("the checksum test success,the total number of files is %d.\n", fileNum);
56
57    return RESULT_SUCCESS;
58}
59