- /* we create and initialize RootInfo if it doesn't exist. */
- strcat(path, ROOTINFO);
- if (ad_open(path, ADFLAGS_HF, O_RDWR, 0666, &db->rootinfo) < 0) {
- cnid_t id;
-
- /* see if we can open it read-only. if it's read-only, we can't
- * add CNIDs. */
- memset(&db->rootinfo, 0, sizeof(db->rootinfo));
- if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0666, &db->rootinfo) == 0) {
- db->flags = CNIDFLAG_ROOTINFO_RO;
- syslog(LOG_INFO, "cnid_open: read-only RootInfo");
- goto mkdir_appledb;
- }
-
- /* create the file */
- memset(&db->rootinfo, 0, sizeof(db->rootinfo));
- if (ad_open(path, ADFLAGS_HF, O_CREAT | O_RDWR, 0666,
- &db->rootinfo) < 0) {
- syslog(LOG_ERR, "cnid_open: ad_open(RootInfo)");
- goto fail_db;
- }
-
- /* lock the RootInfo file. this and cnid_add are the only places
- * that should fiddle with RootInfo. */
- lock.l_start = ad_getentryoff(&db->rootinfo, ADEID_DID);
- lock.l_len = ad_getentrylen(&db->rootinfo, ADEID_DID);
- if (fcntl(ad_hfileno(&db->rootinfo), F_SETLKW, &lock) < 0) {
- syslog(LOG_ERR, "cnid_open: can't establish lock: %m");
- goto fail_adouble;
- }
-
- /* store the beginning CNID */
- id = htonl(CNID_START);
- memcpy(ad_entry(&db->rootinfo, ADEID_DID), &id, sizeof(id));
- ad_flush(&db->rootinfo, ADFLAGS_HF);
-
- /* unlock it */
- lock.l_type = F_UNLCK;
- fcntl(ad_hfileno(&db->rootinfo), F_SETLK, &lock);
- lock.l_type = F_WRLCK;
- }
-