[de0c3c]: dae / op_session.c Maximize Restore History

Download this file

op_session.c    154 lines (125 with data), 3.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* \file op_session.c
* Copyright 2002
* Read the file COPYING
*
* \author John Levon <moz@compsoc.man.ac.uk>
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <dirent.h>
#include <errno.h>
#include "opd_util.h"
#include "../util/op_popt.h"
char const * sessionname;
int showvers;
static struct poptOption options[] = {
{ "session", 's', POPT_ARG_STRING, &sessionname, 0, "save current session under this name", "session-name", },
{ "version", 'v', POPT_ARG_NONE, &showvers, 0, "show version", NULL, },
POPT_AUTOHELP
{ NULL, 0, 0, NULL, 0, NULL, NULL, },
};
/**
* opd_options - parse command line options
* @argc: argc
* @argv: argv array
*
* Parse all command line arguments.
*/
static void opd_options(int argc, char const *argv[])
{
poptContext optcon;
char const * file;
optcon = opd_poptGetContext(NULL, argc, argv, options, 0);
if (showvers) {
show_version(argv[0]);
}
file = poptGetArg(optcon);
if (file) {
sessionname = file;
}
if (!sessionname) {
fprintf(stderr, "op_session: no session name specified !\n");
poptPrintHelp(optcon, stderr, 0);
exit(EXIT_FAILURE);
}
poptFreeContext(optcon);
}
/**
* opd_move_files - move all the sample files
* \param sname name of session directory
*
*/
static void opd_move_files(char const * sname)
{
char * dir_name;
DIR * dir;
struct dirent * dirent;
int is_dir_empty = 1;
dir_name = xmalloc(strlen(OP_SAMPLES_DIR) + strlen(sname) + 1);
strcpy(dir_name, OP_SAMPLES_DIR);
strcat(dir_name, sname);
if (mkdir(dir_name, 0755)) {
if (errno == EEXIST) {
fprintf(stderr, "session \"%s\" already exists\n", dir_name);
} else {
fprintf(stderr, "unable to create directory \"%s\"\n", dir_name);
}
exit(EXIT_FAILURE);
}
if (!(dir = opendir(OP_SAMPLES_DIR))) {
fprintf(stderr, "unable to open directory " OP_SAMPLES_DIR "\n");
exit(EXIT_FAILURE);
}
while ((dirent = readdir(dir)) != 0) {
int ret;
ret = opd_move_regular_file(dir_name, OP_SAMPLES_DIR, dirent->d_name);
if (ret < 0) {
fprintf(stderr, "unable to backup %s/%s to directory %s\n",
OP_SAMPLES_DIR, dirent->d_name, dir_name);
exit(EXIT_FAILURE);
} else if (ret == 0) {
is_dir_empty = 0;
}
}
closedir(dir);
if (!is_dir_empty) {
opd_move_regular_file(dir_name, OP_BASE_DIR, OP_LOG_FILENAME);
} else {
rmdir(dir_name);
fprintf(stderr, "no samples files to save, session %s not created\n",
sname);
exit(EXIT_FAILURE);
}
free(dir_name);
}
/**
* opd_signal_daemon - signal daemon to re-open if it exists
*/
static void opd_signal_daemon(void)
{
pid_t pid = opd_read_lock_file(OP_LOCK_FILE);
if (pid) {
kill(pid, SIGHUP);
}
}
int main(int argc, char const *argv[])
{
pid_t pid;
opd_options(argc, argv);
/* not ideal, but OK for now. The sleep hopefully
* means the daemon starts reading before the signal
* is delivered, so it will finish reading, *then*
* handle the SIGHUP. Hack !
*/
pid = opd_read_lock_file(OP_LOCK_FILE);
if (pid) {
system("op_dump");
sleep(2);
}
opd_move_files(sessionname);
opd_signal_daemon();
return 0;
}