int main(int argc, char *argv[])
{
	ST stack;
	char *src_folder, *dst_folder;
	int src_malloc, dst_malloc;

	if (argc < 3)
	{
		fprintf(stderr, "Usage: out <sourceFolder> <destFolder>\n");
		exit(1);
	}

	// This whole malloc stuff is for _adding_ the possible trailing '/'
	src_malloc = strlen(argv[1]);
	src_folder = malloc(sizeof(char) * (src_malloc + 1 + 1));
	if (argv[1][src_malloc - 1] != '/') sprintf(src_folder, "%s/", argv[1]);
	else sprintf(src_folder, "%s", argv[1]);

	dst_malloc = strlen(argv[2]);
	dst_folder = malloc(sizeof(char) * (dst_malloc + 1 + 1));
	if (argv[2][dst_malloc - 1] != '/') sprintf(dst_folder, "%s/", argv[2]);
	else sprintf(dst_folder, "%s", argv[2]);
	
	
	if (isNotDir(src_folder))
	{
		fprintf(stderr, "There was en error with the source folder, exiting\n");
		exit(1);
	}

	if (isNotDir(dst_folder))
	{
		if (mkdir(dst_folder, 0755) != 0)
		{
			fprintf(stderr, "There was en error with the dest folder, exiting\n");
			exit(1);
		}
	}

	stack = STinit();

	// Ugly hack to unify the functions below
	STpush(stack, strdup(""));

	recursiveCopy(stack, src_folder, dst_folder);

	return 0;
}
Exemplo n.º 2
0
char doJobs( jobs jlist[], int number_of_jobs )
{    
    int result, check;
    int index = 0;
    int mt, d, y, h, m, s, update_index;
    char time_update_buff [strlen(jlist[ index ].dateTime)];
    
    time_t epoch_time;
    struct tm *tm_p = NULL;
    epoch_time = time( NULL );
    tm_p = localtime( &epoch_time );
 
    jberrno = 0;
    
    for ( index; index < number_of_jobs; index++)
    {
        // If the job needs to be executed -> do it
        if( shouldExecute( jlist[ index ].dateTime ))
        {
            result = recursiveCopy( jlist[ index ].from, jlist[ index ].to, 1);
        }
        else
        {
            if (BUGS)
                printf("\nShould not be ran today.\n");
        }

        // If something went wrong with the backup, notify. 
        if ( result )
        {
            jberrno = ERROR_RECURSING_JOB_DIR;
            if(BUGS) printf("\n%s %s\n\n", jbetoa(),  jlist[ index ].title);
        }
        else
        {
            // UPDATE MONTH / DAY to next update period.
            // IF MONTH > -1 : ++month - leave day alone
            // IF MONTH == -1 && day > -1 ++day
            // THIS IS ALL ASSUMING THAT ITS NOT A YEARLY BACKUP, AND 
            // THAT HOUR/MIN/SEC DOESNT CHANGE
            // TURN ALL TO INTS FOR LATER IF NEEDS TO BE MODIFIED
            
            sscanf(jlist[ index ].dateTime, 
            "%d"TIME_SEPARATOR"%d"TIME_SEPARATOR
            "%d"TIME_SEPARATOR"%d"TIME_SEPARATOR
            "%d"TIME_SEPARATOR"%d"TIME_SEPARATOR"%d",
            &y, &mt, &d, &h, &m, &s, &update_index);
            
            // NEED TO BETTER MANAGE THE INCRIMENTING OF DAYS
            if( mt > -1 )
            {
                if( mt+update_index > 12) // If its larger than 12
                {
                     mt = (mt+update_index)%12; //set num months larger.
                }
                else
                {
                    tm_p->tm_mon += update_index+1;     // Adds index to mon, Leave the +1
                    mt = tm_p->tm_mon;
                }
            }
            if( mt == -1 && d > -1 )                    // If mon not given, assume daily
            {
                if ( tm_p->tm_mday >= 28 )              // not quite monthly
                    tm_p->tm_mday = 0;
                tm_p->tm_mday += update_index;
                d = tm_p->tm_mday;
            }
            // Set time_update_buff before updating job file
            check = sprintf(time_update_buff,
            "%d"TIME_SEPARATOR"%d"TIME_SEPARATOR
            "%d"TIME_SEPARATOR"%d"TIME_SEPARATOR
            "%d"TIME_SEPARATOR"%d"TIME_SEPARATOR"%d",
            y, mt, d, h, m, s, update_index);
                
            if ( check < 0 )
            {
                jberrno = ERROR_SETTING_TIME_BUFF;
                if(BUGS) printf("\n%s\n", jbetoa());
                return -1;   
            }
            else
            {
                strcpy(jlist[ index ].dateTime, time_update_buff);
                updateJobFile( jlist, index );
            }
        }   // END OF RESULT ELSE
    }   // END JOB LOOP
	return 0;
}