+
+//
+// This function gets the doc_tombstone structure for the
+// current thread. If the thread doesn't have one, the
+// structure is allocated.
+//
+static struct doc_tombstone *
+get_uthread_doc_tombstone(void)
+{
+ struct uthread *ut;
+ ut = get_bsdthread_info(current_thread());
+
+ if (ut->t_tombstone == NULL) {
+ ut->t_tombstone = kalloc(sizeof(struct doc_tombstone));
+ if (ut->t_tombstone) {
+ memset(ut->t_tombstone, 0, sizeof(struct doc_tombstone));
+ }
+ }
+
+ return ut->t_tombstone;
+}
+
+//
+// This routine clears out the current tombstone for the
+// current thread and if necessary passes the doc-id of
+// the tombstone on to the dst_cnode.
+//
+// If the doc-id transfers to dst_cnode, we also generate
+// a doc-id changed fsevent. Unlike all the other fsevents,
+// doc-id changed events can only be generated here in HFS
+// where we have the necessary info.
+//
+static void
+clear_tombstone_docid(struct doc_tombstone *ut, struct hfsmount *hfsmp, struct cnode *dst_cnode)
+{
+ uint32_t old_id = ut->t_lastop_document_id;
+
+ ut->t_lastop_document_id = 0;
+ ut->t_lastop_parent = NULL;
+ ut->t_lastop_parent_vid = 0;
+ ut->t_lastop_filename[0] = '\0';
+
+ //
+ // If the lastop item is still the same and needs to be cleared,
+ // clear it.
+ //
+ if (dst_cnode && old_id && ut->t_lastop_item && vnode_vid(ut->t_lastop_item) == ut->t_lastop_item_vid) {
+ //
+ // clear the document_id from the file that used to have it.
+ // XXXdbg - we need to lock the other vnode and make sure to
+ // update it on disk.
+ //
+ struct cnode *ocp = VTOC(ut->t_lastop_item);
+ struct FndrExtendedFileInfo *ofip = (struct FndrExtendedFileInfo *)((char *)&ocp->c_attr.ca_finderinfo + 16);
+
+ // printf("clearing doc-id from ino %d\n", ocp->c_desc.cd_cnid);
+ ofip->document_id = 0;
+ ocp->c_bsdflags &= ~UF_TRACKED;
+ ocp->c_flag |= C_MODIFIED | C_FORCEUPDATE; // mark it dirty
+ /* cat_update(hfsmp, &ocp->c_desc, &ocp->c_attr, NULL, NULL); */
+
+ }
+
+#if CONFIG_FSE
+ if (dst_cnode && old_id) {
+ struct FndrExtendedFileInfo *fip = (struct FndrExtendedFileInfo *)((char *)&dst_cnode->c_attr.ca_finderinfo + 16);
+
+ add_fsevent(FSE_DOCID_CHANGED, vfs_context_current(),
+ FSE_ARG_DEV, hfsmp->hfs_raw_dev,
+ FSE_ARG_INO, (ino64_t)ut->t_lastop_fileid, // src inode #
+ FSE_ARG_INO, (ino64_t)dst_cnode->c_fileid, // dst inode #
+ FSE_ARG_INT32, (uint32_t)fip->document_id,
+ FSE_ARG_DONE);
+ }
+#endif
+ // last, clear these now that we're all done
+ ut->t_lastop_item = NULL;
+ ut->t_lastop_fileid = 0;
+ ut->t_lastop_item_vid = 0;
+}
+
+
+//
+// This function is used to filter out operations on temp
+// filenames. We have to filter out operations on certain
+// temp filenames to work-around questionable application
+// behavior from apps like Autocad that perform unusual
+// sequences of file system operations for a "safe save".
+static int
+is_ignorable_temp_name(const char *nameptr, int len)
+{
+ if (len == 0) {
+ len = strlen(nameptr);
+ }
+
+ if ( strncmp(nameptr, "atmp", 4) == 0
+ || (len > 4 && strncmp(nameptr+len-4, ".bak", 4) == 0)
+ || (len > 4 && strncmp(nameptr+len-4, ".tmp", 4) == 0)) {
+ return 1;
+ }
+
+ return 0;
+}
+
+//
+// Decide if we need to save a tombstone or not. Normally we always
+// save a tombstone - but if there already is one and the name we're
+// given is an ignorable name, then we will not save a tombstone.
+//
+static int
+should_save_docid_tombstone(struct doc_tombstone *ut, struct vnode *vp, struct componentname *cnp)
+{
+ if (cnp->cn_nameptr == NULL) {
+ return 0;
+ }
+
+ if (ut->t_lastop_document_id && ut->t_lastop_item == vp && is_ignorable_temp_name(cnp->cn_nameptr, cnp->cn_namelen)) {
+ return 0;
+ }
+
+ return 1;
+}
+
+
+//
+// This function saves a tombstone for the given vnode and name. The
+// tombstone represents the parent directory and name where the document
+// used to live and the document-id of that file. This info is recorded
+// in the doc_tombstone structure hanging off the uthread (which assumes
+// that all safe-save operations happen on the same thread).
+//
+// If later on the same parent/name combo comes back into existence then
+// we'll preserve the doc-id from this vnode onto the new vnode.
+//
+static void
+save_tombstone(struct hfsmount *hfsmp, struct vnode *dvp, struct vnode *vp, struct componentname *cnp, int for_unlink)
+{
+ struct cnode *cp = VTOC(vp);
+ struct doc_tombstone *ut;
+ ut = get_uthread_doc_tombstone();
+
+ if (for_unlink && vp->v_type == VREG && cp->c_linkcount > 1) {
+ //
+ // a regular file that is being unlinked and that is also
+ // hardlinked should not clear the UF_TRACKED state or
+ // mess with the tombstone because somewhere else in the
+ // file system the file is still alive.
+ //
+ return;
+ }
+
+ ut->t_lastop_parent = dvp;
+ ut->t_lastop_parent_vid = vnode_vid(dvp);
+ ut->t_lastop_fileid = cp->c_fileid;
+ if (for_unlink) {
+ ut->t_lastop_item = NULL;
+ ut->t_lastop_item_vid = 0;
+ } else {
+ ut->t_lastop_item = vp;
+ ut->t_lastop_item_vid = vnode_vid(vp);
+ }
+
+ strlcpy((char *)&ut->t_lastop_filename[0], cnp->cn_nameptr, sizeof(ut->t_lastop_filename));
+
+ struct FndrExtendedFileInfo *fip = (struct FndrExtendedFileInfo *)((char *)&cp->c_attr.ca_finderinfo + 16);
+ ut->t_lastop_document_id = fip->document_id;
+
+ if (for_unlink) {
+ // clear this so it's never returned again
+ fip->document_id = 0;
+ cp->c_bsdflags &= ~UF_TRACKED;
+
+ if (ut->t_lastop_document_id) {
+ (void) cat_update(hfsmp, &cp->c_desc, &cp->c_attr, NULL, NULL);
+
+#if CONFIG_FSE
+ // this event is more of a "pending-delete"
+ add_fsevent(FSE_DOCID_CHANGED, vfs_context_current(),
+ FSE_ARG_DEV, hfsmp->hfs_raw_dev,
+ FSE_ARG_INO, (ino64_t)cp->c_fileid, // src inode #
+ FSE_ARG_INO, (ino64_t)0, // dst inode #
+ FSE_ARG_INT32, ut->t_lastop_document_id, // document id
+ FSE_ARG_DONE);
+#endif
+ }
+ }
+}
+
+