error: rename prep_exit_mksquashfs to just prep_exit()

and remove the superfluous exit() in prep_exit(), exit()
should be via the macros that call prep_exit().

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
error: move extern definitions to error.h from progressbar.h

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
error: use progressbar_info for TRACE to synchronise it

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
error: Use error.h for Unsquashfs as well

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
read_xattrs: Use error.h for read_xattrs as well

and get rid of the separate error macro implementations
in read_xattrs.  This finally unifies all the error handling
into error.h

Read_xattrs.c has for a long time been an annoying special
case because it is used by both Mksquashfs and Unsquashfs,
it couldn't use the error.h infrastructure used by Mksquashfs
because that pulled in dependencies on Mksquashfs that don't
exist in Unsquashs, and it couldn't use the unsquashfs.h
infrastructure used by Unsquashfs because that pulled in
dependencies on Unsquashfs that don't exist in Mksquashfs,
and so it had to define its own macros.  But this has meant
it has not benefited from the improvements in error handling
added to the Mksquashfs/Unsquashfs error infrastructure.

The previous commits have finally broken the conflicting
dependencies introduced by error.h, unnecessary dependencies
have been removed and both Mksquashfs and Unsquashfs now
provide the necessary functions used by error.h...

So we can finally drop the read_xattrs.c only error macro
implemenation :-)

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
read_xattrs: save_xattr_block() no longer returns failure

    Use MEM_ERROR() for malloc failure.

    Now MEM_ERROR deals with out of memory, save_xattr_block()
    no longer needs to return failure.  Remove failure return
    and checks for failure.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
read_xattrs: Use MEM_ERROR() in read_xattr_entry()

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
read_xattrs: Use MEM_ERROR() in read_xattrs_from_disk()

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
read_xattrs: Use MEM_ERROR() in get_xattrs()

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: get rid of update_progress_bar()

Don't display the progress bar every time a file is written
as the background 1/4 second display is sufficient.

Removing this eliminates an unanticipated main thread dependency
on the progress bar thread.  By grabbing the progress_mutex mutex
before doing the pthread_cond_signal() to wake up the progress bar
thread it means the main thread waits on the progress_mutex mutex
if the progress bar thread already has it grabbed.  Normally
when the progress bar wakes up it grabs the progress_mutex mutex
prints the progress bar and quickly releases the mutex, and so
the main thread will never wait on the mutex.  However, if
the progress bar printing is slow, the main thread can wake up
the progress bar thread when a file is written, and it can
still be printing the progress bar when the main thread writes
the next file, causing the main thread to wait.

In effect this throttles file writing to the speed of the progress
bar printing, normally this has no effect because progress bar
printing is faster, however, if the output terminal is running
over a slow link this can become a bottleneck.  The author
has noticed this running Mksquashfs on a VT102 connected via a
9600 baud serial line.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
progress_bar: get rid of progress_wait and pthread_cond_timedwait()

Now that we don't wake up the progress bar thread get rid
of the progress_wait variable and the pthread_cond_timewait()
call and instead replace it with a call to nanosleep().

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: tidy up signal handling and document the signal usage

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: print file being squashed when ^\ (SIGQUIT) typed

If ^\ is typed on the keyboard whilst Mksquashfs is squashing files
(progress bar being printed), then display the filename of the
file being squashed.

The progress bar is useful to display how far blockwise/percentage-wise
Mksquashfs has got, but, it doesn't display filenames, and it is
often nice to get a snapshot filewise where Mksquashfs has got to.
Obviously, you can use -info for this, but this is a "heavy-weight"
option which displays every filename, it not only leads to
screenfulls of text, but it also can slow Mksquashfs down as it
is bottlenecked by all the filename printing.

Adding the ^\ option offers a best of both worlds, Mksquashfs
displays the progress bar which gives nice feedback on the
progress of Mksquashfs but in a way which does not generate
lots of text and doesn't slow Mksquashfs down, and typing ^\
gives you the filename of file being squashed but only when
desired, which doesn't generate screenfulls of text or slow
Mksquashfs down.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: move filesystem table writing into own function

and out of the main() function.  This will allow a dedicated
filesystem restore thread to call it, if the appended to filesystem
needs restoring.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: get rid of pthread_setcancel{state!type}

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: change sigprocmask to pthread_sigmask

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: move installation of SIGUSR1 handler to each thread

rather than installing it before thread creation and having
the created sub-threads inherit it.  This is because the main
thread, the progress bar and the info thread should not have
a SIGUSR1 handler.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: Add separate restore thread rather than setjmp/longjmp

When ^C is hit twice (or on error) Mksquashfs if it is appending to a
filesystem needs to exit restoring the filesystem to its original
state.

Originally when Mksquashfs was a single threaded application this
meant the single thread had to unwind back to a known state and
then restore.  This was achieved by the use of setjmp/longjmp.
When multi-threading was added to Mksquashfs this approach
was retained but with the addition of code to disable the sub-threads
before restoring took place.  This was an acceptable solution but
slightly inelegant because multi-threading meant unwinding of a single
thread was no longer necessary.

This commit removes the use of setjmp/longjmp and instead delegates
restoring to a separate thread which when ^C is hit twice or
unrecoverable error is encountered by a sub-thread (sending SIGUSR2),
kills the main thread and does the restoring itself.

Note, when the main thread receives an unrecoverable error, the new code
still executes the recovery on the main thread but now without any
unwinding.  In this situation unwinding was never necessary, but it
was easier with the original code to use share the code and unwind.
Now unwinding is not needed anywhere, it is easier to share the code
and not unwind.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: rename sighandler2 to sighandler

Sighandler no longer exisits so rename sighandler2.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: Use pthread_cancel() to terminate sub-threads

Use pthread_cancel() to terminate sub-threads when restoring
filesystem rather than using pthread_kill().

Using pthread_cancel() is safer and better because it only
cancels threads at specific cancellation points, rather than
pthread_kill() which asynchronously interrupts threads.  However,
pthread_cancel() requires all code which uses mutexes to register
and unregister cleanup code to ensure on pthread cancellation
mutexes are properly released, otherwise code will deadlock
(e.g. pthread_cond_wait() is a cancellation point, but it exits with the
mutex held, and so the thread dies holding the lock).

So also add all the necessary pthread cleaup handlers as well.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>