Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 206 lines (157 sloc) 4.204 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
#include "pstore/read-write.h"
#include "pstore/builtins.h"
#include "pstore/builtins-common.h"
#include "pstore/segment.h"
#include "pstore/column.h"
#include "pstore/extent.h"
#include "pstore/header.h"
#include "pstore/table.h"
#include "pstore/core.h"
#include "pstore/die.h"

#include <sys/types.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <getopt.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>

#define EXTENT_LEN MiB(16)

static char *input_file;
static char *table_ref;
static char *column_ref;
static uint64_t extent_len;

static bool pstore_column_matches(struct pstore_column *self, char *ref)
{
if (ref)
return id_or_name_matches(self->column_id, self->name, ref);

return true;
}

static bool pstore_table_matches(struct pstore_table *self, char *ref)
{
if (ref)
return id_or_name_matches(self->table_id, self->name, ref);

return true;
}

static int extend_column(struct pstore_column *column, int input)
{
struct pstore_extent *extent;
off_t offset;

offset = seek_or_die(input, 0, SEEK_CUR);

extent = pstore_extent_read(column, column->last_extent, input);

column->prev_extent = extent;
column->extent = pstore_extent_new(column, PSTORE_COMP_NONE);

seek_or_die(input, offset, SEEK_SET);

if (pstore_column_preallocate(column, input, extent_len) < 0)
return -1;

if (pstore_extent_write_metadata(column->extent, PSTORE_LAST_EXTENT, input) < 0)
return -1;

return 0;
}

static int extend_table(struct pstore_table *table, int input)
{
unsigned long ndx;

for (ndx = 0; ndx < table->nr_columns; ndx++) {
struct pstore_column *column = table->columns[ndx];

if (pstore_column_matches(column, column_ref)) {
if (extend_column(column, input) < 0)
return -1;
}
}

return 0;
}

static int extend(int input)
{
struct pstore_header *header;
unsigned long ndx;
off_t f_end;

header = pstore_header_read(input);

seek_or_die(input, 0, SEEK_END);

for (ndx = 0; ndx < header->nr_tables; ndx++) {
struct pstore_table *table = header->tables[ndx];

if (pstore_table_matches(table, table_ref)) {
if (extend_table(table, input) < 0)
return -1;
}
}

f_end = seek_or_die(input, 0, SEEK_CUR);

/*
* Write out the header again because offsets to last extents changed.
*/
seek_or_die(input, 0, SEEK_SET);
if (pstore_header_write(header, input) < 0)
die("pstore_header_write");

pstore_header_delete(header);

seek_or_die(input, f_end, SEEK_SET);

return 0;
}

static const struct option options[] = {
{ "column", required_argument, NULL, 'c' },
{ "extent-len", required_argument, NULL, 'e' },
{ "table", required_argument, NULL, 't' },
{ }
};

static void usage(void)
{
printf("\n usage: pstore extend [OPTIONS] INPUT\n");
printf("\n The options are:\n");
printf(" -c, --column REF set column\n");
printf(" -e, --extent-len LENGTH set extent length (default: 16M)\n");
printf(" -t, --table REF set table\n");
printf("\n");
exit(EXIT_FAILURE);
}

static void parse_args(int argc, char *argv[])
{
int ch;

table_ref = NULL;
column_ref = NULL;
extent_len = EXTENT_LEN;

while((ch = getopt_long(argc, argv, "c:e:t:", options, NULL)) != -1) {
switch (ch) {
case 'c':
column_ref = optarg;
break;
case 'e':
extent_len = parse_storage_arg(optarg);
break;
case 't':
table_ref = optarg;
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;

input_file = argv[0];
}

int cmd_extend(int argc, char *argv[])
{
int input;
off_t f_size;

if (argc < 3)
usage();

parse_args(argc - 1, argv + 1);

input = open(input_file, O_RDWR);
if (input < 0)
die("Failed to open input file '%s': %s", input_file, strerror(errno));

f_size = seek_or_die(input, 0, SEEK_END);

if (posix_fallocate(input, f_size, extent_len) != 0)
die("posix_fallocate");

seek_or_die(input, 0, SEEK_SET);

if (extend(input) < 0)
die("extend failed");

f_size = seek_or_die(input, 0, SEEK_CUR);

if (ftruncate(input, f_size) != 0)
die("ftruncate");

if (fsync(input) < 0)
die("fsync");

if (close(input) < 0)
die("close");

return 0;
}
Something went wrong with that request. Please try again.