Exemple #1
0
/*
 * Flush outbound queue to real outbound.
 */
void flush_queue(void)
{
    char	    *temp;
    struct dirent   *de;
    DIR		    *dp;
    
    Syslog('+', "Flushing outbound queue");
    if (enoughspace(CFG.freespace) == 0) {
	Syslog('+', "Low diskspace, not flushing outbound queue");
	return;
    }

    IsDoing("Flush queue");
    if (!do_quiet) {
	mbse_colour(LIGHTBLUE, BLACK);
	printf("Flushing outbound queue\n");
	mbse_colour(CYAN, BLACK);
    }
    
    temp = calloc(PATH_MAX, sizeof(char));
    snprintf(temp, PATH_MAX, "%s/foobar", CFG.out_queue);
    mkdirs(temp, 0750);

    if ((dp = opendir(CFG.out_queue)) == 0) {
	WriteError("$Can't open %s", CFG.out_queue);
	free(temp);
	return;
    }
    
    /*
     * The outbound queue contains subdirectories which actuallly hold the
     * queue outbound files. Process each found subdirectory.
     */
    while ((de = readdir(dp))) {
	if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
	    snprintf(temp, PATH_MAX, "%s/%s", CFG.out_queue, de->d_name);
	    Syslog('p', "Queue directory %s", temp);
	    flush_dir(de->d_name);
	    if (chdir(CFG.out_queue))
		WriteError("$Can't chdir to %s", CFG.out_queue);
	    if (rmdir(temp))
		WriteError("$Can't rmdir %s", temp);
	}
    }
    closedir(dp);
    free(temp);

    if (!do_quiet) {
	printf("\r                                              \r");
	fflush(stdout);
    }
}
Exemple #2
0
static int mms_queue_flush(char *dir)
{
	List *stack = gwlist_create();
	Octstr *xdir = NULL;

	gwlist_append(stack, octstr_create(""));
	int ret = 0; 

	while (NULL != (xdir = gwlist_extract_first(stack)))  {
		ret += flush_dir(dir, octstr_get_cstr(xdir), stack);
		octstr_destroy(xdir);
	}

	gwlist_destroy(stack,octstr_destroy_item);

	return ret;
}
Exemple #3
0
static int rewrite_db(struct database *db)
{
	FILE *fhtmp;
	int err;

	int tmplen = strlen(db->fn) + 10;
	char fn_complete[tmplen], fn_old[tmplen], fn_out[tmplen];

	sprintf(fn_complete, "%s.complete", db->fn);
	sprintf(fn_old, "%s~", db->fn);
	sprintf(fn_out, "%s.out", db->fn);

	fhtmp = fopen(fn_out, "w");
	if (!fhtmp) {
		DBerror("Cannot open `%s' output file: %s\n", fn_out,
			strerror(errno));
		return -1;
	}	
	
	dump_database(db, fhtmp);
	
	err = 0;
	/* Finish the output file */
	if (ferror(fhtmp) || fflush(fhtmp) != 0 || fsync(fileno(fhtmp)) != 0 ||
	    fclose(fhtmp))
		err = -1;
	/* Rename to .complete */	
	else if (force_rename(fn_out, fn_complete))
		err = -1;
	/* RED-PEN: need to do retry for race */	
	/* Move to final name */
	else if (force_rename(db->fn, fn_old) || rename(fn_complete, db->fn))
		err = -1;
	/* Hit disk */	
	else if (flush_dir(db->fn))
		err = -1;
		
	if (err) {
		DBerror("Error writing to database %s: %s\n", db->fn,
				strerror(errno));
	}
				
	return err;			
}
/**On startup, the inotifiy handlers are created, and existing files shall be sent into DLT stream
 * @param opts FiletransferOptions
 * @return Returns 0 if everything was okay. If there was a failure a value < 0 will be returned.
 */
int init_filetransfer_dirs(FiletransferOptions const *opts)
{
    DLT_LOG(dltsystem, DLT_LOG_DEBUG,
            DLT_STRING("dlt-system-filetransfer, initializing inotify on directories."));
    int i;
#ifdef linux
    ino.handle = inotify_init();

    if(ino.handle < 0)
    {
        DLT_LOG(filetransferContext, DLT_LOG_FATAL,
                DLT_STRING("Failed to initialize inotify in dlt-system file transfer."));
        return -1;
    }
#endif

    for(i = 0;i < opts->Count;i++)
    {
        //create subdirectories for processing the files

        char *subdirpath;
        int len = strlen(opts->Directory[i])+strlen(SUBDIR_COMPRESS)+2;
        subdirpath= malloc (len);
        MALLOC_ASSERT(subdirpath);
        snprintf(subdirpath,len,"%s/%s",opts->Directory[i],SUBDIR_COMPRESS);
        int ret = mkdir(subdirpath,0777);

        if (0 != ret && EEXIST != errno){
            DLT_LOG(dltsystem, DLT_LOG_ERROR,
                    DLT_STRING("dlt-system-filetransfer, error creating subdirectory: "),DLT_STRING(subdirpath),DLT_STRING(" Errorcode: "),DLT_INT(errno));
            free (subdirpath);
            return -1;
        }
        free(subdirpath);

        len = strlen(opts->Directory[i])+strlen(SUBDIR_TOSEND)+2;
        subdirpath= malloc (len);
        MALLOC_ASSERT(subdirpath);
        snprintf(subdirpath,len,"%s/%s",opts->Directory[i],SUBDIR_TOSEND);
        ret = mkdir(subdirpath,0777);
        if (0 != ret && EEXIST != errno){
            DLT_LOG(dltsystem, DLT_LOG_ERROR,
                    DLT_STRING("dlt-system-filetransfer, error creating subdirectory: "),DLT_STRING(subdirpath),DLT_STRING(" Errorcode: "),DLT_INT(errno));
            free (subdirpath);
            return -1;
        }
        free(subdirpath);

#ifdef linux
        ino.fd[i] = inotify_add_watch(ino.handle, opts->Directory[i],
                                      IN_CLOSE_WRITE|IN_MOVED_TO);
        if(ino.fd[i] < 0)
        {
            char buf[1024];
            snprintf(buf, 1024, "Failed to add inotify watch to directory %s in dlt-system file transfer.",
                     opts->Directory[i]);
            DLT_LOG(filetransferContext, DLT_LOG_FATAL,
                    DLT_STRING(buf));
            return -1;
        }
#endif

        flush_dir(opts, i);

    }
    return 0;
}