summaryrefslogtreecommitdiff
path: root/cd-tool.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cd-tool.cpp')
-rw-r--r--cd-tool.cpp104
1 files changed, 63 insertions, 41 deletions
diff --git a/cd-tool.cpp b/cd-tool.cpp
index 4b601ba..9f10eaa 100644
--- a/cd-tool.cpp
+++ b/cd-tool.cpp
@@ -25,9 +25,10 @@
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
+#include "Input.h"
+#include "Output.h"
#include "cdutils.h"
#include "generic.h"
-#include "fileutils.h"
int lga = 0;
struct option long_options[] = {
@@ -58,7 +59,7 @@ void showhelp(void) {
int main(int argc, char ** argv) {
int type = GUESS, c, size, force = 0, sector;
- int iso_r, iso_w;
+ Handle * iso_r, * iso_w = 0;
char * ppf = 0, * iso_name = 0, * arg1 = 0, * arg2 = 0, * f;
verbosity = M_WARNING;
@@ -90,17 +91,14 @@ int main(int argc, char ** argv) {
iso_name = argv[optind++];
- if ((iso_r = open(iso_name, O_RDONLY)) < 0) {
- printm(M_ERROR, "Failed to open %s for reading.\n", iso_name);
- exit(-1);
- }
-
if (argc == optind) {
showhelp();
printm(M_ERROR, "Need a command to execute.\n");
exit(-1);
}
+ iso_r = new Input(iso_name);
+
get_iso_infos(iso_r);
if (!strcmp(argv[optind], "infos")) {
@@ -113,6 +111,7 @@ int main(int argc, char ** argv) {
if ((argc - 1) != optind) {
showhelp();
printm(M_ERROR, "printdir needs one argument.\n");
+ delete iso_r;
exit(-1);
}
arg1 = argv[optind];
@@ -120,85 +119,87 @@ int main(int argc, char ** argv) {
free(f);
if (!dir.R) {
printm(M_ERROR, "Path %s was not found on iso.\n", arg1);
+ delete iso_r;
exit(-1);
}
if (!(dir.Flags & 2)) {
printm(M_ERROR, "Path %s design a file and not a directory.\n", arg1);
+ delete iso_r;
exit(-1);
}
show_head_entry();
show_dir(iso_r, &dir);
} else if (!strcmp(argv[optind], "extract-file")) {
struct DirEntry dir;
- int file;
+ Handle * file;
optind++;
if ((argc - 2) != optind) {
showhelp();
printm(M_ERROR, "extract-file needs two arguments.\n");
+ delete iso_r;
exit(-1);
}
arg1 = argv[optind++];
arg2 = argv[optind++];
- if ((file = open(arg1, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
- printm(M_ERROR, "Failed to open file %s for writing.\n", arg1);
- exit(-1);
- }
+ file = new Output(arg1);
dir = find_path(iso_r, f = strdup(arg2));
free(f);
if (!dir.R) {
printm(M_ERROR, "Path %s was not found on iso.\n", arg2);
+ delete iso_r;
+ delete file;
exit(-1);
}
printm(M_STATUS, "Reading path %s to file %s.\n", arg2, arg1);
read_file(iso_r, file, type, dir.Sector, dir.Size);
+ delete file;
} else if (!strcmp(argv[optind], "extract")) {
- int file;
+ Handle * file;
optind++;
if ((argc - 3) != optind) {
showhelp();
printm(M_ERROR, "extract needs three arguments.\n");
+ delete iso_r;
exit(-1);
}
arg1 = argv[optind++];
size = atoi(argv[optind++]);
sector = atoi(argv[optind++]);
- if ((file = open(arg1, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
- printm(M_ERROR, "Failed to open file %s for writing.\n", arg1);
- exit(-1);
- }
+ file = new Output(arg1);
printm(M_STATUS, "Reading %i bytes from sector %i to file %s.\n", size, sector, arg1);
read_file(iso_r, file, type, sector, size);
+ delete file;
} else if (!strcmp(argv[optind], "insert-file")) {
struct DirEntry dir, * d;
unsigned char * buffer;
- int file;
+ Handle * file;
int old_type;
- if ((iso_w = open(iso_name, O_WRONLY)) < 0) {
- printm(M_ERROR, "Failed to open %s for writing.\n", iso_name);
- exit(-1);
- }
- lseek(iso_w, 0, SEEK_SET);
if (ppf) {
if (open_ppf(ppf, iso_r, "Created by CD-Tool") < 0) {
printm(M_ERROR, "Failed to open file %s for writing.\n", ppf);
}
+ } else {
+ iso_w = new Output(iso_name, 0, 0);
}
optind++;
if ((argc - 2) != optind) {
showhelp();
printm(M_ERROR, "insert-file needs two arguments.\n");
+ delete iso_r;
+ if (ppf) {
+ close_ppf();
+ } else {
+ delete iso_w;
+ }
exit(-1);
}
arg1 = argv[optind++];
arg2 = argv[optind++];
- if ((file = open(arg1, O_RDONLY)) < 0) {
- printm(M_ERROR, "Failed to open file %s for reading.\n", arg1);
- exit(-1);
- }
+ file = new Input(arg1);
dir = find_path(iso_r, f = strdup(arg2));
free(f);
if (!dir.R) {
@@ -212,11 +213,18 @@ int main(int argc, char ** argv) {
type = old_type;
}
if (((dir.Size / sec_sizes[old_type]) + ((dir.Size % sec_sizes[old_type]) ? 1 : 0)) <
- ((filesize(file) / sec_sizes[type]) + ((filesize(file) % sec_sizes[type]) ? 1 : 0))) {
+ (((size_t) file->GetSize() / sec_sizes[type]) + (((size_t) file->GetSize() % sec_sizes[type]) ? 1 : 0))) {
if (force) {
- printm(M_WARNING, "New file too big: %i vs %i in %s.\n", dir.Size, filesize(file), sec_modes[type]);
+ printm(M_WARNING, "New file too big: %i vs %i in " + sec_modes[type] + ".\n", dir.Size, file->GetSize());
} else {
- printm(M_ERROR, "New file too big: %i vs %i in %s.\n", dir.Size, filesize(file), sec_modes[type]);
+ printm(M_ERROR, "New file too big: %i vs %i in " + sec_modes[type] + ".\n", dir.Size, file->GetSize());
+ delete iso_r;
+ if (ppf) {
+ close_ppf();
+ } else {
+ delete iso_w;
+ }
+ delete file;
exit(-1);
}
}
@@ -231,43 +239,57 @@ int main(int argc, char ** argv) {
f = arg2;
}
d = find_dir_entry(iso_r, &buffer, &dir, f);
- d->Size = filesize(file);
+ d->Size = file->GetSize();
write_datas(iso_r, iso_w, buffer, GUESS, dir.Sector, dir.Size);
free(buffer);
- } else if (!strcmp(argv[optind], "insert")) {
- int file;
- if ((iso_w = open(iso_name, O_WRONLY)) < 0) {
- printm(M_ERROR, "Failed to open %s for writing.\n", iso_name);
- exit(-1);
+ delete file;
+ if (ppf) {
+ close_ppf();
+ } else {
+ delete iso_w;
}
- lseek(iso_w, 0, SEEK_SET);
+ } else if (!strcmp(argv[optind], "insert")) {
+ Handle * file;
if (ppf) {
if (open_ppf(ppf, iso_r, "Created by CD-Tool") < 0) {
printm(M_ERROR, "Failed to open file %s for writing.\n", ppf);
}
+ } else {
+ iso_w = new Output(iso_name, 0, 0);
}
optind++;
if ((argc - 2) != optind) {
showhelp();
printm(M_ERROR, "insert needs two arguments.\n");
+ if (ppf) {
+ close_ppf();
+ } else {
+ delete iso_w;
+ }
+ delete iso_r;
exit(-1);
}
arg1 = argv[optind++];
sector = atoi(argv[optind++]);
- if ((file = open(arg1, O_RDONLY)) < 0) {
- printm(M_ERROR, "Failed to open file %s for reading.\n", arg1);
- exit(-1);
- }
+ file = new Input(arg1);
printm(M_STATUS, "Writing file %s at sector %i.\n", arg1, sector);
write_file(iso_r, iso_w, file, type, sector);
+ if (ppf) {
+ close_ppf();
+ } else {
+ delete iso_w;
+ }
+ delete file;
} else {
showhelp();
printm(M_ERROR, "Command %s unknow.\n", argv[optind]);
+ delete iso_r;
exit(-1);
}
+ delete iso_r;
exit(0);
}