コード例 #1
0
ファイル: reconstruct.c プロジェクト: cyberpear/cyrus-imapd
int main(int argc, char **argv)
{
    int opt, i, r;
    int rflag = 0;
    int mflag = 0;
    int fflag = 0;
    int xflag = 0;
    char buf[MAX_MAILBOX_PATH+1];
    char *fname;
    strarray_t discovered = STRARRAY_INITIALIZER;
    char *alt_config = NULL;
    char *start_part = NULL;

    if ((geteuid()) == 0 && (become_cyrus(/*is_master*/0) != 0)) {
	fatal("must run as the Cyrus user", EC_USAGE);
    }

    construct_hash_table(&unqid_table, 2047, 1);

    while ((opt = getopt(argc, argv, "C:kp:rmfsxgGqRUoOnV:")) != EOF) {
	switch (opt) {
	case 'C': /* alt config file */
	    alt_config = optarg;
	    break;

	case 'p':
	    start_part = optarg;
	    break;

	case 'r':
	    rflag = 1;
	    break;

	case 'm':
	    mflag = 1;
	    break;

	case 'n':
	    reconstruct_flags &= ~RECONSTRUCT_MAKE_CHANGES;
	    break;

	case 'g':
	    fprintf(stderr, "reconstruct: deprecated option -g ignored\n");
	    break;

	case 'G':
	    reconstruct_flags |= RECONSTRUCT_ALWAYS_PARSE;
	    break;

	case 'f':
	    fflag = 1;
	    break;

	case 'x':
	    xflag = 1;
	    break;

	case 'k':
	    fprintf(stderr, "reconstruct: deprecated option -k ignored\n");
	    break;

	case 's':
	    reconstruct_flags &= ~RECONSTRUCT_DO_STAT;
	    break;

	case 'q':
	    reconstruct_flags |= RECONSTRUCT_QUIET;
	    break;

	case 'R':
	    reconstruct_flags |= RECONSTRUCT_GUID_REWRITE;
	    break;

	case 'U':
	    reconstruct_flags |= RECONSTRUCT_GUID_UNLINK;
	    break;

	case 'o':
	    reconstruct_flags |= RECONSTRUCT_IGNORE_ODDFILES;
	    break;

	case 'O':
	    reconstruct_flags |= RECONSTRUCT_REMOVE_ODDFILES;
	    break;

	case 'V':
	    setversion = atoi(optarg);
	    break;

	default:
	    usage();
	}
    }

    cyrus_init(alt_config, "reconstruct", 0, CONFIG_NEED_PARTITION_DATA);
    global_sasl_init(1,0,NULL);

    /* Set namespace -- force standard (internal) */
    if ((r = mboxname_init_namespace(&recon_namespace, 1)) != 0) {
	syslog(LOG_ERR, "%s", error_message(r));
	fatal(error_message(r), EC_CONFIG);
    }

    sync_log_init();

    if (mflag) {
	if (rflag || fflag || optind != argc) {
	    cyrus_done();
	    usage();
	}
	do_mboxlist();
    }

    mboxlist_init(0);
    mboxlist_open(NULL);

    quotadb_init(0);
    quotadb_open(NULL);

#ifdef WITH_DAV
    caldav_init();
    carddav_init();
#endif

    /* Deal with nonexistent mailboxes */
    if (start_part) {
	/* We were handed a mailbox that does not exist currently */
	if(optind == argc) {
	    fprintf(stderr,
		    "When using -p, you must specify a mailbox to attempt to reconstruct.");
	    exit(EC_USAGE);
	}

	/* do any of the mailboxes exist in mboxlist already? */
	/* Do they look like mailboxes? */
	for (i = optind; i < argc; i++) {
	    struct stat sbuf;

	    if (strchr(argv[i],'%') || strchr(argv[i],'*')) {
		fprintf(stderr, "Using wildcards with -p is not supported.\n");
		exit(EC_USAGE);
	    }

	    /* Translate mailboxname */
	    (*recon_namespace.mboxname_tointernal)(&recon_namespace, argv[i],
						   NULL, buf);

	    /* Does it exist */
	    do {
		r = mboxlist_lookup(buf, NULL, NULL);
	    } while (r == IMAP_AGAIN);

	    if (r != IMAP_MAILBOX_NONEXISTENT) {
		fprintf(stderr,
			"Mailbox %s already exists.  Cannot specify -p.\n",
			argv[i]);
		exit(EC_USAGE);
	    }

	    /* Does the suspected path *look* like a mailbox? */
	    fname = mboxname_metapath(start_part, buf, META_HEADER, 0);
	    if (stat(fname, &sbuf) < 0) {
		fprintf(stderr,
			"%s does not appear to be a mailbox (no %s).\n",
			argv[i], fname);
		exit(EC_USAGE);
	    }
	}

	/* None of them exist.  Create them. */
	for (i = optind; i < argc; i++) {
	    /* Translate mailboxname */
	    (*recon_namespace.mboxname_tointernal)(&recon_namespace, argv[i],
						   NULL, buf);

	    /* don't notify mailbox creation here */
	    r = mboxlist_createmailbox(buf, 0, start_part, 1,
				       "cyrus", NULL, 0, 0, !xflag, 0, NULL);
	    if (r) {
		fprintf(stderr, "could not create %s\n", argv[i]);
	    }
	}
    }

    /* Normal Operation */
    if (optind == argc) {
	if (rflag) {
	    fprintf(stderr, "please specify a mailbox to recurse from\n");
	    cyrus_done();
	    exit(EC_USAGE);
	}
	assert(!rflag);
	strlcpy(buf, "*", sizeof(buf));
	(*recon_namespace.mboxlist_findall)(&recon_namespace, buf, 1, 0, 0,
					    do_reconstruct, NULL);
    }

    for (i = optind; i < argc; i++) {
	char *domain = NULL;

	/* save domain */
	if (config_virtdomains) domain = strchr(argv[i], '@');

	strlcpy(buf, argv[i], sizeof(buf));
	/* Translate any separators in mailboxname */
	mboxname_hiersep_tointernal(&recon_namespace, buf,
				    config_virtdomains ?
				    strcspn(buf, "@") : 0);

	/* reconstruct the first mailbox/pattern */
	(*recon_namespace.mboxlist_findall)(&recon_namespace, buf, 1, 0,
					    0, do_reconstruct,
					    fflag ? &discovered : NULL);
	if (rflag) {
	    /* build a pattern for submailboxes */
	    char *p = strchr(buf, '@');
	    if (p) *p = '\0';
	    strlcat(buf, ".*", sizeof(buf));

	    /* append the domain */
	    if (domain) strlcat(buf, domain, sizeof(buf));

	    /* reconstruct the submailboxes */
	    (*recon_namespace.mboxlist_findall)(&recon_namespace, buf, 1, 0,
						0, do_reconstruct,
						fflag ? &discovered : NULL);
	}
    }

    /* examine our list to see if we discovered anything */
    while (discovered.count) {
	char *name = strarray_shift(&discovered);
	int r = 0;

	/* create p (database only) and reconstruct it */
	/* partition is defined by the parent mailbox */
	/* don't notify mailbox creation here */
	r = mboxlist_createmailbox(name, 0, NULL, 1,
				   "cyrus", NULL, 0, 0, !xflag, 0, NULL);
	if (r) {
	    fprintf(stderr, "createmailbox %s: %s\n",
		    name, error_message(r));
	} else {
	    do_reconstruct(name, strlen(name), 0, &discovered);
	}
	/* may have added more things into our list */

	free(name);
    }

    free_hash_table(&unqid_table, free);

    sync_log_done();

    mboxlist_close();
    mboxlist_done();

    quotadb_close();
    quotadb_done();

    partlist_local_done();
#ifdef WITH_DAV
    carddav_done();
    caldav_done();
#endif

    cyrus_done();

    strarray_fini(&discovered);

    return 0;
}
コード例 #2
0
ファイル: mbpath.c プロジェクト: pmhahn/cyrus-imapd
int
main(int argc, char **argv)
{
  struct mboxlist_entry *mbentry = NULL;
  int rc, i, quiet = 0, stop_on_error=0, metadata=0;
  int opt;		/* getopt() returns an int */
  char *alt_config = NULL;
  char buf[MAX_MAILBOX_PATH+1];

  if ((geteuid()) == 0 && (become_cyrus() != 0)) {
      fatal("must run as the Cyrus user", EC_USAGE);
  }

  while ((opt = getopt(argc, argv, "C:qsm")) != EOF) {
    switch(opt) {
    case 'C': /* alt config file */
      alt_config = optarg;
      break;
    case 'q':
      quiet = 1;
      break;
    case 's':
      stop_on_error = 1;
      break;
    case 'm':
	metadata = 1;
	break;

    default:
      usage();
    }
  }

  cyrus_init(alt_config, "mbpath", 0);

  if ((rc = mboxname_init_namespace(&mbpath_namespace, 1)) != 0) {
    fatal(error_message(rc), -1);
  }

  mboxlist_init(0);
  mboxlist_open(NULL);

  for (i = optind; i < argc; i++) {
    /* Translate mailboxname */
    (*mbpath_namespace.mboxname_tointernal)(&mbpath_namespace,
					    argv[i], NULL, buf);

    if ((rc = mboxlist_lookup(buf, &mbentry, NULL)) == 0) {
      if (mbentry->mbtype & MBTYPE_REMOTE) {
	printf("%s!%s\n", mbentry->server, mbentry->partition);
      } else if (metadata) {
	const char *path = mboxname_metapath(mbentry->partition, 
					     mbentry->name, 0, 0);
	printf("%s\n", path);
      }
      else {
	const char *path = mboxname_datapath(mbentry->partition, 
					     mbentry->name, 0);
	printf("%s\n", path);
      }
      mboxlist_entry_free(&mbentry);
    } else {
      if (!quiet && (rc == IMAP_MAILBOX_NONEXISTENT)) {
	fprintf(stderr, "Invalid mailbox name: %s\n", argv[i]);
      }
      if (stop_on_error) {
	if (quiet) {
	  fatal("", -1);
	} else {
	  fatal("Error in processing mailbox. Stopping\n", -1);
	}
      }
    }
  }

  mboxlist_close();
  mboxlist_done();

  cyrus_done();

  return 0;
}