/* * Open the device with the operating system and * initialize buffer pointers. * * Returns: true on success * false on error * * Note, for a tape, the VolName is the name we give to the * volume (not really used here), but for a file, the * VolName represents the name of the file to be created/opened. * In the case of a file, the full name is the device name * (archive_name) with the VolName concatenated. */ bool DEVICE::open(DCR *dcr, int omode) { char preserve[ST_BYTES]; clear_all_bits(ST_MAX, preserve); if (is_open()) { if (open_mode == omode) { return true; } else { d_close(m_fd); clear_opened(); Dmsg0(100, "Close fd for mode change.\n"); if (bit_is_set(ST_LABEL, state)) set_bit(ST_LABEL, preserve); if (bit_is_set(ST_APPENDREADY, state)) set_bit(ST_APPENDREADY, preserve); if (bit_is_set(ST_READREADY, state)) set_bit(ST_READREADY, preserve); } } if (dcr) { dcr->setVolCatName(dcr->VolumeName); VolCatInfo = dcr->VolCatInfo; /* structure assign */ } Dmsg4(100, "open dev: type=%d dev_name=%s vol=%s mode=%s\n", dev_type, print_name(), getVolCatName(), mode_to_str(omode)); clear_bit(ST_LABEL, state); clear_bit(ST_APPENDREADY, state); clear_bit(ST_READREADY, state); clear_bit(ST_EOT, state); clear_bit(ST_WEOT, state); clear_bit(ST_EOF, state); label_type = B_BAREOS_LABEL; /* * We are about to open the device so let any plugin know we are. */ if (dcr && generate_plugin_event(dcr->jcr, bsdEventDeviceOpen, dcr) != bRC_OK) { Dmsg0(100, "open_dev: bsdEventDeviceOpen failed\n"); return false; } Dmsg1(100, "call open_device mode=%s\n", mode_to_str(omode)); open_device(dcr, omode); /* * Reset any important state info */ clone_bits(ST_MAX, preserve, state); Dmsg2(100, "preserve=0x%x fd=%d\n", preserve, m_fd); return m_fd >= 0; }
/* * Close the device */ bool DEVICE::close() { bool ok = true; Dmsg4(40, "close_dev vol=%s fd=%d dev=%p dev=%s\n", VolHdr.VolumeName, m_fd, this, print_name()); offline_or_rewind(); if (!is_open()) { Dmsg2(200, "device %s already closed vol=%s\n", print_name(), VolHdr.VolumeName); return true; /* already closed */ } switch (dev_type) { case B_VTL_DEV: case B_VTAPE_DEV: case B_TAPE_DEV: unlock_door(); /* Fall through wanted */ default: if (d_close(m_fd) != 0) { berrno be; dev_errno = errno; Mmsg2(errmsg, _("Error closing device %s. ERR=%s.\n"), print_name(), be.bstrerror()); ok = false; } break; } unmount(1); /* do unmount if required */ /* Clean up device packet so it can be reused */ clear_opened(); /* * Be careful not to clear items needed by the DVD driver * when it is closing a single part. */ state &= ~(ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF| ST_NOSPACE|ST_MOUNTED|ST_MEDIA|ST_SHORT); label_type = B_BACULA_LABEL; file = block_num = 0; file_size = 0; file_addr = 0; EndFile = EndBlock = 0; openmode = 0; clear_volhdr(); memset(&VolCatInfo, 0, sizeof(VolCatInfo)); if (tid) { stop_thread_timer(tid); tid = 0; } return ok; }
/////////////////////////////////////////////////////////////////////////// // Name: CloseDatabase // // Description: // Close the database and task. // // Declaration: // void CDbVista::CloseDatabase() // // Input: none // // Output: none // // Return: none // // date / author revision // ----------------- -------- // 03-Dec-2001 SFK Created from CloseDatabase in DbGeneralDbSupport.cpp ////////////////////////////////////////////////////////////////// void CDbVista::CloseDatabase() { if (mbDatabaseOpen) { //SetCurrentDrive(); // implement in new version??? SFK d_close(); dt_closetask(&Currtask); mbDatabaseOpen = false; } }
/* * Open the device with the operating system and * initialize buffer pointers. * * Returns: true on success * false on error * * Note, for a tape, the VolName is the name we give to the * volume (not really used here), but for a file, the * VolName represents the name of the file to be created/opened. * In the case of a file, the full name is the device name * (archive_name) with the VolName concatenated. */ bool DEVICE::open(DCR *dcr, int omode) { int preserve = 0; if (is_open()) { if (openmode == omode) { return true; } else { Dmsg1(200, "Close fd=%d for mode change in open().\n", m_fd); d_close(m_fd); clear_opened(); preserve = state & (ST_LABEL|ST_APPEND|ST_READ); } } if (dcr) { dcr->setVolCatName(dcr->VolumeName); VolCatInfo = dcr->VolCatInfo; /* structure assign */ } state &= ~(ST_NOSPACE|ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF); label_type = B_BACULA_LABEL; if (is_tape() || is_fifo()) { open_tape_device(dcr, omode); } else if (is_ftp()) { open_device(dcr, omode); } else { Dmsg1(100, "call open_file_device mode=%s\n", mode_to_str(omode)); open_file_device(dcr, omode); } state |= preserve; /* reset any important state info */ Dmsg2(100, "preserve=0x%x fd=%d\n", preserve, m_fd); Dmsg7(100, "open dev: fd=%d dev=%p dcr=%p vol=%s type=%d dev_name=%s mode=%s\n", m_fd, getVolCatName(), this, dcr, dev_type, print_name(), mode_to_str(omode)); return m_fd >= 0; }
/* * Open a fifo device */ void win32_fifo_device::open_device(DCR *dcr, int omode) { file_size = 0; int timeout = max_open_wait; utime_t start_time = time(NULL); mount(dcr, 1); /* do mount if required */ Dmsg0(100, "Open dev: device is fifo\n"); get_autochanger_loaded_slot(dcr); open_mode = omode; set_mode(omode); if (timeout < 1) { timeout = 1; } errno = 0; if (timeout) { /* * Set open timer */ tid = start_thread_timer(dcr->jcr, pthread_self(), timeout); } Dmsg2(100, "Try open %s mode=%s\n", prt_name, mode_to_str(omode)); /* * If busy retry each second for max_open_wait seconds */ for ( ;; ) { /* * Try non-blocking open */ m_fd = d_open(dev_name, oflags | O_NONBLOCK, 0); if (m_fd < 0) { berrno be; dev_errno = errno; Dmsg5(100, "Open error on %s omode=%d oflags=%x errno=%d: ERR=%s\n", prt_name, omode, oflags, errno, be.bstrerror()); } else { d_close(m_fd); m_fd = d_open(dev_name, oflags, 0); /* open normally */ if (m_fd < 0) { berrno be; dev_errno = errno; Dmsg5(100, "Open error on %s omode=%d oflags=%x errno=%d: ERR=%s\n", prt_name, omode, oflags, errno, be.bstrerror()); break; } dev_errno = 0; lock_door(); break; /* Successfully opened and rewound */ } bmicrosleep(5, 0); /* * Exceed wait time ? */ if (time(NULL) - start_time >= max_open_wait) { break; /* yes, get out */ } } if (!is_open()) { berrno be; Mmsg2(errmsg, _("Unable to open device %s: ERR=%s\n"), prt_name, be.bstrerror(dev_errno)); Dmsg1(100, "%s", errmsg); } /* * Stop any open() timer we started */ if (tid) { stop_thread_timer(tid); tid = 0; } Dmsg1(100, "open dev: fifo %d opened\n", m_fd); }
/* * Close the device. */ bool DEVICE::close(DCR *dcr) { bool retval = true; int status; Dmsg1(100, "close_dev %s\n", print_name()); if (!is_open()) { Dmsg2(100, "device %s already closed vol=%s\n", print_name(), VolHdr.VolumeName); goto bail_out; /* already closed */ } if (!norewindonclose) { offline_or_rewind(); } switch (dev_type) { case B_VTL_DEV: case B_TAPE_DEV: unlock_door(); /* * Fall through wanted */ default: status = d_close(m_fd); if (status < 0) { berrno be; Mmsg2(errmsg, _("Unable to close device %s. ERR=%s\n"), print_name(), be.bstrerror()); dev_errno = errno; retval = false; } break; } unmount(dcr, 1); /* do unmount if required */ /* * Clean up device packet so it can be reused. */ clear_opened(); clear_bit(ST_LABEL, state); clear_bit(ST_READREADY, state); clear_bit(ST_APPENDREADY, state); clear_bit(ST_EOT, state); clear_bit(ST_WEOT, state); clear_bit(ST_EOF, state); clear_bit(ST_MOUNTED, state); clear_bit(ST_MEDIA, state); clear_bit(ST_SHORT, state); label_type = B_BAREOS_LABEL; file = block_num = 0; file_size = 0; file_addr = 0; EndFile = EndBlock = 0; open_mode = 0; clear_volhdr(); memset(&VolCatInfo, 0, sizeof(VolCatInfo)); if (tid) { stop_thread_timer(tid); tid = 0; } /* * We closed the device so let any plugin know we did. */ if (dcr) { generate_plugin_event(dcr->jcr, bsdEventDeviceClose, dcr); } bail_out: return retval; }
/* * Open a tape device */ void generic_tape_device::open_device(DCR *dcr, int omode) { file_size = 0; int timeout = max_open_wait; #if !defined(HAVE_WIN32) struct mtop mt_com; utime_t start_time = time(NULL); #endif mount(dcr, 1); /* do mount if required */ Dmsg0(100, "Open dev: device is tape\n"); get_autochanger_loaded_slot(dcr); open_mode = omode; set_mode(omode); if (timeout < 1) { timeout = 1; } errno = 0; Dmsg2(100, "Try open %s mode=%s\n", prt_name, mode_to_str(omode)); #if defined(HAVE_WIN32) /* * Windows Code */ if ((m_fd = d_open(dev_name, oflags, 0)) < 0) { dev_errno = errno; } #else /* * UNIX Code * * If busy retry each second for max_open_wait seconds */ for ( ;; ) { /* * Try non-blocking open */ m_fd = d_open(dev_name, oflags | O_NONBLOCK, 0); if (m_fd < 0) { berrno be; dev_errno = errno; Dmsg5(100, "Open error on %s omode=%d oflags=%x errno=%d: ERR=%s\n", prt_name, omode, oflags, errno, be.bstrerror()); } else { /* * Tape open, now rewind it */ Dmsg0(100, "Rewind after open\n"); mt_com.mt_op = MTREW; mt_com.mt_count = 1; /* * Rewind only if dev is a tape */ if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) { berrno be; dev_errno = errno; /* set error status from rewind */ d_close(m_fd); clear_opened(); Dmsg2(100, "Rewind error on %s close: ERR=%s\n", prt_name, be.bstrerror(dev_errno)); /* * If we get busy, device is probably rewinding, try again */ if (dev_errno != EBUSY) { break; /* error -- no medium */ } } else { /* * Got fd and rewind worked, so we must have medium in drive */ d_close(m_fd); m_fd = d_open(dev_name, oflags, 0); /* open normally */ if (m_fd < 0) { berrno be; dev_errno = errno; Dmsg5(100, "Open error on %s omode=%d oflags=%x errno=%d: ERR=%s\n", prt_name, omode, oflags, errno, be.bstrerror()); break; } dev_errno = 0; lock_door(); set_os_device_parameters(dcr); /* do system dependent stuff */ break; /* Successfully opened and rewound */ } } bmicrosleep(5, 0); /* * Exceed wait time ? */ if (time(NULL) - start_time >= max_open_wait) { break; /* yes, get out */ } } #endif if (!is_open()) { berrno be; Mmsg2(errmsg, _("Unable to open device %s: ERR=%s\n"), prt_name, be.bstrerror(dev_errno)); Dmsg1(100, "%s", errmsg); } Dmsg1(100, "open dev: tape %d opened\n", m_fd); }
/* * Rewind the device. * * Returns: true on success * false on failure */ bool generic_tape_device::rewind(DCR *dcr) { struct mtop mt_com; unsigned int i; bool first = true; Dmsg3(400, "rewind res=%d fd=%d %s\n", num_reserved(), m_fd, prt_name); state &= ~(ST_EOT | ST_EOF | ST_WEOT); /* Remove EOF/EOT flags */ block_num = file = 0; file_size = 0; file_addr = 0; if (m_fd < 0) { return false; } mt_com.mt_op = MTREW; mt_com.mt_count = 1; /* * If we get an I/O error on rewind, it is probably because * the drive is actually busy. We loop for (about 5 minutes) * retrying every 5 seconds. */ for (i = max_rewind_wait; ; i -= 5) { if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) { berrno be; clrerror(mt_com.mt_op); if (i == max_rewind_wait) { Dmsg1(200, "Rewind error, %s. retrying ...\n", be.bstrerror()); } /* * This is a gross hack, because if the user has the * device mounted (i.e. open), then uses mtx to load * a tape, the current open file descriptor is invalid. * So, we close the drive and re-open it. */ if (first && dcr) { int oo_mode = open_mode; d_close(m_fd); clear_opened(); open(dcr, oo_mode); if (m_fd < 0) { return false; } first = false; continue; } #ifdef HAVE_SUN_OS if (dev_errno == EIO) { Mmsg1(errmsg, _("No tape loaded or drive offline on %s.\n"), prt_name); return false; } #else if (dev_errno == EIO && i > 0) { Dmsg0(200, "Sleeping 5 seconds.\n"); bmicrosleep(5, 0); continue; } #endif Mmsg2(errmsg, _("Rewind error on %s. ERR=%s.\n"), prt_name, be.bstrerror()); return false; } break; } return true; }
/* 1. CREATE or find database dictionary (.dbd file). * 2. CREATE empty 'dtsearch' database files. * 3. OPEN 'dtsearch' database. * 4. INITIALIZE the database. * 5. WRITE dbrec after initializing it. * 6. RENAME each database file. * 7. UNLINK (delete) d9x files. */ int main (int argc, char *argv[]) { int i; char *ptr; FILE *f; struct or_miscrec miscrec; struct or_swordrec swordrec; struct or_lwordrec lwordrec; setlocale (LC_ALL, ""); dtsearch_catd = catopen (FNAME_DTSRCAT, 0); aa_argv0 = argv[0]; max_ormisc_size = sizeof (miscrec.or_misc); maxwidth_sword = sizeof (swordrec.or_swordkey) - 1; maxwidth_lword = sizeof (lwordrec.or_lwordkey) - 1; printf (catgets (dtsearch_catd, MS_misc, 4, "%s Version %s.\n"), aa_argv0, DtSrVERSION ); /* Handle cmd line args. Init global variables. */ user_args_processor (argc, argv); /* ------- copy model .dbd to new .dbd ------- */ /* CASE 1: If user specified -d special alternative * directory for model .dbd, it should be there. */ if (modelpath[0] != 0) { if (debug_mode) printf (PROGNAME"628 Try opening '%s' (-d dir).\n", modelpath); if ((f = fopen (modelpath, "rb")) != NULL) { if (debug_mode) puts (PROGNAME"638 Found it!"); create_new_dbd (f); fclose (f); goto DBD_OKAY; } else { print_usage(); printf (catgets (dtsearch_catd, MS_initausd, 213, default_unable_to_open_msg), "\n"PROGNAME"302", modelpath, strerror(errno)); DtSearchExit (4); } } /* end CASE 1 */ /* CASE 2: If model .dbd is in current directory, use it. * If error is anything other than 'cant find file', quit now. */ if (debug_mode) printf (PROGNAME"649 Try opening '%s' (curr dir).\n", FNAME_MODEL); if ((f = fopen (FNAME_MODEL, "rb")) != NULL) { if (debug_mode) puts (PROGNAME"660 Found it!"); create_new_dbd (f); fclose (f); goto DBD_OKAY; } else if (errno != ENOENT) { print_usage(); printf (catgets (dtsearch_catd, MS_initausd, 213, default_unable_to_open_msg), "\n"PROGNAME"655", FNAME_MODEL, strerror(errno)); DtSearchExit (4); } /* end else CASE 2 */ /* CASE 3: Last chance. Look for model .dbd in target directory. * At this point have to quit on any error. */ strcpy (modelpath, newpath); strcpy (modelpath + path_offset, FNAME_MODEL); if (debug_mode) printf (PROGNAME"672 Try opening '%s' (new dir).\n", modelpath); if ((f = fopen (modelpath, "rb")) != NULL) { if (debug_mode) puts (PROGNAME"675 Found it!"); create_new_dbd (f); fclose (f); goto DBD_OKAY; } if (debug_mode) puts (PROGNAME"682 Never found it!"); print_usage(); printf (catgets (dtsearch_catd, MS_initausd, 213, default_unable_to_open_msg), "\n"PROGNAME"686", FNAME_MODEL, "Not found in either current or target directories. Use -d option\a"); DtSearchExit (4); DBD_OKAY: /* Open a new database */ *newextp = 0; /* use no extension when opening database */ if (debug_mode) printf ("040*** d_open newpath = '%s'.\n", newpath); d_open (newpath, "o"); if (db_status != S_OKAY) { printf (catgets (dtsearch_catd, MS_initausd, 230, PROGNAME "230 Could not open database '%s'.\n"), newpath); puts (vista_msg (PROGNAME "231")); DtSearchExit (3); } austext_exit_dbms = (void (*) (int)) d_close; /* emerg exit func */ /* initialize the 'dtsearch' database */ if (debug_mode) printf ("042*** d_initialize.\n"); d_initialize (0); if (db_status != S_OKAY) { printf (catgets (dtsearch_catd, MS_initausd, 239, PROGNAME "239 Could not initialize database '%s'.\n"), newpath); puts (vista_msg (PROGNAME "240")); DtSearchExit (3); } /* Create and initialize dbrec database header record in first slot. * First fill entire record with binary zeros. * Then set specific values as specified by flavor on command line. * For now most values are hard-coded. */ if (debug_mode) printf ("050*** create dbrec.\n"); memset (&dbrec, 0, sizeof (dbrec)); /* Init fields that are completely independent */ dbrec.or_language = (DtSrINT16) language; dbrec.or_maxwordsz = (DtSrINT16) maxwordsz; dbrec.or_minwordsz = (DtSrINT16) minwordsz; dbrec.or_fzkeysz = (DtSrINT16) fzkeysz; dbrec.or_abstrsz = (DtSrINT16) abstrsz; dbrec.or_dbflags = ORD_NONOTES | ORD_NOMARKDEL | ORD_XWORDS; strncpy (dbrec.or_version, SCHEMA_VERSION, sizeof(dbrec.or_version)); dbrec.or_version [sizeof(dbrec.or_version) - 1] = 0; /* Load dbrec's recslots fields based on correct number * of misc recs required to hold user's abstract. * Round abstrsz upward if there is any space left on last misc rec. */ dbrec.or_recslots = 1; /* start with obj rec itself */ for (i = dbrec.or_fzkeysz + dbrec.or_abstrsz; i > 0; i -= max_ormisc_size) dbrec.or_recslots++; if (i < 0) { /* Add in difference to INCREASE abstrsz */ dbrec.or_abstrsz -= i; printf (catgets (dtsearch_catd, MS_misc, 433, "%1$sAdjusted maximum abstract size upward to %2$hd.\n"), PROGNAME "433 ", dbrec.or_abstrsz); } /* Init fields that are dependent on language */ switch (language) { case DtSrLaENG: case DtSrLaENG2: dbrec.or_dbflags |= ORD_XSTEMS; break; default: break; } /* Init fields that are dependent on flavor */ if (flavor == AUSTEXT_FLAVOR) { dbrec.or_dbaccess = ORA_BLOB; dbrec.or_compflags = ORC_COMPBLOB; dbrec.or_hufid = -1L; /* -1 = use huffman compression, but * hufid not yet known. */ dbrec.or_dbotype = DtSrObjTEXT; } else { /* default flavor == DTSEARCH_FLAVOR */ dbrec.or_dbaccess = ORA_NOTAVAIL; } if (!quiet_mode) { /******putchar ('\n');******/ print_dbrec (newpath, &dbrec); fflush (stdout); } swab_dbrec (&dbrec, HTON); if (debug_mode) printf ("060*** fillnew dbrec.\n"); d_fillnew (OR_DBREC, &dbrec, 0); if (db_status != S_OKAY) { printf (catgets (dtsearch_catd, MS_initausd, 509, PROGNAME "509 Could not initialize database header record.\n")); puts (vista_msg (PROGNAME "510")); DtSearchExit (3); } /* Close the database */ d_close (); austext_exit_dbms = NULL; /* emerg exit no longer required */ /* Delete all nonvista (inverted index) database files (.d9x) */ remove_d9x_file (".d97"); remove_d9x_file (".d98"); remove_d9x_file (".d99"); *newextp = 0; /* no extension suffixes for next msgs */ printf (catgets (dtsearch_catd, MS_initausd, 24, PROGNAME " Successfully initialized database '%s'.\n"), newpath); return 0; } /* main() */