Changeset 28ed2d89 in mainline


Ignore:
Timestamp:
2024-12-13T08:44:05Z (10 months ago)
Author:
Nataliia Korop <n.corop08@…>
Children:
fb31682
Parents:
31d2aee
git-author:
Nataliia Korop <n.corop08@…> (2024-11-23 08:41:19)
git-committer:
Nataliia Korop <n.corop08@…> (2024-12-13 08:44:05)
Message:

add force option for existing files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/pcapctl/main.c

    r31d2aee r28ed2d89  
    3838#include <arg_parse.h>
    3939#include <getopt.h>
     40#include <vfs/vfs.h>
    4041
    4142#include "pcapdump_client.h"
     
    4344#define NAME "pcapctl"
    4445#define DEFAULT_DEV_NUM 0
     46#define DEFAULT_OPS_NUM 0
    4547#define DECIMAL_SYSTEM 10
    4648
     
    9698        { "list", no_argument, 0, 'l' },
    9799        { "help", no_argument, 0, 'h' },
    98         { "outfile", required_argument, 0, 'f' },
     100        { "outfile", required_argument, 0, 'o' },
    99101        { "start", no_argument, 0, 'r' },
    100102        { "stop", no_argument, 0, 't' },
    101         { "ops", required_argument, 0, 'o' },
     103        { "ops", required_argument, 0, 'p' },
     104        { "force", no_argument, 0, 'f'},
    102105        { 0, 0, 0, 0 }
    103106};
     107
     108static bool file_exists(const char *path)
     109{
     110        vfs_stat_t stats;
     111
     112    if (vfs_stat_path(path, &stats) != EOK)
     113        return false;
     114
     115    return true;
     116}
    104117
    105118static void usage(void)
     
    122135        bool start = false;
    123136        bool stop = false;
    124         int dev_number = -1;
    125         int ops_number = -1;
     137        int dev_number = DEFAULT_DEV_NUM;
     138        int ops_number = DEFAULT_OPS_NUM;
     139        bool forced = false;
    126140        const char *output_file_name = "";
    127141        int idx = 0;
     
    132146        }
    133147        while (ret != -1) {
    134                 ret = getopt_long(argc, argv, "d:lhf:rt", opts, &idx);
     148                ret = getopt_long(argc, argv, "d:lho:rtp:f", opts, &idx);
    135149                switch (ret) {
    136150                case 'd':
     
    150164                        usage();
    151165                        return 0;
    152                 case 'f':
     166                case 'o':
    153167                        output_file_name = optarg;
    154168                        break;
     
    159173                        stop = true;
    160174                        break;
    161                 case 'o':
     175                case 'p':
    162176                        char* ops_inval;
    163177                        long ops_result = strtol(optarg, &ops_inval, DECIMAL_SYSTEM);
    164178                        ops_number = (int)ops_result;
    165179                        break;
     180                case 'f':
     181                        forced = true;
     182                        break;
    166183                }
    167184        }
     
    170187
    171188        if (start) {
     189
     190                if (file_exists(output_file_name) && !forced)
     191                {
     192                        printf("File %s already exists. If you want to write to it, then use flag --force.\n", output_file_name);
     193                        return 0;
     194                }
     195
    172196                /* start with dev number and name */
    173197                start_dumping(&dev_number, output_file_name, &ops_number);
     
    175199                /* stop with dev number */
    176200                stop_dumping(&dev_number);
     201        } else {
     202                usage();
    177203        }
    178204        return 0;
Note: See TracChangeset for help on using the changeset viewer.