示例#1
0
strArray        *cpyStrArray(strArray *array)
{
	int           i;
	strArray      *copy = newStrArray(array->len);

	if (copy == NULL)
	{
		return NULL;
	}

	for (i = 0; i < copy->len; i++)
	{
		if (array->vals[i] != NULL && (copy->vals[i] = strdup(array->vals[i])) == NULL)
		{
			freeStrArray(copy);

			return NULL;
		}
	}

	return (copy);
}
示例#2
0
文件: restore.c 项目: abgood/backup
int main (int argc, const char * argv[], char* envp[])
{
    
    // usage: rstr dir2 dir3
    if ( argc != 3 ) {
        fprintf ( stderr, "Usage: %s dir_backup dir_restore\n", argv[0] );
        exit(1);
    }
    
    char * pwd = getenv("PWD");
    DIR *backupDir;
    DIR *restoreDir;
    
    if ( ( backupDir = opendir(argv[1]) ) == NULL ) {
        free(backupDir);
        perror(argv[1]);
        exit(2);
    }
    
    if ( ( restoreDir = opendir(argv[2]) ) == NULL ) {
        // if restore dir doesnt exist, create it
        if(mkdir(argv[2], S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
            perror("Problem creating restore destination");
            exit(5);
        }
        if ( ( restoreDir = opendir(argv[2]) ) == NULL ) {
            perror(argv[2]);
            closedir(backupDir);
            free(backupDir);
            free(restoreDir);
            exit(3);
        }
    }
    
    printf("The following restore points are available:\n");
    // moving into backupDir will allow directory reading
    if(chdir(argv[1]) != 0) {
        perror(argv[1]);
        exit(8);
    }
    
    char** backups = getAndPrintFolders(backupDir);
    int numberOfBackups = getNumOfBackups(backupDir);
    
    char prompt[MAX_LEN];
    sprintf(prompt,"Which restore point? (0 to cancel)\n%s", PROMPT);
    int lineNumber = getChoice(prompt, numberOfBackups);
    //User sees numbers from 1 to numberOfBackups, but we want it from 0 to numberOfBackups-1 due to array access
    if(lineNumber == 0) {
        printf("Restore program ended.\n");
        freeStrArray(backups, numberOfBackups);
        return 0;
    }
    
    lineNumber = lineNumber-1;
    
    char* selectedBckpPath = getBackupFullPath(argv[1], backups[lineNumber]);
    char* fullBckpInfoPath = getBackupInfo(selectedBckpPath);
    
    // moving out of the backupDir
    if(chdir(pwd) != 0) {
        perror(pwd);
        exit(8);
    }
    
    DIR *selectedBackup;
    if ( ( selectedBackup = opendir(selectedBckpPath) ) == NULL ) {
        freeStrArray(backups, numberOfBackups);
        perror(selectedBckpPath);
        exit(2);
    }
    
    char * datestr = backupDateToReadableDate(backups[lineNumber]);
    printf("\n%s restore point chosen.\n", datestr);
    free(datestr);
    freeStrArray(backups, numberOfBackups);
    printf("This backup contains the following files:\n\n");
    printBackupInfo(fullBckpInfoPath);
    
    int numberOfFiles = getNumOfLines(fullBckpInfoPath);
    sprintf(prompt,"\nSelect a file to restore (0 to cancel):\n%s", PROMPT);
    lineNumber = getChoice(prompt, numberOfFiles  + 1);
    
    printf("\n");
    if ( lineNumber == numberOfFiles + 1 ) {
        
        printf("Doing full restore!\n");
        char* fileRestorePath;
        char* destFilePath;
        char* originFilePath;
        
        int i;
        for (i = 1 ; i <= getNumOfLines(fullBckpInfoPath); i++ ) {
            
            fileRestorePath = getLineAt(i, fullBckpInfoPath);
            char* fileName = getFileNameFromInfoLine(fileRestorePath);
            destFilePath = getFileFullPath(argv[2], fileName);
            originFilePath = getFileFullPath(argv[1], fileRestorePath);
            
            if(copyFile(originFilePath, destFilePath) == 0)
                printf("Restored %s successfully!\n", fileName);
            else
                printf("Error copying %s!\n", fileName);
            
            free(fileRestorePath);
            free(fileName);
            free(destFilePath);
            free(originFilePath);
        }
        
    } else if ( lineNumber > 0 && lineNumber <= numberOfFiles ) {
        char* fileRestorePath = getLineAt(lineNumber, fullBckpInfoPath);
        char* fileName = getFileNameFromInfoLine(fileRestorePath);
        char* destFilePath = getFileFullPath( argv[2], fileName );
        char* originFilePath = getFileFullPath( argv[1], fileRestorePath );
        if(copyFile(originFilePath, destFilePath) == 0)
            printf("Restored %s successfully!\n", fileName);
        else
            printf("Error copying %s!\n", fileName);

        free(fileRestorePath);
        free(fileName);
        free(destFilePath);
        free(originFilePath);
    }
        
    // clean up the strings
    free(selectedBckpPath);
    free(fullBckpInfoPath);    
    
    closedir(backupDir);
    closedir(restoreDir);
    closedir(selectedBackup);
    
    printf("\nRestore program ended.\n");
    return 0;
}