#include #include #include #include #include #include #include #include #include void print_usage(const char *prog_name) { printf("用法:%s [选项] <文件路径>\n", prog_name); printf("选项:\n"); printf(" --set-access-count --min <最小值> -max <最大值>\n"); printf(" --set-access-time --start \"HH:MM\" --end \"HH:MM\"\n"); printf(" --set-size-limit --size <大小 (字节) >\n"); printf(" --help 显示此帮助信息\n"); } int set_access_count(const char *file_path, int min, int max) { int ret; ret = setxattr(file_path, "user.access_count_min", &min, sizeof(int), 0); if (ret < 0) { perror("设置 access_count_min 失败"); return -1; } ret = setxattr(file_path, "user.access_count_max", &max, sizeof(int), 0); if (ret < 0) { perror("设置 access_count_max 失败"); return -1; } return 0; } int set_access_time(const char *file_path, const char *start_time, const char *end_time) { int ret; ret = setxattr(file_path, "user.access_time_start", start_time, strlen(start_time) + 1, 0); if (ret < 0) { perror("设置 access_time_start 失败"); return -1; } ret = setxattr(file_path, "user.access_time_end", end_time, strlen(end_time) + 1, 0); if (ret < 0) { perror("设置 access_time_end 失败"); return -1; } return 0; } int set_size_limit(const char *file_path, int size_limit) { int ret; ret = setxattr(file_path, "user.size_limit", &size_limit, sizeof(int), 0); if (ret < 0) { perror("设置 size_limit 失败"); return -1; } return 0; } int main(int argc, char *argv[]) { int opt; int option_index = 0; int min = -1, max =-1, size_limit = -1; char *start_time = NULL, *end_time = NULL; char *file_path = NULL; static struct option long_options[] = { {"set-access-count", no_argument, 0, 'c'}, {"set-access-time", no_argument, 0, 't'}, {"set-size-limit", no_argument, 0, 's'}, {"min", required_argument, 0, 'm'}, {"max", required_argument, 0, 'x'}, {"start", required_argument, 0, 'a'}, {"end", required_argument, 0, 'e'}, {"size", required_argument, 0, 'z'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; int set_count_flag = 0, set_time_flag = 0, set_size_flag = 0; if (argc < 2) { print_usage(argv[0]); exit(EXIT_FAILURE); } while ((opt = getopt_long(argc, argv, "ctsm:x:a:e:z:h", long_options, &option_index)) != -1) { switch (opt) { case 'c': set_count_flag = 1; break; case 't': set_time_flag = 1; break; case 's': set_size_flag = 1; break; case 'm': min = atoi(optarg); break; case 'x': max = atoi(optarg); break; case 'a': start_time = optarg; break; case 'e': end_time = optarg; break; case 'z': size_limit = atoi(optarg); break; case 'h': default: print_usage(argv[0]); exit(EXIT_FAILURE); } } if (optind < argc) { file_path = argv[optind]; } else { fprintf(stderr, "错误:未指定文件路径\n"); print_usage(argv[0]); exit(EXIT_FAILURE); } if (set_count_flag) { if (min == -1 || max == -1) { fprintf(stderr, "错误:设置访问次数限制时必须指定 --min 和 --max\n"); exit(EXIT_FAILURE); } if (set_access_count(file_path, min, max) < 0) { exit(EXIT_FAILURE); } else { printf("成功设置访问次数限制\n"); } } if (set_time_flag) { if (start_time == NULL || end_time == NULL) { fprintf(stderr, "错误:设置访问时间时必须指定 --start 和 --end\n"); exit(EXIT_FAILURE); } if (set_access_time(file_path, start_time, end_time) < 0) { exit(EXIT_FAILURE); } else { printf("成功设置访问时间区间\n"); } } if (set_size_flag) { if (size_limit == -1) { fprintf(stderr, "错误:设置文件大小限制时必须指定 --size\n"); exit(EXIT_FAILURE); } if (set_size_limit(file_path, size_limit) < 0) { exit(EXIT_FAILURE); } else { printf("成功设置文件大小限制\n"); } } return 0; }