Reworked the file locking option as a command instead of as a

compile-time option.  The behavior can now be controlled from
within the program with "locking disable" or "locking enable".
This commit is contained in:
Tim Edwards 2022-01-01 14:28:59 -05:00
parent 6a78f4967e
commit e4d1c29112
7 changed files with 100 additions and 47 deletions

View File

@ -54,6 +54,9 @@ void CmdPaintEraseButton();
extern Label *DefaultLabel;
/* See the "locking" command */
extern bool FileLocking;
/*
* ----------------------------------------------------------------------------
*
@ -527,7 +530,7 @@ CmdLoad(w, cmd)
/*
* Function callback which continues the search through all cells for
* expansion/unexpansion.
* expansion/unexpansion, used by CmdLoad() above.
*/
int
@ -538,6 +541,70 @@ keepGoing(use, clientdata)
return 0; /* keep the search going */
}
/*
* ----------------------------------------------------------------------------
*
* CmdLocking --
*
* Implement the "locking" command.
*
* Usage:
* locking [enable|disable]
*
* This command controls the behavior of file locking. If enabled, then
* every layout read from a .mag file will generate a file descriptor in
* the operating system, and the file will be held open for the duration
* of use. This is a fairly simple way of preventing two different
* processes of magic from attempting to write to the same .mag file.
* Generally speaking, the file locking should remain on at all times,
* and read/write behavior of any specific cell can be controlled with
* the "cellname writeable" command option. However, in some cases,
* layouts with many components may exceed the operating system's
* allotted number of open file descriptors, in which case the only real
* option is to disable file locking altogether using "locking disable".
*
* Results:
* None.
*
* Side effects:
* Sets global boolean variable FileLocking.
*
* ----------------------------------------------------------------------------
*/
void
CmdLocking(w, cmd)
MagWindow *w;
TxCommand *cmd;
{
int option;
static char *cmdLockingYesNo[] = { "disable", "no", "false", "off", "0",
"enable", "yes", "true", "on", "1", 0 };
if (cmd->tx_argc <= 1)
{
#ifdef MAGIC_WRAPPER
/* Interpreter return value is the state of locking */
Tcl_SetResult(magicinterp, (FileLocking) ? "enabled" : "disabled",
TCL_VOLATILE);
#else
/* Print the status of file locking to the console */
TxPrintf("%s\n", (FileLocking) ? "enabled" : "disabled");
#endif
}
else
{
option = Lookup(cmd->tx_argv[1], cmdLockingYesNo);
if (option < 0)
{
TxError("Unknown locking option \"%s\"\n", cmd->tx_argv[1]);
return;
}
FileLocking = (option <= 4) ? FALSE : TRUE;
}
}
/*
* ----------------------------------------------------------------------------
*

View File

@ -46,7 +46,7 @@ extern void CmdDelete(), CmdDown(), CmdDrc(), CmdDrop(), CmdDump();
extern void CmdEdit(), CmdElement(), CmdErase(), CmdExpand(), CmdExtract();
extern void CmdFeedback(), CmdFill(), CmdFindBox(), CmdFindLabel(), CmdFlush();
extern void CmdGetcell(), CmdGrid(), CmdIdentify();
extern void CmdLabel(), CmdLoad();
extern void CmdLabel(), CmdLoad(), CmdLocking();
extern void CmdMove(), CmdNetlist(), CmdOrient(), CmdPaint(), CmdPath();
extern void CmdPlow(), CmdPolygon(), CmdPort(), CmdProperty();
extern void CmdRandom(), CmdSave(), CmdScaleGrid(), CmdSee();
@ -369,6 +369,9 @@ DBWInitCommands()
WindAddCommand(DBWclientID,
"load [cellname] load a cell into a window",
CmdLoad, FALSE);
WindAddCommand(DBWclientID,
"locking [enable|disable] enable or disable file locking",
CmdLocking, FALSE);
WindAddCommand(DBWclientID,
"move [dir [amount]] OR\n"
"move to x y move box and selection, either by amount\n\

13
scripts/configure vendored
View File

@ -6598,7 +6598,7 @@ if test $usingTcl ; then
# Find the version of "wish" that corresponds to TCL_EXEC_PREFIX
# We really ought to run "ldd" to confirm that the linked libraries match.
if text "x${magic_with_wish_binary}" = "x" ; then
if test "x${magic_with_wish_binary}" = "x" ; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for wish executable" >&5
$as_echo_n "checking for wish executable... " >&6; }
for dir in \
@ -6844,15 +6844,8 @@ else
fi
if test "x$enable_locking" = "xyes" ; then
case $target in
*cygwin*)
;;
*)
$as_echo "#define FILE_LOCKS 1" >>confdefs.h
;;
esac
if test "x$enable_locking" = "xno" ; then
echo "File locking no longer set during configuration; ignoring option."
fi
# Check whether --enable-calma was given.

View File

@ -705,7 +705,7 @@ if test $usingTcl ; then
# Find the version of "wish" that corresponds to TCL_EXEC_PREFIX
# We really ought to run "ldd" to confirm that the linked libraries match.
if text "x${magic_with_wish_binary}" = "x" ; then
if test "x${magic_with_wish_binary}" = "x" ; then
AC_MSG_CHECKING([for wish executable])
for dir in \
${TK_EXEC_PREFIX}/bin \
@ -935,14 +935,8 @@ AC_ARG_ENABLE(locking,
[],
[enable_locking=yes])
if test "x$enable_locking" = "xyes" ; then
case $target in
*cygwin*)
;;
*)
AC_DEFINE(FILE_LOCKS)
;;
esac
if test "x$enable_locking" = "xno" ; then
echo "File locking no longer set during configuration; ignoring option."
fi
AC_ARG_ENABLE(calma,

View File

@ -11,8 +11,6 @@
*-------------------------------------------------------------------------
*/
#ifdef FILE_LOCKS
#include <unistd.h>
#include <fcntl.h>
@ -166,5 +164,3 @@ FILE *flock_open(filename, mode, is_locked)
done:
return f;
}
#endif /* FILE_LOCKS */

