Skip to content

Commit 6f83797

Browse files
committed
Allow the path to file to include symlinks
Symlinks are not followed below the root, to avoid potential recursions Windows is an exception, since we don't have an 'lstat' equivalent
1 parent f7b7c25 commit 6f83797

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

Diff for: extract-xiso.c

+28-18
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@
352352
#define lseek _lseeki64
353353
#define mkdir(a, b) _mkdir(a)
354354
#define stat _stat64
355+
#define lstat _stat64
355356
#define realpath(a, b) _fullpath(b, a, _MAX_PATH)
356357

357358
#define bswap_16(x) _byteswap_ushort(x)
@@ -986,35 +987,39 @@ int create_xiso( char *in_root_directory, const char *in_output_directory, dir_n
986987
write_tree_context wt_context = { 0 };
987988
uint32_t start_sector = 0;
988989
int i = 0, n = 0, xiso = -1, err = 0;
989-
char *cwd = NULL, *iso_name = NULL, *xiso_path = NULL, *iso_dir = NULL, *real_path = NULL;
990+
char *cwd = NULL, *iso_name = NULL, *xiso_path = NULL, *iso_dir = NULL, *real_path = NULL, *in_dir_path = NULL;
990991

991992
s_total_bytes = s_total_files = 0;
992993

993994
if ( ( cwd = getcwd( NULL, 0 ) ) == NULL ) mem_err();
994-
if ( ! err ) {
995-
if ( ! in_root ) {
996-
i = (int)strlen(in_root_directory) - 1;
997-
if ( in_root_directory[i] == '/' || in_root_directory[i] == '\\' ) in_root_directory[i] = 0;
998-
if ((iso_dir = strrchr(in_root_directory, PATH_CHAR))) iso_dir++;
999-
else iso_dir = in_root_directory;
1000-
1001-
iso_name = in_name ? in_name : iso_dir;
1002-
} else {
995+
if (!err) {
996+
if (!in_root) {
997+
if ((in_dir_path = realpath(in_root_directory, NULL)) == NULL) misc_err("unable to get absolute path of %s: %s", in_root_directory, strerror(errno));
998+
if (!err) {
999+
// Remember not to free in_dir_path until iso_dir is not needed anymore
1000+
if ((iso_dir = strrchr(in_dir_path, PATH_CHAR)) != NULL) iso_dir++;
1001+
else iso_dir = in_dir_path;
1002+
iso_name = in_name ? in_name : iso_dir;
1003+
}
1004+
}
1005+
else {
10031006
iso_dir = iso_name = in_root_directory;
10041007
}
10051008
}
1006-
if ( ! err ) {
1007-
if ( ! *iso_dir ) iso_dir = PATH_CHAR_STR;
1008-
if ( ! iso_name || ! *iso_name ) iso_name = "root";
1009-
else if ( iso_name[ 1 ] == ':' ) { iso_name[ 1 ] = iso_name[ 0 ]; ++iso_name; }
1010-
if (!err && (real_path = realpath(in_output_directory ? in_output_directory : ".", NULL)) == NULL) misc_err("unable to get absolute path of %s: %s", real_path, strerror(errno));
1009+
if(!err) {
1010+
if (!iso_dir || !*iso_dir) iso_dir = PATH_CHAR_STR;
1011+
if (!iso_name || !*iso_name) iso_name = "root";
1012+
else if (iso_name[1] == ':') { iso_name[1] = iso_name[0]; ++iso_name; }
1013+
1014+
if (in_output_directory == NULL) in_output_directory = ".";
1015+
if (!err && (real_path = realpath(in_output_directory, NULL)) == NULL) misc_err("unable to get absolute path of %s: %s", in_output_directory, strerror(errno));
10111016
if (!err && (asprintf(&xiso_path, "%s%c%s%s", real_path, PATH_CHAR, iso_name, in_name ? "" : ".iso")) == -1) mem_err();
10121017
if (real_path) {
10131018
free(real_path);
10141019
real_path = NULL;
10151020
}
10161021

1017-
if (!err && !in_root && chdir(in_root_directory) == -1) chdir_err(in_root_directory);
1022+
if (!err && !in_root && chdir(in_dir_path) == -1) chdir_err(in_dir_path);
10181023
}
10191024
if ( ! err ) {
10201025
exiso_log( "\n%s %s%s:\n", in_root ? "rewriting" : "creating", iso_name, in_name ? "" : ".iso" );
@@ -1090,7 +1095,7 @@ int create_xiso( char *in_root_directory, const char *in_output_directory, dir_n
10901095
if ( ! err && write( xiso, XISO_HEADER_DATA, XISO_HEADER_DATA_LENGTH ) != XISO_HEADER_DATA_LENGTH ) write_err();
10911096

10921097
if ( ! err && ! in_root ) {
1093-
if ( chdir( ".." ) == -1 ) chdir_err( ".." );
1098+
if (chdir("..") == -1) chdir_err("..");
10941099
}
10951100
if (!err && (root.filename = strdup(iso_dir)) == NULL) mem_err();
10961101
if (!err) {
@@ -1138,6 +1143,11 @@ int create_xiso( char *in_root_directory, const char *in_output_directory, dir_n
11381143
if (root.filename) free(root.filename);
11391144
if (root.filename_cp1252) free(root.filename_cp1252);
11401145

1146+
if (in_dir_path) {
1147+
free(in_dir_path);
1148+
in_dir_path = NULL;
1149+
}
1150+
11411151
if ( cwd ) {
11421152
if ( chdir( cwd ) == -1 ) chdir_err( cwd );
11431153
free( cwd );
@@ -2032,7 +2042,7 @@ int generate_avl_tree_local( dir_node_avl **out_root, int *io_n ) {
20322042
avl->filename = NULL;
20332043
} else if ((avl->filename_cp1252 = getCP1252String(p->d_name)) == NULL) mem_err();
20342044
}
2035-
if ( ! err && stat( p->d_name, &sb ) == -1 ) read_err();
2045+
if ( ! err && lstat( p->d_name, &sb ) == -1 ) read_err();
20362046
if ( ! err ) {
20372047
if ( S_ISDIR( sb.st_mode ) ) {
20382048
empty_dir = false;

0 commit comments

Comments
 (0)