Skip to content

Commit

Permalink
Fix inconsistent separators in path on Windows builds
Browse files Browse the repository at this point in the history
  • Loading branch information
bfabiszewski committed Feb 25, 2022
1 parent a709688 commit 23e8974
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
46 changes: 24 additions & 22 deletions tests/test.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ markup_dir="${basefile%.*}_markup"
tmp_dir="tmp"
rawml_file="${basefile%.*}.rawml"
md5prog="@MD5PROG@"
mobitool="../tools/mobitool"
mobidrm="../tools/mobidrm"
separator=\/
if [[ "x@WIN32@" == "xyes" ]]; then
runfile=${testfile//\//\\}
separator=\\
else
runfile=${testfile}
fi
mobitool="..${separator}tools${separator}mobitool"
mobidrm="..${separator}tools${separator}mobidrm"
pid=
do_md5=1
is_encrypted=0
Expand Down Expand Up @@ -73,8 +75,8 @@ if [[ -z "${md5prog}" ]]; then
else
testfile_md5=$(${md5prog} "${testfile}")
testfile_md5=${testfile_md5%% *}
md5file_markup="@srcdir@/md5/${testfile_md5}_markup.md5"
md5file_rawml="@srcdir@/md5/${testfile_md5}_rawml.md5"
md5file_markup="@srcdir@${separator}md5${separator}${testfile_md5}_markup.md5"
md5file_rawml="@srcdir@${separator}md5${separator}${testfile_md5}_rawml.md5"
fi

# create tmp dir
Expand All @@ -84,45 +86,45 @@ mkdir -p "${tmp_dir}"
rm -rf "${tmp_dir}/${markup_dir}"
log "Running ${mobitool} -o \"${tmp_dir}\" -s ${options} \"${runfile}\""
${mobitool} -o "${tmp_dir}" -s ${options} "${runfile}" || die "Recreating source failed, mobitool error ($?)" $?
[[ -d "${tmp_dir}/${markup_dir}" ]] || die "Recreating source failed" 1
[[ -d "${tmp_dir}${separator}${markup_dir}" ]] || die "Recreating source failed" 1

# verify checksums
if [[ "${do_md5}" -eq "1" ]]; then
if [[ -f "${md5file_markup}" ]]; then
(
cd "${tmp_dir}/${markup_dir}" || die "Could not change directory to ${tmp_dir}/${markup_dir}" $?
rm -f "../${testfile_md5}.md5"
${md5prog} * > "../${testfile_md5}.md5"
diff -w "../${testfile_md5}.md5" "../../${md5file_markup}" || die "Wrong md5 checksum in ${markup_dir}" $?
rm -f "../${testfile_md5}.md5"
cd "${tmp_dir}${separator}${markup_dir}" || die "Could not change directory to ${tmp_dir}${separator}${markup_dir}" $?
rm -f "..${separator}${testfile_md5}.md5"
${md5prog} * > "..${separator}${testfile_md5}.md5"
diff -w "..${separator}${testfile_md5}.md5" "..${separator}..${separator}${md5file_markup}" || die "Wrong md5 checksum in ${markup_dir}" $?
rm -f "..${separator}${testfile_md5}.md5"
) || exit $?
log "Checksum correct"
else
log "No checksums file: ${md5file_markup}, skipping md5 test"
die "Missing markup checksums file" $skip
fi
fi
rm -rf "${tmp_dir}/${markup_dir}"
rm -rf "${tmp_dir}${separator}${markup_dir}"

# dump rawml
rm -f "${tmp_dir}/${rawml_file}"
rm -f "${tmp_dir}${separator}${rawml_file}"
log "Running ${mobitool} -o \"${tmp_dir}\" -d ${options} \"${runfile}\""
${mobitool} -o "${tmp_dir}" -d ${options} "${runfile}" || die "Dumping rawml failed, mobitool error ($?)" $?
[[ -f "${tmp_dir}/${rawml_file}" ]] || die "Dumping rawml failed" 1
[[ -f "${tmp_dir}${separator}${rawml_file}" ]] || die "Dumping rawml failed" 1

# verify checksum
if [[ "${do_md5}" -eq "1" ]]; then
if [[ -f "${md5file_rawml}" ]]; then
md5_1=$(cat ${md5file_rawml})
md5_2=$(${md5prog} "${tmp_dir}/${rawml_file}")
md5_2=$(${md5prog} "${tmp_dir}${separator}${rawml_file}")
[[ "${md5_1%% *}" == "${md5_2%% *}" ]] || die "Wrong md5 checksum for ${rawml_file} (${md5_1%% *} != ${md5_2%% *})" 1
log "Checksum correct"
else
log "No checksums file: ${md5file_rawml}, skipping md5 test"
die "Missing rawml checksums file" $skip
fi
fi
rm -f "${tmp_dir}/${rawml_file}"
rm -f "${tmp_dir}${separator}${rawml_file}"

# test encryption / decryption
[[ "x@ENCRYPTION_OPT@" == "xyes" ]] || exit 0
Expand All @@ -140,7 +142,7 @@ fi
drm_file="${basefile%.*}-${suffix}"
drm_markup="${drm_file}_markup"

rm -f "${tmp_dir}/${drm_file}."*
rm -f "${tmp_dir}${separator}${drm_file}."*

log "Running ${mobidrm} -o \"${tmp_dir}\" ${options} \"${runfile}\""
output="$(${mobidrm} -o "${tmp_dir}" ${options} "${runfile}" 2>&1)"
Expand All @@ -158,17 +160,17 @@ fi

drm_files=()

[[ -f "${tmp_dir}/${drm_file}.mobi" ]] && drm_files+=("${drm_file}.mobi")
[[ -f "${tmp_dir}/${drm_file}.azw3" ]] && drm_files+=("${drm_file}.azw3")
[[ -f "${tmp_dir}${separator}${drm_file}.mobi" ]] && drm_files+=("${drm_file}.mobi")
[[ -f "${tmp_dir}${separator}${drm_file}.azw3" ]] && drm_files+=("${drm_file}.azw3")

[[ ${#drm_files[@]} -eq 0 ]] && die "DRM operation failed" 1

# verify
for drm_file in ${drm_files[@]}; do
log "Running ${mobitool} -o \"${tmp_dir}\" ${verify_options} \"${tmp_dir}/${drm_file}\""
${mobitool} -o "${tmp_dir}" ${verify_options} "${tmp_dir}/${drm_file}" || die "DRM operation produced broken file, mobitool error ($?)" $?
rm -f "${tmp_dir}/${drm_file}"
rm -rf "${tmp_dir}/${drm_markup}"
log "Running ${mobitool} -o \"${tmp_dir}\" ${verify_options} \"${tmp_dir}${separator}${drm_file}\""
${mobitool} -o "${tmp_dir}" ${verify_options} "${tmp_dir}${separator}${drm_file}" || die "DRM operation produced broken file, mobitool error ($?)" $?
rm -f "${tmp_dir}${separator}${drm_file}"
rm -rf "${tmp_dir}${separator}${drm_markup}"
done

exit 0
20 changes: 20 additions & 0 deletions tools/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const char separator = '/';
bool outdir_opt = false;
char outdir[FILENAME_MAX];

#define UNUSED(x) (void)(x)

/**
@brief Messages for libmobi return codes
For reference see enum MOBI_RET in mobi.h
Expand Down Expand Up @@ -205,6 +207,24 @@ bool dir_exists(const char *path) {
return true;
}

/**
@brief Make sure we use consistent separators on Windows builds
@param[in,out] path Path to be fixed
*/
void normalize_path(char *path) {
#ifdef _WIN32
if (path != NULL) {
for (size_t i = 0; i <= strlen(path); i++) {
if (path[i] == '/') {
path[i] = separator;
}
}
}
#else
UNUSED(path);
#endif
}


/**
@brief Print summary meta information
Expand Down
1 change: 1 addition & 0 deletions tools/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ int create_subdir(char *newdir, const char *parent_dir, const char *subdir_name)
int write_file(const unsigned char *buffer, const size_t len, const char *path);
int write_to_dir(const char *dir, const char *name, const unsigned char *buffer, const size_t len);
bool dir_exists(const char *path);
void normalize_path(char *path);
void print_summary(const MOBIData *m);
void print_exth(const MOBIData *m);
int set_decryption_key(MOBIData *m, const char *serial, const char *pid);
Expand Down
3 changes: 3 additions & 0 deletions tools/mobidrm.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ int main(int argc, char *argv[]) {
return ERROR;
}
strncpy(outdir, optarg, FILENAME_MAX - 1);
normalize_path(outdir);
if (!dir_exists(outdir)) {
printf("Output directory is not valid\n");
return ERROR;
Expand Down Expand Up @@ -479,6 +480,8 @@ int main(int argc, char *argv[]) {
char filename[FILENAME_MAX];
strncpy(filename, argv[optind], FILENAME_MAX - 1);
filename[FILENAME_MAX - 1] = '\0';
normalize_path(filename);

ret = loadfilename(filename);

return ret;
Expand Down
6 changes: 4 additions & 2 deletions tools/mobimeta.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,15 @@ int main(int argc, char *argv[]) {
char infile[FILENAME_MAX];
strncpy(infile, argv[optind], FILENAME_MAX - 1);
infile[FILENAME_MAX - 1] = '\0';

normalize_path(infile);

if (file_args >= 2) { optind++; }

char outfile[FILENAME_MAX];
strncpy(outfile, argv[optind], FILENAME_MAX - 1);
outfile[FILENAME_MAX - 1] = '\0';

normalize_path(outfile);

/* Initialize MOBIData structure */
MOBIData *m = mobi_init();
if (m == NULL) {
Expand Down
3 changes: 3 additions & 0 deletions tools/mobitool.c
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ int main(int argc, char *argv[]) {
return ERROR;
}
strncpy(outdir, optarg, FILENAME_MAX - 1);
normalize_path(outdir);
if (!dir_exists(outdir)) {
printf("Output directory is not valid\n");
return ERROR;
Expand Down Expand Up @@ -1043,6 +1044,8 @@ int main(int argc, char *argv[]) {
char filename[FILENAME_MAX];
strncpy(filename, argv[optind], FILENAME_MAX - 1);
filename[FILENAME_MAX - 1] = '\0';
normalize_path(filename);

ret = loadfilename(filename);
if (split_opt) {
printf("\nSplitting hybrid file...\n");
Expand Down

0 comments on commit 23e8974

Please sign in to comment.