-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
copy.c
103 lines (90 loc) · 2.82 KB
/
copy.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
/*
* ldminfo - Part of the Linux-NTFS project.
*
* Copyright (C) 2001-2012 Richard Russon <ldm@flatcap.org>
*
* Documentation is available at http://linux-ntfs.sourceforge.net/ldm
*
* 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 of the License, 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 (in the main directory of the Linux-NTFS source
* in the file COPYING); if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "ldminfo.h"
#include "common.h"
/**
* copy_database - Save the LDM Database to a file
*/
void copy_database (int device, char *name, struct ldmdb *ldb)
{
unsigned char *buffer;
int fpart = 0; /* Partition table + primary PRIVHEAD */
int fdata = 0; /* LDM Database */
long long start = ldb->ph.config_start * 512;
long long size = ldb->ph.config_size * 512;
if (!name)
return;
buffer = kmalloc (size, 0);
if (!buffer)
return;
sprintf ((char*) buffer, "%.58s.part", basename (name));
fpart = open ((char*)buffer, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fpart < 0) {
printf ("Couldn't open output file: %s\n", buffer);
goto out;
}
sprintf ((char*) buffer, "%.58s.data", basename (name));
fdata = open ((char*)buffer, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fdata < 0) {
printf ("Couldn't open output file: %s\n", buffer);
goto out;
}
/* First copy the partition table and primary PRIVHEAD to fpart */
if (lseek (device, 0, SEEK_SET) < 0) {
printf ("lseek to %d failed\n", 0);
goto out;
}
if (read (device, buffer, 4096) < 4096) {
printf ("Couldn't read the partition table and primary PRIVHEAD\n");
goto out;
}
if (write (fpart, buffer, 4096) < 4096) {
printf ("Couldn't write the partition table and primary PRIVHEAD\n");
goto out;
}
if (lseek (device, start, SEEK_SET) < 0) {
printf ("lseek to %lld failed\n", size);
goto out;
}
if (read (device, buffer, size) < size) {
printf ("Couldn't read the ldm database\n");
goto out;
}
if (write (fdata, buffer, size) < size) {
printf ("Couldn't write the ldm database\n");
goto out;
}
printf ("Successfully copied the LDM data\n");
out:
kfree (buffer);
close (fpart);
close (fdata);
}