-
Notifications
You must be signed in to change notification settings - Fork 6
/
split.c
141 lines (111 loc) · 3.7 KB
/
split.c
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
/* $Id: split.c,v 1.3 2005/05/13 18:52:06 harbourn Exp $
* dcfldd - The Enhanced Forensic DD
* By Nicholas Harbour
*/
/* Copyright (C) 85, 90, 91, 1995-2001, 2005 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* GNU dd originally written by Paul Rubin, David MacKenzie, and Stuart Kemp. */
#define _GNU_SOURCE 1
#include <stdio.h>
#include "dcfldd.h"
#include "split.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "full-write.h"
#include "log.h"
/* for portability, use these arrays for referencing numbers and letters */
static char *numbers = "0123456789";
#define NUM_NUMBERS 10
static char *letters = "abcdefghijklmnopqrstuvwxyz";
#define NUM_LETTERS 26
static char *getext(char *, int);
static int maxsplits(char *);
/* Generate a split file extension string based on
* the specified format string and a given number
*/
static char *getext(char *fmt, int num)
{
int fmtlen = strlen(fmt);
int i;
char *retval;
assert(fmtlen > 0);
retval = malloc(fmtlen);
/* Fill the retval in reverse while constantly dividing num apropriately */
for (i = fmtlen - 1; i >= 0; i--) {
int x;
if (fmt[i] == 'a') {
x = num % NUM_LETTERS;
retval[i] = letters[x];
num = num / NUM_LETTERS;
} else {
x = num % NUM_NUMBERS;
retval[i] = numbers[x];
num = num / NUM_NUMBERS;
}
}
retval[fmtlen] = '\0';
return retval;
}
/* Given a format string, determine the maximum number of splits
* that can be used. */
static int maxsplits(char *fmt)
{
int fmtlen = strlen(fmt);
int i;
int retval = 1;
assert(fmtlen > 0);
for (i = fmtlen - 1; i >= 0; i--)
retval *= fmt[i] == 'a' ? NUM_LETTERS : NUM_NUMBERS;
return retval;
}
/* Open the next extension in a split sequence */
static void open_split(split_t *split)
{
int fd;
int splitnum = split->total_bytes / split->max_bytes;
mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
char *ext, *fname;
ext = getext(split->format, splitnum);
asprintf(&fname, "%s.%s", split->name, ext);
free(ext);
fd = open(fname, O_WRONLY | O_CREAT, perms);
if (fd < 0)
syscall_error(fname);
split->currfd = fd;
split->curr_bytes = 0;
free(fname);
}
int split_write(split_t *split, const char *buf, size_t len)
{
off_t left = split->max_bytes - split->curr_bytes;
int nwritten = 0;
if (left == 0 || split->currfd == -1) {
open_split(split);
left = split->max_bytes;
}
if (len <= left) {
nwritten = full_write(split->currfd, buf, len);
split->total_bytes += nwritten;
split->curr_bytes += nwritten;
} else {
nwritten = full_write(split->currfd, buf, left);
split->total_bytes += nwritten;
split->curr_bytes += nwritten;
nwritten += split_write(split, &buf[nwritten], len - nwritten);
}
return nwritten;
}