lfs.c

来自「lustre 1.6.5 source code」· C语言 代码 · 共 1,844 行 · 第 1/5 页

C
1,844
字号
                                neg_opt = 2;                      break;                case 'A':                        xtime = &param.atime;                        xsign = &param.asign;                case 'C':                        if (c == 'C') {                                xtime = &param.ctime;                                xsign = &param.csign;                        }                case 'M':                        if (c == 'M') {                                xtime = &param.mtime;                                xsign = &param.msign;                        }                        new_fashion = 1;                        if (neg_opt) {                                if (optarg[0] == '-')                                        optarg[0] = '+';                                else if (optarg[0] == '+')                                        optarg[0] = '-';                                else {                                        str[0] = '-';                                        str[1] = '\0';                                        strcat(str, optarg);                                        optarg = str;                                }                        }                        ret = set_time(&t, xtime, optarg);                        if (ret == INT_MAX)                                return -1;                        if (ret)                                *xsign = ret;                        break;                case 'D':                        new_fashion = 1;                        param.maxdepth = strtol(optarg, 0, 0);                        break;                case 'n':                        new_fashion = 1;                        param.pattern = (char *)optarg;                        param.exclude_pattern = !!neg_opt;                        break;                case 'O': {                        char *buf, *token, *next, *p;                        int len;                        len = strlen((char *)optarg);                        buf = malloc(len+1);                        if (buf == NULL)                                return -ENOMEM;                        strcpy(buf, (char *)optarg);                        if (param.num_alloc_obds == 0) {                                param.obduuid = (struct obd_uuid *)malloc(FIND_MAX_OSTS *                                                       sizeof(struct obd_uuid));                                if (param.obduuid == NULL)                                        return -ENOMEM;                                param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;                        }                        for (token = buf; token && *token; token = next) {                                p = strchr(token, ',');                                next = 0;                                if (p) {                                        *p = 0;                                        next = p+1;                                }                                strcpy((char *)&param.obduuid[param.num_obds++].uuid,                                       token);                        }                        if (buf)                                free(buf);                        break;                }                case 'p':                        new_fashion = 1;                        param.zeroend = 1;                        break;                case 'P':                        break;                case 'q':                        new_fashion = 0;                        param.quiet++;                        param.verbose = 0;                        break;                case 'r':                        new_fashion = 0;                        param.recursive = 1;                        break;                case 't':                        param.exclude_type = !!neg_opt;                        switch(optarg[0]) {                        case 'b': param.type = S_IFBLK; break;                        case 'c': param.type = S_IFCHR; break;                        case 'd': param.type = S_IFDIR; break;                        case 'f': param.type = S_IFREG; break;                        case 'l': param.type = S_IFLNK; break;                        case 'p': param.type = S_IFIFO; break;                        case 's': param.type = S_IFSOCK; break;#ifdef S_IFDOOR /* Solaris only */                        case 'D': param.type = S_IFDOOR; break;#endif                        default: fprintf(stderr, "error: %s: bad type '%s'\n",                                         argv[0], optarg);                                 return CMD_HELP;                        };                        break;                case 's':                        if (neg_opt) {                                if (optarg[0] == '-')                                        optarg[0] = '+';                                else if (optarg[0] == '+')                                        optarg[0] = '-';                                else {                                        str[0] = '-';                                        str[1] = '\0';                                        strcat(str, optarg);                                        optarg = str;                                }                        }                        if (optarg[0] == '+')                                param.size_sign = -1;                        else if (optarg[0] == '-')                                param.size_sign = +1;                        if (param.size_sign)                                optarg++;                        ret = parse_size(optarg, &param.size,&param.size_units);                        if (ret) {                                fprintf(stderr,"error: bad size '%s'\n",                                        optarg);                                return ret;                        }                        param.size_check = 1;                        break;                case 'v':                        new_fashion = 0;                        param.verbose++;                        param.quiet = 0;                        break;                case '?':                        return CMD_HELP;                default:                        fprintf(stderr, "error: %s: option '%s' unrecognized\n",                                argv[0], argv[optind - 1]);                        return CMD_HELP;                };        }        if (pathstart == -1) {                fprintf(stderr, "error: %s: no filename|pathname\n",                                argv[0]);                return CMD_HELP;        } else if (pathend == -1) {                /* no options */                pathend = argc;        }        if (new_fashion) {                param.quiet = 1;        } else {                static int deprecated_warning;                if (!deprecated_warning) {                        fprintf(stderr, "lfs find: -q, -r, -v options "                                "deprecated.  Use 'lfs getstripe' instead.\n");                        deprecated_warning = 1;                }                if (!param.recursive && param.maxdepth == -1)                        param.maxdepth = 1;        }        do {                if (new_fashion)                        ret = llapi_find(argv[pathstart], &param);                else                        ret = llapi_getstripe(argv[pathstart], &param);        } while (++pathstart < pathend && !ret);        if (ret)                fprintf(stderr, "error: %s failed for %s.\n",                        argv[0], argv[optind - 1]);        if (param.obduuid && param.num_alloc_obds)                free(param.obduuid);        return ret;}static int lfs_getstripe(int argc, char **argv){        struct option long_opts[] = {                {"obd", 1, 0, 'O'},                {"quiet", 0, 0, 'q'},                {"recursive", 0, 0, 'r'},                {"verbose", 0, 0, 'v'},                {0, 0, 0, 0}        };        char short_opts[] = "hO:qrv";        int c, rc;        struct find_param param = { 0 };        optind = 0;        while ((c = getopt_long(argc, argv, short_opts,                                long_opts, NULL)) != -1) {                switch (c) {                case 'O':                        if (param.obduuid) {                                fprintf(stderr,                                        "error: %s: only one obduuid allowed",                                        argv[0]);                                return CMD_HELP;                        }                        param.obduuid = (struct obd_uuid *)optarg;                        break;                case 'q':                        param.quiet++;                        param.verbose = 0;                        break;                case 'r':                        param.recursive = 1;                        break;                case 'v':                        param.verbose++;                        param.quiet = 0;                        break;                case '?':                        return CMD_HELP;                default:                        fprintf(stderr, "error: %s: option '%s' unrecognized\n",                                argv[0], argv[optind - 1]);                        return CMD_HELP;                }        }        if (optind >= argc)                return CMD_HELP;        param.maxdepth = param.recursive ? -1 : 1;        do {                rc = llapi_getstripe(argv[optind], &param);        } while (++optind < argc && !rc);        if (rc)                fprintf(stderr, "error: %s failed for %s.\n",                         argv[0], argv[optind - 1]);        return rc;}static int lfs_osts(int argc, char **argv){        FILE *fp;        struct mntent *mnt = NULL;        struct obd_uuid *obduuid = NULL;        struct find_param param;        int rc=0;        if (argc != 1)                return CMD_HELP;        fp = setmntent(MOUNTED, "r");        if (fp == NULL) {                 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,                        strerror (errno));        } else {                mnt = getmntent(fp);                memset(&param, 0, sizeof(param));                param.obduuid = obduuid;                while (feof(fp) == 0 && ferror(fp) ==0) {                        if (llapi_is_lustre_mnt(mnt)) {                                rc = llapi_getstripe(mnt->mnt_dir, &param);                                if (rc)                                        fprintf(stderr,                                               "error: %s: failed on %s\n",                                               argv[0], mnt->mnt_dir);                        }                        mnt = getmntent(fp);                }                endmntent(fp);        }        return rc;}#define COOK(value)                                                     \({                                                                      \        int radix = 0;                                                  \        while (value > 1024) {                                          \                value /= 1024;                                          \                radix++;                                                \        }                                                               \        radix;                                                          \})#define UUF     "%-20s"#define CSF     "%9s"#define CDF     "%9llu"#define HSF     "%8s"#define HDF     "%6.1f"#define RSF     "%5s"#define RDF     "%4d%%"static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len){        char rpath[PATH_MAX] = {'\0'};        struct mntent *mnt;        int rc, len, out_len = 0;        if (!realpath(path, rpath)) {                rc = -errno;                fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",                        path, strerror(-rc));                return rc;        }        len = 0;        mnt = getmntent(fp);        while (feof(fp) == 0 && ferror(fp) == 0) {                if (llapi_is_lustre_mnt(mnt)) {                        len = strlen(mnt->mnt_dir);                        if (len > out_len &&                            !strncmp(rpath, mnt->mnt_dir, len)) {                                out_len = len;                                memset(mntdir, 0, dir_len);                                strncpy(mntdir, mnt->mnt_dir, dir_len);                        }                }                mnt = getmntent(fp);        }        if (out_len > 0)                return 0;        fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);        return -EINVAL;}static int showdf(char *mntdir, struct obd_statfs *stat,                  char *uuid, int ishow, int cooked,                  char *type, int index, int rc){        long long avail, used, total;        double ratio = 0;        char *suffix = "KMGTPEZY";        /* Note if we have >2^64 bytes/fs these buffers will need to be grown */        char tbuf[20], ubuf[20], abuf[20], rbuf[20];        if (!uuid || !stat)                return -EINVAL;        switch (rc) {        case 0:                if (ishow) {                        avail = stat->os_ffree;                        used = stat->os_files - stat->os_ffree;                        total = stat->os_files;                } else {                        int shift = cooked ? 0 : 10;                        avail = (stat->os_bavail * stat->os_bsize) >> shift;                        used  = ((stat->os_blocks - stat->os_bfree) *                                 stat->os_bsize) >> shift;                        total = (stat->os_blocks * stat->os_bsize) >> shift;                }                if (total > 0)                        ratio = (double)used / (double)total;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?