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>
mksquashfs: get rid of waitforthread()

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

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
restore: Disable progress bar on restore

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
progressbar: don't display progress bar if disabled

When disabling the progress bar don't display it if it is already
disabled.  This allows code to simply call disable_progress_bar() rather
than having to first check if it is enabled.

Also if the progress_bar is enabled, print a newline afterwards, this is
to ensure the next message is on the next line.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: Shift SIGUSR2 to SIGUSR1

now SIGUSR1 isn't used

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: don't zero thread[x] on thread exit or check for it

This is now unnecessary using pthread_cancel()/pthread_join()

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: use pthread_exit(NULL) in writer thread

rather than return NULL

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: don't now need to check if progress bar is enabled

first before calling disable_progress_bar()

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
restore: get rid of res as it is not used

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
mksquashfs: make EXIT_MKSQUASHFS() etc restore via the restore thread

Since parallelisation only the main thread has been able to
call the EXIT_MKSQUASHFS macros, the sub-threads have not been
able to because the exit macros used to do a longjmp on restore
which is only valid on the main thread.

Unfortunately there are instances where the sub-threads do execute
code which call the exit macros, these are usually (but not exclusively)
in code which is mostly called by the main thread.  If these exit macros
ever triggered on the sub-threads then it wouldn't have worked.
Removing these exit macros was by and large difficult because they are
needed for the main thread.

But now there exists a restore thread, it is easy to change the exit
macros to signal the restore thread and then to exit the calling thread.
This works for the main thread and the sub-threads, meaning the exit
macros can now be used anywhere and by any thread.  This removes the
constant need when writing code to check what threads will be calling it,
and of course it fixes the existing bugs.

The other upshot of this change is all restoring is now done via the
restore thread.

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