From dcc4410cdc8ee138a9451a5b25e3904fed124f4b Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Sun, 13 Feb 2022 22:53:37 +0000 Subject: cleaner fix for Mac reload problem (aka fseek error), from Paul V git-svn-id: svn://tug.org/texlive/trunk@62013 c570f23f-e606-0410-a88d-b1316a301751 --- Build/source/texk/xdvik/ChangeLog | 10 ++++++++++ Build/source/texk/xdvik/dvi-init.c | 23 +++++++++++++++++++---- Build/source/texk/xdvik/util.c | 16 +++++++++++++++- Build/source/texk/xdvik/util.h | 3 ++- 4 files changed, 46 insertions(+), 6 deletions(-) (limited to 'Build/source') diff --git a/Build/source/texk/xdvik/ChangeLog b/Build/source/texk/xdvik/ChangeLog index 76c0cf33b17..6e0f29740a7 100644 --- a/Build/source/texk/xdvik/ChangeLog +++ b/Build/source/texk/xdvik/ChangeLog @@ -1,3 +1,13 @@ +2022-02-13 Paul Vojta + + * dvi-init.c (make_backup_fp): call new try_dup, and existing + try_fdopen, before fseek. + * util.c (try_dup): define new fn. + * util.h (try_dup): declare it. + This resolves xdvi crashing under MacOS after hitting `R' in some + situations (the preceding change did as well, in a clunkier way). + https://tug.org/pipermail/tlbuild/2022q1/005140.html ff. + 2022-02-01 John Hawkinson * dvi-init.c (make_backup_fp): check returns from fseeks, diff --git a/Build/source/texk/xdvik/dvi-init.c b/Build/source/texk/xdvik/dvi-init.c index 829bd58080b..52d5a3ecfa9 100644 --- a/Build/source/texk/xdvik/dvi-init.c +++ b/Build/source/texk/xdvik/dvi-init.c @@ -1,6 +1,6 @@ /*========================================================================*\ -Copyright (c) 1990-2013 Paul Vojta +Copyright (c) 1990-2022 Paul Vojta Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to @@ -1185,18 +1185,33 @@ make_backup_fp(FILE *source_fp, FILE *target_fp) ASSERT(target_fp != NULL, ""); ASSERT(source_fp != NULL, ""); - if (fseek(target_fp, 0L, SEEK_SET)) { + tmp_fd = try_dup(fileno(target_fp)); + if (tmp_fd == -1) { + XDVI_ERROR((stderr, "Couldn't duplicate descriptor of file %s: %s - disabling `useTempFp'; target_fp: %p.", + m_tmp_dvi_name, strerror(errno), target_fp)); + goto fclose_and_fail; + } + + fclose(target_fp); + + if (lseek(tmp_fd, 0L, SEEK_SET) == -1) { XDVI_ERROR((stderr, "Couldn't seek to start of file %s: %s - disabling `useTempFp'; target_fp: %p.", m_tmp_dvi_name, strerror(errno), target_fp)); - goto fclose_and_fail; + goto fail; } #if HAVE_FTRUNCATE if (ftruncate(tmp_fd, 0) < 0) { XDVI_ERROR((stderr, "Couldn't truncate file %s: %s - disabling `useTempFp'; target_fp: %p.", m_tmp_dvi_name, strerror(errno), target_fp)); - goto fclose_and_fail; + goto fail; } #endif + + if ((target_fp = try_fdopen(tmp_fd, "wb+")) == NULL) { + XDVI_ERROR((stderr, "error opening temporary file (%s) - disabling `useTempFp'.", strerror(errno))); + goto fail; + } + if (fseek(source_fp, 0L, SEEK_SET)) { perror("fseek of source_fp"); goto fclose_and_fail; diff --git a/Build/source/texk/xdvik/util.c b/Build/source/texk/xdvik/util.c index 958a99c4788..7a7bd687786 100644 --- a/Build/source/texk/xdvik/util.c +++ b/Build/source/texk/xdvik/util.c @@ -1,6 +1,6 @@ /*========================================================================*\ -Copyright (c) 1990-2015 Paul Vojta and others +Copyright (c) 1990-2022 Paul Vojta and others Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to @@ -281,6 +281,20 @@ try_open_mode(const char *fname, int flags, mode_t mode) return fd; } +/* + * Like try_fopen(), for dup(). + */ +int +try_dup(int oldfd) +{ + int fd = dup(oldfd); + if (fd < 0 && (errno == EMFILE || errno == ENFILE)) { + close_a_file(); + fd = dup(oldfd); + } + return fd; +} + /* * invoked on SIGSEGV: try to stop gs before aborting, to prevent gs diff --git a/Build/source/texk/xdvik/util.h b/Build/source/texk/xdvik/util.h index 09a14fde460..cbebe8c7fcb 100644 --- a/Build/source/texk/xdvik/util.h +++ b/Build/source/texk/xdvik/util.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2015 the xdvik development team + * Copyright (c) 2002-2022 the xdvik development team * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to @@ -51,6 +51,7 @@ FILE *try_fopen(const char *fname, const char *mode); FILE *try_fdopen(int fd, const char *mode); int try_open(const char *fname, int flags); int try_open_mode(const char *fname, int flags, mode_t mode); +int try_dup(int oldfd); extern int xdvi_temp_fd(char **tempfilename); extern void xdvi_assert(const char *version, -- cgit v1.2.3