Permalink
Browse files

Added two notification options: notify-send and xmessage

  • Loading branch information...
1 parent 4b04fec commit 5d435c093d5c3647dbed044e6a989806f9c85032 @TacoSteemers committed Oct 12, 2013
Showing with 114 additions and 9 deletions.
  1. +10 −1 README.md
  2. +33 −2 code/backup.c
  3. +7 −0 code/global.h
  4. +58 −6 code/processArguments.c
  5. +6 −0 code/processArguments.h
View
@@ -22,6 +22,14 @@ The blacklist file might look like the following:
These identifiers can be obtained via the uslist application:
$ ./uslist
+
+If you wish to receive notifications on the desktop, you can use one of the following arguments:
+
+ --notification notify-send
+ --notification xmessage
+
+It is recommended that you use notify-send, if available on your system, because it is integrated with modern distributions.
+When using xmessage, the notification will be shown for three seconds. Any pending backups will start after their related xmessage notification is gone.
You may want to have usbud be started on system startup. In that case, I refer you to your platform's documentation to see find the recommended way of doing so.
@@ -53,8 +61,9 @@ A the moment of writing, it is not that easy under a GNU/Linux operating system.
## To be implemented:
- Configuration options for storage device size limits
+
Low priotity:
-- Remove dependence on rsync
+- Remove dependency on rsync
- Configuration options for target directory size limits
- Add the following optional behavior when hitting target directory size limits (if a target directory size limit is configured): remove the oldest backups until enough space is created. The idea behind this is that the oldest backups should already have been backed up by the system's actual backup system, outside of usbud's control.
- Determining the difference between a 'thumb drive' and external storage hard disks
View
@@ -93,10 +93,41 @@ void doBackup(char *source, char *target)
size_t errorBufLen = 32;
char outputBuffer[32];
char command[8192]; /* Max path length * 2 */
+ char notifyBefore[1024];
+ char notifyAfter[1024];
char* targetNoSpaces = replace(target, " ", "");
tidyStringUp(targetNoSpaces);
- sprintf(command, "%s %s %s %s", "rsync", "-Prtu", source,
- targetNoSpaces);
+ if(gNotificationSetting != NONOTIFICATIONS)
+ {
+ char notifyBaseBefore[512];
+ char notifyBaseAfter[512];
+ sprintf(notifyBaseBefore, "Starting backup of %s.", source);
+ sprintf(notifyBaseAfter, "Finished backup of %s.", source);
+
+ if(gNotificationSetting == NOTIFYSENDMODE)
+ {
+ sprintf(notifyBefore,
+ "notify-send -i stock_save-as \"USB Storage Backup\" \"%s\"",
+ notifyBaseBefore);
+ sprintf(notifyAfter,
+ "notify-send -i stock_save-as \"USB Storage Backup\" \"%s\"",
+ notifyBaseAfter);
+ }
+ if(gNotificationSetting == XMESSAGEMODE)
+ {
+ sprintf(notifyBefore, "xmessage \"USB Storage Backup %s\" -timeout 3",
+ notifyBaseBefore);
+ sprintf(notifyAfter, "xmessage \"USB Storage Backup %s\" -timeout 3",
+ notifyBaseAfter);
+ }
+ sprintf(command, "%s; %s %s %s %s; %s", notifyBefore,
+ "rsync", "-Prtu", source, targetNoSpaces, notifyAfter);
+ }
+ else
+ {
+ sprintf(command, "%s %s %s %s", "rsync", "-Prtu", source,
+ targetNoSpaces);
+ }
syslog(LOG_INFO, "Backup command: %s", command);
FILE *outputPipe = popen (command, "r");
if (!outputPipe)
View
@@ -14,6 +14,13 @@ const int gModeSetting; /* Defined in either daemon.c or uslist.c */
#define DAEMONMODE 1
#define LISTMODE 2
+/* Which type of notification should be used */
+/* Will be set while proccessing arguments */
+int gNotificationSetting;
+#define NONOTIFICATIONS 0 /* 0 is default for gNotificationSetting */
+#define NOTIFYSENDMODE 1
+#define XMESSAGEMODE 2
+
#define MAXLISTLENGTH 256
#define MAXIDLENGTH 256
#define MAXDEVICEPATHLENGTH 32
View
@@ -15,18 +15,29 @@
const char* signalTarget; /* declared in processArguments.h */
const char* gTargetDirectory; /* declared in processArguments.h */
-const char * signalWhitelist; /* declared in processArguments.h */
+const char* signalWhitelist; /* declared in processArguments.h */
char **gBlacklist; /* declared in global.h */
-const char * signalBlacklist; /* declared in processArguments.h */
+const char* signalBlacklist; /* declared in processArguments.h */
char **gWhitelist; /* declared in global.h */
+/* Which type of notification should be used */
+int gNotificationSetting; /* declared in global.h */
+const char* signalNotificationSetting; /* declared in processArguments.h */
+const char* signalNotifySend; /* declared in processArguments.h */
+const char* signalXMessage; /* declared in processArguments.h */
+
void initializeArgumentProcessing()
{
signalTarget = "--target";
signalBlacklist = "--blacklist";
signalWhitelist = "--whitelist";
gBlacklist = malloc(MAXLISTLENGTH);
gWhitelist = malloc(MAXLISTLENGTH);
+
+ gNotificationSetting = NONOTIFICATIONS;
+ signalNotificationSetting = "--notification";
+ signalNotifySend = "notify-send";
+ signalXMessage = "xmessage";
}
void processArguments(const char * const *arr, int arrc)
@@ -59,6 +70,10 @@ void processArguments(const char * const *arr, int arrc)
loadListFromFile(gWhitelist, arr, arrc, i);
i = i + 1; /* skip past argument parameter */
break;
+ case 3:
+ setNotificationMode(arr, arrc, i);
+ i = i + 1; /* skip past argument parameter */
+ break;
case -1:
syslog(LOG_ERR, "Exiting with failure. A null argument was passed to getArgumentIndex.");
exit(EXIT_FAILURE);
@@ -89,6 +104,10 @@ int getArgumentSwitchInt(const char* argument)
{ /* The whitelist filepath is being set */
return 2;
}
+ if(strcmp(argument, signalNotificationSetting) == 0)
+ { /* The notification setting is being set */
+ return 3;
+ }
return -2;
}
@@ -97,8 +116,7 @@ void setTarget(const char * const *arr, int arrc, int indexOfArgument)
{
if((indexOfArgument + 1) >= arrc)
{
- syslog(LOG_ERR, "Exiting with failure. Command-line argument \"%s\" can not be set: it is missing it's parameter.", signalTarget);
- exit(EXIT_FAILURE);
+ dieBecauseMissingArgument(arr[indexOfArgument]);
}
if(gTargetDirectory != NULL)
{
@@ -119,9 +137,9 @@ void loadListFromFile(char **out, const char * const *arr, int arrc, int indexOf
{
if((indexOfArgument + 1) >= arrc)
{
- syslog(LOG_ERR, "Exiting with failure. Command-line argument \"%s\" can not be set: it is missing it's parameter.", arr[indexOfArgument]);
- exit(EXIT_FAILURE);
+ dieBecauseMissingArgument(arr[indexOfArgument]);
}
+
FILE *file = openOrDie(arr[indexOfArgument + 1], "r"); /* util.c */
int count = 0;
char outputBuffer[MAXIDLENGTH];
@@ -141,3 +159,37 @@ void loadListFromFile(char **out, const char * const *arr, int arrc, int indexOf
}
syslog(LOG_INFO, "Using list located at \"%s\".\n", arr[indexOfArgument + 1]);
}
+
+void setNotificationMode(const char * const *arr, int arrc, int indexOfArgument)
+{
+ syslog(LOG_DEBUG, "setNotificationMode %s.\n", arr[indexOfArgument + 1]);
+ if((indexOfArgument + 1) >= arrc)
+ {
+ dieBecauseMissingArgument(arr[indexOfArgument]);
+ }
+ if(gNotificationSetting != NONOTIFICATIONS)
+ {
+ logArgumentReSetIgnored(signalNotificationSetting);
+ return;
+ }
+
+ const char* argument = arr[indexOfArgument+1];
+ if(strcmp(argument, signalNotifySend) == 0)
+ {
+ gNotificationSetting = NOTIFYSENDMODE;
+ syslog(LOG_INFO, "Notifying user with notify-send.");
+ return;
+ }
+ if(strcmp(argument, signalXMessage) == 0)
+ {
+ gNotificationSetting = XMESSAGEMODE;
+ syslog(LOG_INFO, "Notifying user with xmessage.");
+ return;
+ }
+}
+
+void dieBecauseMissingArgument(const char * argument)
+{
+ syslog(LOG_ERR, "Exiting with failure. Command-line argument \"%s\" can not be set: it is missing it's parameter.", argument);
+ exit(EXIT_FAILURE);
+}
View
@@ -12,9 +12,15 @@ void setTarget(const char * const *arr, int arrc, int indexOfArgument);
int getArgumentSwitchInt(const char* argument);
void logArgumentReSetIgnored(const char* argument);
void loadListFromFile(char **out, const char * const *arr, int arrc, int indexOfArgument);
+void setNotificationMode(const char * const *arr, int arrc, int indexOfArgument);
+void dieBecauseMissingArgument(const char * argument);
const char* signalTarget; /* defined in processArguments.c */
const char* signalBlacklist; /* defined in processArguments.c */
const char* signalWhitelist; /* defined in processArguments.c */
+/* For determining which way the user wishes to be notified */
+const char* signalNotificationSetting; /* defined in processArguments.c */
+const char* signalNotifySend; /* defined in processArguments.c */
+const char* signalXMessage; /* defined in processArguments.c */
#endif

0 comments on commit 5d435c0

Please sign in to comment.