/* Open the file. For MU_STREAM_READ, the code tries mmap() first and fall back to normal file. */ static int mbox_open (mu_mailbox_t mailbox, int flags) { mbox_data_t mud = mailbox->data; int status = 0; if (mud == NULL) return EINVAL; mailbox->flags = flags; /* Get a stream. */ if (mailbox->stream == NULL) { /* We do not try to mmap for CREAT or APPEND, it is not supported. */ status = (flags & MU_STREAM_CREAT) || (mailbox->flags & MU_STREAM_APPEND); /* Try to mmap () the file first. */ if (status == 0) { status = mu_mapfile_stream_create (&mailbox->stream, mud->name, mailbox->flags); if (status == 0) { status = mu_stream_open (mailbox->stream); } } /* Fall back to normal file if mmap() failed. */ if (status != 0) { status = mu_file_stream_create (&mailbox->stream, mud->name, mailbox->flags); if (status != 0) return status; status = mu_stream_open (mailbox->stream); } /* All failed, bail out. */ if (status != 0) { mu_stream_destroy (&mailbox->stream, NULL); return status; } /* Even on top of normal FILE *, lets agressively cache. But this may not be suitable for system tight on memory. */ mu_stream_setbufsiz (mailbox->stream, BUFSIZ); } else { status = mu_stream_open (mailbox->stream); if (status != 0) return status; } MU_DEBUG2 (mailbox->debug, MU_DEBUG_TRACE1, "mbox_open (%s, 0x%x)\n", mud->name, mailbox->flags); if (mailbox->locker == NULL) status = mu_locker_create (&(mailbox->locker), mud->name, 0); return status; }
static int nntp_folder_open (mu_folder_t folder, int flags) { f_nntp_t f_nntp = folder->data; mu_stream_t carrier = NULL; const char *host; unsigned port = MU_NNTP_DEFAULT_PORT; /* default nntp port. */ int status = 0; /* If we are already open for business, noop. */ mu_monitor_wrlock (folder->monitor); if (f_nntp->isopen) { mu_monitor_unlock (folder->monitor); return 0; } mu_monitor_unlock (folder->monitor); /* Fetch the server name and the port in the mu_url_t. */ status = mu_url_sget_host (folder->url, &host); if (status != 0) return status; mu_url_get_port (folder->url, &port); folder->flags = flags; /* Create the networking stack. */ status = mu_tcp_stream_create (&carrier, host, port, folder->flags); if (status != 0) return status; /* Ask for the stream internal buffering mechanism scheme. */ mu_stream_setbufsiz (carrier, BUFSIZ); mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_PROT, ("folder_nntp_open (%s:%ld)", host, port)); status = mu_nntp_create (&f_nntp->nntp); if (status == 0) { status = mu_nntp_set_carrier (f_nntp->nntp, carrier); if (status == 0) { status = mu_nntp_connect (f_nntp->nntp); if (status == 0) { mu_monitor_wrlock (folder->monitor); f_nntp->isopen++; mu_monitor_unlock (folder->monitor); } } } return status; }