Changeset f9d8c3a in mainline


Ignore:
Timestamp:
2011-09-25T17:23:34Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1db44ea, 6d8c4654
Parents:
113093a
git-author:
Maurizio Lombardi <m.lombardi85@…> (2011-09-25 17:23:34)
git-committer:
Jakub Jermar <jakub@…> (2011-09-25 17:23:34)
Message:

mkfile sparse file support

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/modules/mkfile/mkfile.c

    r113093a rf9d8c3a  
    5454static struct option const long_options[] = {
    5555        {"size", required_argument, 0, 's'},
     56        {"sparse", no_argument, 0, 'p'},
    5657        {"help", no_argument, 0, 'h'},
    5758        {0, 0, 0, 0}
     
    6970                "  -h, --help       A short option summary\n"
    7071                "  -s, --size sz    Size of the file\n"
     72                "  -p, --sparse     Create a sparse file\n"
    7173                "\n"
    7274                "Size is a number followed by 'k', 'm' or 'g' for kB, MB, GB.\n"
     
    115117        ssize_t file_size;
    116118        ssize_t total_written;
    117         ssize_t to_write, rc;
     119        ssize_t to_write, rc, rc2 = 0;
    118120        char *file_name;
    119121        void *buffer;
     122        bool create_sparse = false;
    120123
    121124        file_size = 0;
     
    124127
    125128        for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
    126                 c = getopt_long(argc, argv, "s:h", long_options, &opt_ind);
     129                c = getopt_long(argc, argv, "ps:h", long_options, &opt_ind);
    127130                switch (c) {
    128131                case 'h':
    129132                        help_cmd_mkfile(HELP_LONG);
    130133                        return CMD_SUCCESS;
     134                case 'p':
     135                        create_sparse = true;
     136                        break;
    131137                case 's':
    132138                        file_size = read_size(optarg);
     
    154160                printf("%s: failed to create file %s.\n", cmdname, file_name);
    155161                return CMD_FAILURE;
     162        }
     163
     164        if (create_sparse && file_size > 0) {
     165                const char byte = 0x00;
     166
     167                if ((rc2 = lseek(fd, file_size - 1, SEEK_SET)) < 0)
     168                        goto exit;
     169
     170                rc2 = write(fd, &byte, sizeof(char));
     171                goto exit;
    156172        }
    157173
     
    174190        }
    175191
     192        free(buffer);
     193exit:
    176194        rc = close(fd);
    177         if (rc != 0) {
     195
     196        if (rc != 0 || rc2 < 0) {
    178197                printf("%s: Error writing file (%zd).\n", cmdname, rc);
    179198                return CMD_FAILURE;
    180199        }
    181 
    182         free(buffer);
    183200
    184201        return CMD_SUCCESS;
Note: See TracChangeset for help on using the changeset viewer.