Permalink
Browse files

Refactored backup.c. Replaced some \ts with 4 spaces.

  • Loading branch information...
1 parent 7b971ec commit 4d0702b4a984d5aaa60d4ca65d7536a212622336 @TacoSteemers committed Apr 13, 2013
Showing with 241 additions and 223 deletions.
  1. +75 −59 code/backup.c
  2. +3 −2 code/backup.h
  3. +163 −162 code/detection.c
View
134 code/backup.c
@@ -17,91 +17,107 @@
void processDevice(char* mountPoint, char* deviceId)
{
- int index;
- if(checkIfDeviceIsKnown(deviceId)==1)
- return; /* Already known, and thus backed up */
- index = registerDevice(deviceId);
- if(index == -1)
- return; /* Failed to get the device registered */
- backDeviceUp(mountPoint, deviceId);
+ int index;
+ if(checkIfDeviceIsKnown(deviceId)==1)
+ return; /* Already known, and thus backed up */
+ index = registerDevice(deviceId);
+ if(index == -1)
+ return; /* Failed to get the device registered */
+ if(!needsBackup(deviceId))
+ return;
+
+ /* Device needs backing up! */
+ char* target =
+ malloc(strlen(gTargetDirectory) + strlen(deviceId) + 2);
+ createTargetDirectoryString(target, deviceId);
+ doBackup(mountPoint, target);
+ free(target);
}
-void backDeviceUp(char* mountPoint, char* deviceId)
+int needsBackup(char* deviceId)
{
- int blacklisted = 0;
- int whitelisted = 0;
- int needsWhitelisting = 0;
+ int blacklisted = 0;
+ int whitelisted = 0;
+ int needsWhitelisting = 0;
if(gBlacklist[0] != NULL)
- {
- blacklisted = contains((const char * const *)gBlacklist, deviceId);
- }
+ {
+ blacklisted = contains((const char * const *)gBlacklist,
+ deviceId);
+ }
if(gWhitelist[0] != NULL)
- {
- needsWhitelisting = 1;
- whitelisted = contains((const char * const *)gWhitelist, deviceId);
- }
- if((blacklisted == 1) && (whitelisted == 1))
- {
- syslog(LOG_INFO, "Device \"%s\" has been blacklisted as well as whitelisted. It will not be backed up.", deviceId);
- return;
- }
- if(blacklisted == 1)
- {
- syslog(LOG_INFO, "Device \"%s\" has been blacklisted. It will not be backed up.", deviceId);
- return;
- }
- if(needsWhitelisting && !whitelisted)
- {
- syslog(LOG_INFO, "Device \"%s\" has not been whitelisted. It will not be backed up.", deviceId);
- return;
- }
-
- /* Create target directory string */
- char* target = malloc(strlen(gTargetDirectory) + strlen(deviceId) + 2);
- if(!target)
- {
- syslog(LOG_ERR, "Exiting with failure: malloc failed during backDeviceUp");
- exit(EXIT_FAILURE);
- }
- memcpy(target, gTargetDirectory, strlen(gTargetDirectory)+1);
- target[strlen(gTargetDirectory)+1] = '\0';
- strcat(target, "/");
- strcat(target, deviceId);
+ {
+ needsWhitelisting = 1;
+ whitelisted = contains((const char * const *)gWhitelist,
+ deviceId);
+ }
+ if((blacklisted == 1) && (whitelisted == 1))
+ {
+ syslog(LOG_INFO, "Device \"%s\" has been blacklisted as %s",
+ deviceId, "well as whitelisted. It will not be backed up.");
+ return 0;
+ }
+ if(blacklisted == 1)
+ {
+ syslog(LOG_INFO, "Device \"%s\" has been blacklisted. %s",
+ deviceId,
+ "It will not be backed up.");
+ return 0;
+ }
+ if(needsWhitelisting && !whitelisted)
+ {
+ syslog(LOG_INFO, "Device \"%s\" has not been whitelisted. %s",
+ deviceId,
+ "It will not be backed up.");
+ return 0;
+ }
+ return 1;
+}
- doBackup(mountPoint, target); /* Defined in backup.c */
- free(target);
+void createTargetDirectoryString(char* target, char* deviceId)
+{
+ if(!target)
+ {
+ syslog(LOG_ERR,
+ "Exiting with failure: malloc failed during backDeviceUp");
+ exit(EXIT_FAILURE);
+ }
+ memcpy(target, gTargetDirectory, strlen(gTargetDirectory)+1);
+ target[strlen(gTargetDirectory)+1] = '\0';
+ strcat(target, "/");
+ strcat(target, deviceId);
}
-void doBackup(char *source, char *target){
+void doBackup(char *source, char *target)
+{
char errorBuf[32];
size_t errorBufLen = 32;
- int error;
char outputBuffer[32];
char command[8192]; /* Max path length * 2 */
char* targetNoSpaces = replace(target, " ", "");
tidyStringUp(targetNoSpaces);
- sprintf(command, "%s %s %s %s", "rsync", "-Prtu", source, targetNoSpaces);
+ sprintf(command, "%s %s %s %s", "rsync", "-Prtu", source,
+ targetNoSpaces);
syslog(LOG_INFO, "Backup command: %s", command);
FILE *outputPipe = popen (command, "r");
if (!outputPipe)
{
- error = errno;
- strerror_r(error, errorBuf, errorBufLen);
+ strerror_r(errno, errorBuf, errorBufLen);
syslog(LOG_ERR, "Execution of backup failed: %s", errorBuf);
- syslog(LOG_ERR, "Exiting with failure: could not run command: \"%s\"", command);
+ syslog(LOG_ERR,
+ "Exiting with failure: could not run command: \"%s\"",
+ command);
exit(EXIT_FAILURE);
}
while(!feof(outputPipe)) {
fgets(outputBuffer, sizeof(outputBuffer), outputPipe);
- /* Logging of backup can be implemented here */
}
if (pclose(outputPipe) == -1)
{
- error = errno;
- strerror_r(error, errorBuf, errorBufLen);
- syslog(LOG_ERR, "Error while closing the pipe to the backup failed: %s", errorBuf);
+ strerror_r(errno, errorBuf, errorBufLen);
+ syslog(LOG_ERR,
+ "Error. Closing the pipe to the backup process failed: %s",
+ errorBuf);
}
- if(targetNoSpaces != NULL)
- free(targetNoSpaces);
syslog(LOG_INFO, "Execution of backup done.");
+ free(targetNoSpaces);
}
View
5 code/backup.h
@@ -6,8 +6,9 @@
#ifndef BACKUP_H
#define BACKUP_H
-void doBackup (char *source, char *target);
void processDevice(char *mountPoint, char *deviceId);
-void backDeviceUp (char *mountPoint, char *deviceId);
+int needsBackup(char* deviceId);
+void createTargetDirectoryString(char* output, char* deviceId);
+void doBackup(char *source, char *target);
#endif
View
325 code/detection.c
@@ -4,7 +4,7 @@
provided. It can also be found on: http://www.gnu.org/licenses/gpl-3.0.txt */
/* Many thanks to Orwellophile and his answer on
- http://stackoverflow.com/questions/2432759/ */
+ http://stackoverflow.com/questions/2432759/ */
#include <dirent.h>
#include <unistd.h> /* read, readlink */
@@ -24,76 +24,77 @@ void doCheck(void)
DIR *directory;
struct dirent *entry;
char* dirToCheck = "/sys/block/";
- char devicePath[MAXDEVICEPATHLENGTH];
+ char devicePath[MAXDEVICEPATHLENGTH];
- initializeRun();
+ initializeRun();
- directory = openDirOrHang(dirToCheck);
+ directory = openDirOrHang(dirToCheck);
do
- { /* Proccess each item in /sys/block/ */
+ { /* Proccess each item in /sys/block/ */
if ((entry = readdir(directory)) == NULL)
- continue;
- if(entry->d_name[0]!='s' || entry->d_name[1]!='d')
- continue;
- /* sda, sdb, sdc, ... */
+ continue;
+ if(entry->d_name[0]!='s' || entry->d_name[1]!='d')
+ continue;
+ /* sda, sdb, sdc, ... */
- /* We will now double check via /proc/mounts that the item exists */
- sprintf(devicePath, "/dev/%s", entry->d_name);
- if(checkIfItemMounted(devicePath)==0)
- continue; /* Not mounted, not of interest */
+ /* We will now check via /proc/mounts
+ which partitions have been mounted */
+ sprintf(devicePath, "/dev/%s", entry->d_name);
+ if(checkIfItemMounted(devicePath)==0)
+ continue; /* Not mounted, not of interest */
processItem(devicePath, entry->d_name);
} while (entry != NULL);
- free(directory);
- free(entry);
+ free(directory);
+ free(entry);
- finalizeRun();
+ finalizeRun();
}
void processItem(char *devicePath, char* entryName)
{
ssize_t len; /* Length of the path to the symbolic link */
char buf[MAXIDLENGTH];
- /* strLocation will contain the symbolic link in /sys/block/,
- that leads to the device that we want to investigate */
+ /* strLocation will contain the symbolic link in /sys/block/,
+ that leads to the device that we want to investigate */
char strLocation[MAXIDLENGTH];
- /* strDevice will contain the path to the actual device,
- (several versions of it, with varying detail)
- rather than the symbolic link */
+ /* strDevice will contain the path to the actual device,
+ (several versions of it, with varying detail)
+ rather than the symbolic link */
char strDevice[MAXIDLENGTH];
- /* strId will contain the identifier for the device, that will be used by
- this code base */
- char strId[MAXIDLENGTH];
- char mountPoint[MAXIDLENGTH];
+ /* strId will contain the identifier for the device, that will be used by
+ this code base */
+ char strId[MAXIDLENGTH];
+ char mountPoint[MAXIDLENGTH];
- /* Construct the path to /sys/block/... */
- snprintf(strLocation, sizeof strLocation, "/sys/block/%s", entryName);
+ /* Construct the path to /sys/block/... */
+ snprintf(strLocation, sizeof strLocation, "/sys/block/%s", entryName);
- /* Read symbolink link 'location' into buf,
- returns the number of bytes it has placed in buf */
+ /* Read symbolink link 'location' into buf,
+ returns the number of bytes it has placed in buf */
len = readlink(strLocation, buf, MAXIDLENGTH);
buf[len] = 0;
- /* strDevice will contain the path to the actual device,
- rather than the symbolic link */
+ /* strDevice will contain the path to the actual device,
+ rather than the symbolic link */
sprintf(strDevice, "%s/%s", "/sys/block/", buf);
- /* Now we try to gather device information
- such as manufacturer, product and serial number.
- strDevice will be changed in the process.
- We use a variable to make note of the length,
- to be sure that we can properly zero-terminate. */
- int intIdLen = 0;
- getDeviceInfo(strId, &intIdLen, strDevice);
- if(intIdLen==0)
- return; /* Some kind of bogus device */
+ /* Now we try to gather device information
+ such as manufacturer, product and serial number.
+ strDevice will be changed in the process.
+ We use a variable to make note of the length,
+ to be sure that we can properly zero-terminate. */
+ int intIdLen = 0;
+ getDeviceInfo(strId, &intIdLen, strDevice);
+ if(intIdLen==0)
+ return; /* Some kind of bogus device */
- getMountPoint(mountPoint, devicePath);
- if(strlen(mountPoint) == 0)
- return ; /* Apparently the device has been dismounted by now */
-
- if(gModeSetting == LISTMODE)
- { /* In list mode, we are only interested in listing the devices */
+ getMountPoint(mountPoint, devicePath);
+ if(strlen(mountPoint) == 0)
+ return ; /* Apparently the device has been dismounted by now */
+
+ if(gModeSetting == LISTMODE)
+ { /* In list mode, we are only interested in listing the devices */
if(gPrintDetailsInListMode == 1)
{
printf("%s, %s, %s\n", devicePath, mountPoint, strId);
@@ -102,164 +103,164 @@ void processItem(char *devicePath, char* entryName)
{
printf("%s\n", strId);
}
- return;
- }
-
- /* It appears we have a valid device */
- processDevice(mountPoint, strId); /* Defined in backup.c */
+ return;
+ }
+
+ /* It appears we have a valid device */
+ processDevice(mountPoint, strId); /* Defined in backup.c */
}
void getDeviceInfo(char* out, int *pOutLen, const char device[])
{
int i;
- char *p;
- /* strModel will contain the identifier for the model of this device,
- as reported by the device.
+ char *p;
+ /* strModel will contain the identifier for the model of this device,
+ as reported by the device.
This is of interest in the case of multi-card readers. */
- char strModel[MAXIDLENGTH];
- int intModelLen = 0;
+ char strModel[MAXIDLENGTH];
+ int intModelLen = 0;
- /* strrchr: Locate last occurrence of character in string
- Returns a pointer to the last occurrence of
- character in the C string str.
- We will be using that function to deconstruct the device
- variable into the parts that interest us.
- We do this to get the proper device location (which
- USB connector our device is plugged in to), and the
- subsystem number. That number we use to get the device
- model.
- In the case of a simple thumb drive, the subsystem number
- will be 0.
- A card reader with multiple slots will have multiple
- subsystems, each with it's own number.
- If we are looking at a SATA device instead of a USB device,
- then this is where things will start to look different
- than expected. */
+ /* strrchr: Locate last occurrence of character in string
+ Returns a pointer to the last occurrence of
+ character in the C string str.
+ We will be using that function to deconstruct the device
+ variable into the parts that interest us.
+ We do this to get the proper device location (which
+ USB connector our device is plugged in to), and the
+ subsystem number. That number we use to get the device
+ model.
+ In the case of a simple thumb drive, the subsystem number
+ will be 0.
+ A card reader with multiple slots will have multiple
+ subsystems, each with it's own number.
+ If we are looking at a SATA device instead of a USB device,
+ then this is where things will start to look different
+ than expected. */
- /* Retrieving subsystem (ends in subsystem id) */
+ /* Retrieving subsystem (ends in subsystem id) */
for (i=0; i<2; i++) {
p = strrchr(device, '/');
*p = 0;
}
- /* Retrieving model name from subsystem */
- addDeviceInfo(strModel, &intModelLen, device, "/model");
+ /* Retrieving model name from subsystem */
+ addDeviceInfo(strModel, &intModelLen, device, "/model");
- /* Retrieving the general system information */
+ /* Retrieving the general system information */
for (i=0; i<4; i++) {
p = strrchr(device, '/');
*p = 0;
}
- addDeviceInfo(out, pOutLen, device, "/manufacturer");
- if (pOutLen > 0)
- out[*pOutLen-1] = ' ';
- addDeviceInfo(out, pOutLen, device, "/product");
- if (pOutLen > 0)
- out[*pOutLen-1] = ' ';
- addDeviceInfo(out, pOutLen, device, "/serial");
- if (pOutLen <= 0)
- return; /* Apparently this is not a storage device
- that is compatible with this daemon.
- An example of that is a SATA device. */
+ addDeviceInfo(out, pOutLen, device, "/manufacturer");
+ if (pOutLen > 0)
+ out[*pOutLen-1] = ' ';
+ addDeviceInfo(out, pOutLen, device, "/product");
+ if (pOutLen > 0)
+ out[*pOutLen-1] = ' ';
+ addDeviceInfo(out, pOutLen, device, "/serial");
+ if (pOutLen <= 0)
+ return; /* Apparently this is not a storage device
+ that is compatible with this daemon.
+ An example of that is a SATA device. */
- tidyStringUp(out);
- tidyStringUp(strModel);
+ tidyStringUp(out);
+ tidyStringUp(strModel);
- /* Process device model name, if any */
- if(strlen(strModel) != 0)
- {
- if((strlen(strModel) + strlen(out) + 2) >= MAXIDLENGTH)
- {
- /* There is not enough space to add a space and
- the model to the id, so we create that space. */
- out[MAXIDLENGTH - strlen(strModel) - 2] = 0;
- }
- strcat(out, " ");
- strcat(out, strModel);
- }
+ /* Process device model name, if any */
+ if(strlen(strModel) != 0)
+ {
+ if((strlen(strModel) + strlen(out) + 2) >= MAXIDLENGTH)
+ {
+ /* There is not enough space to add a space and
+ the model to the id, so we create that space. */
+ out[MAXIDLENGTH - strlen(strModel) - 2] = 0;
+ }
+ strcat(out, " ");
+ strcat(out, strModel);
+ }
}
void addDeviceInfo(char *out, int *pOutLen, const char device[], const char property[])
{
- char source[MAXIDLENGTH];
- char buf[MAXIDLENGTH];
+ char source[MAXIDLENGTH];
+ char buf[MAXIDLENGTH];
- /* Make sure that out has a null character to terminate it,
- this is necessary when using strcat */
- out[*pOutLen] = 0;
+ /* Make sure that out has a null character to terminate it,
+ this is necessary when using strcat */
+ out[*pOutLen] = 0;
- /* Constructing source of desired info */
- strcpy(source, device);
+ /* Constructing source of desired info */
+ strcpy(source, device);
strcat(source, property);
- /* Concatenating desired info to target,
- if such info appears to be found */
- int f = open(source, 0);
- if (f == -1)
+ /* Concatenating desired info to target,
+ if such info appears to be found */
+ int f = open(source, 0);
+ if (f == -1)
return;
int len = read(f, buf, MAXIDLENGTH);
- if (len <= 0)
+ if (len <= 0)
return;
- /* Update the out variables */
- *pOutLen += len;
- buf[len-1] = 0;
- strcat(out, (const char*) buf);
- tidyStringUp(out);
- *pOutLen = strlen(out) + 1;
+ /* Update the out variables */
+ *pOutLen += len;
+ buf[len-1] = 0;
+ strcat(out, (const char*) buf);
+ tidyStringUp(out);
+ *pOutLen = strlen(out) + 1;
}
int checkIfItemMounted(char *devicePath)
{
- char outputBuffer[MAXDEVICEPATHLENGTH];
- FILE *file = openOrHang("/proc/mounts", "r"); /* util.c */
- while(!feof(file)) {
- if (fgets(outputBuffer,sizeof(outputBuffer),file)) {
- /* Check if this line starts with our item */
- if(strncmp(outputBuffer, devicePath, strlen(devicePath)) != 0)
- continue;
- /* It does */
- return 1;
- }
- }
+ char outputBuffer[MAXDEVICEPATHLENGTH];
+ FILE *file = openOrHang("/proc/mounts", "r"); /* util.c */
+ while(!feof(file)) {
+ if (fgets(outputBuffer,sizeof(outputBuffer),file)) {
+ /* Check if this line starts with our item */
+ if(strncmp(outputBuffer, devicePath, strlen(devicePath)) != 0)
+ continue;
+ /* It does */
+ return 1;
+ }
+ }
fclose(file);
- return 0;
+ return 0;
}
void getMountPoint(char *out, char *devicePath)
{
- char outputBuffer[MAXMOUNTPOINTLENGTH];
- char *p1; /* To point at the start of the mount point */
- char *p2; /* To point at the end of the mount point */
- FILE *file = openOrHang("/proc/mounts", "r"); /* util.c */
- out[0] = '\0'; /* Necessary for strcat */
- while(!feof(file)) {
- if (fgets(outputBuffer,sizeof(outputBuffer),file)) {
- /* Check if this line starts with our item */
- if(strncmp(outputBuffer, devicePath, strlen(devicePath)) != 0)
- continue;
- /* It does */
+ char outputBuffer[MAXMOUNTPOINTLENGTH];
+ char *p1; /* To point at the start of the mount point */
+ char *p2; /* To point at the end of the mount point */
+ FILE *file = openOrHang("/proc/mounts", "r"); /* util.c */
+ out[0] = '\0'; /* Necessary for strcat */
+ while(!feof(file)) {
+ if (fgets(outputBuffer,sizeof(outputBuffer),file)) {
+ /* Check if this line starts with our item */
+ if(strncmp(outputBuffer, devicePath, strlen(devicePath)) != 0)
+ continue;
+ /* It does */
- p1 = strchr(outputBuffer, ' ');
- if(!p1)
- {
- syslog(LOG_ERR, "Exiting with failure: getMountPoint received invalid device path \"%s\"", devicePath);
- exit(EXIT_FAILURE);
- }
- p1++;
- /* p1 now points at the start of the mount point*/
+ p1 = strchr(outputBuffer, ' ');
+ if(!p1)
+ {
+ syslog(LOG_ERR, "Exiting with failure: getMountPoint received invalid device path \"%s\"", devicePath);
+ exit(EXIT_FAILURE);
+ }
+ p1++;
+ /* p1 now points at the start of the mount point*/
- p2 = strchr(p1, ' ');
- if(!p2)
- {
- syslog(LOG_ERR, "Exiting with failure: getMountPoint received invalid device path \"%s\"", devicePath);
- exit(EXIT_FAILURE);
- }
- /* p2 now points at the end of the mount point*/
- *p2 = 0;
+ p2 = strchr(p1, ' ');
+ if(!p2)
+ {
+ syslog(LOG_ERR, "Exiting with failure: getMountPoint received invalid device path \"%s\"", devicePath);
+ exit(EXIT_FAILURE);
+ }
+ /* p2 now points at the end of the mount point*/
+ *p2 = 0;
- /* Place only the mount point in the out parameter */
- strcat(out, p1);
- }
- }
+ /* Place only the mount point in the out parameter */
+ strcat(out, p1);
+ }
+ }
fclose(file);
}

0 comments on commit 4d0702b

Please sign in to comment.