View File

@ -46,6 +46,8 @@ static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-8.0/
static HashTable expansionTable;
static bool noTable = TRUE;
bool FileLocking = TRUE;
/* Limit on how long a single file name may be: */
#define MAXSIZE MAXPATHLEN
@ -454,11 +456,10 @@ PaLockOpen(file, mode, ext, path, library, pRealName, is_locked)
p2 = file;
if (PaExpand(&p2, &p1, MAXSIZE) < 0) return NULL;
#ifndef FILE_LOCKS
return fopen(realName, mode);
#else
return flock_open(realName, mode, is_locked);
#endif
if (FileLocking)
return flock_open(realName, mode, is_locked);
else
return fopen(realName, mode);
}
/* If we were already given a full rooted file name,
@ -473,11 +474,11 @@ PaLockOpen(file, mode, ext, path, library, pRealName, is_locked)
{
(void) strncpy(realName, file, MAXSIZE-1);
realName[MAXSIZE-1] = '\0';
#ifndef FILE_LOCKS
return fopen(realName, mode);
#else
return flock_open(realName, mode, is_locked);
#endif
if (FileLocking)
return flock_open(realName, mode, is_locked);
else
return fopen(realName, mode);
}
/* Now try going through the path, one entry at a time. */
@ -485,11 +486,12 @@ PaLockOpen(file, mode, ext, path, library, pRealName, is_locked)
while (nextName(&path, file, realName, MAXSIZE) != NULL)
{
if (*realName == 0) continue;
#ifndef FILE_LOCKS
f = fopen(realName, mode);
#else
f = flock_open(realName, mode, is_locked);
#endif
if (FileLocking)
f = flock_open(realName, mode, is_locked);
else
f = fopen(realName, mode);
if (f != NULL) return f;
// If any error other than "file not found" occurred,
@ -504,11 +506,11 @@ PaLockOpen(file, mode, ext, path, library, pRealName, is_locked)
if (library == NULL) return NULL;
while (nextName(&library, file, realName, MAXSIZE) != NULL)
{
#ifndef FILE_LOCKS
f = fopen(realName, mode);
#else
f = flock_open(realName, mode, is_locked);
#endif
if (FileLocking)
f = flock_open(realName, mode, is_locked);
else
f = fopen(realName, mode);
if (f != NULL) return f;
// If any error other than "file not found" occurred,

View File

@ -54,9 +54,7 @@ extern bool StrIsNumeric(char *);
extern int SetNoisyBool(bool *, char *, FILE *);
#ifdef FILE_LOCKS
extern FILE *flock_open();
#endif
/* The following macro takes an integer and returns another integer that
* is the same as the first except that all the '1' bits are turned off,