{"id":46999,"date":"2025-03-10T20:38:59","date_gmt":"2025-03-11T00:38:59","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=46999"},"modified":"2025-03-12T09:57:37","modified_gmt":"2025-03-12T13:57:37","slug":"nsfilewrapper-sometimes-changes-modification-date-to-the-past","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2025\/03\/10\/nsfilewrapper-sometimes-changes-modification-date-to-the-past\/","title":{"rendered":"NSFileWrapper Sometimes Changes Modification Date to the Past"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/matteorattotti\/NSFileWrapperDateBug\">Matteo Rattoti<\/a> (via <a href=\"https:\/\/christiantietze.de\/posts\/2025\/03\/nsfilewrapper-sometimes-modification-date-past\/\">Christian Tietze<\/a>):<\/p>\n<blockquote cite=\"https:\/\/github.com\/matteorattotti\/NSFileWrapperDateBug\"><p>This repository demonstrates a bug in <code>NSFileWrapper<\/code> where overwriting a file with the same content using <code>-[NSFileWrapper writeToURL:options:originalContentsURL:error:]<\/code> sometimes causes the file&rsquo;s modification date to be set in the past instead of updating to the current time.<\/p><\/blockquote>\n<p>I think I saw someone speculate that this is due to it not using a nanosecond-capable date\/time API&mdash;which would imply that this only happens on APFS&mdash;but I can&rsquo;t seem to find the post.<\/p>\n\n<p id=\"nsfilewrapper-sometimes-changes-modification-date-to-the-past-update-2025-03-12\">Update (2025-03-12): <a href=\"https:\/\/social.bdash.net.nz\/@mrowe\/114141348445896665\">Mark Rowe<\/a>:<\/p>\n<blockquote cite=\"https:\/\/social.bdash.net.nz\/@mrowe\/114141348445896665\"><p>I mentioned on Slack that <code>struct stat<\/code> returns modification times as a <code>timespec<\/code> which has ns resolution. Foundation sets modification times using <code>utimes<\/code> which represents times as <code>timeval<\/code> which only has &#x3BC;s resolution. Foundation also explicitly <a href=\"https:\/\/github.com\/swiftlang\/swift-foundation\/blob\/main\/Sources\/FoundationEssentials\/FileManager\/FileManager%2BFiles.swift#L928-L937\">rounds the ns portion of the timestamp to &#x3BC;s rather than truncating<\/a>. I&rsquo;m not sure why it doesn&rsquo;t use <code>utimensat<\/code> instead. That takes a <code>timespec<\/code> and so should allow ns resolution when setting the modification time.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Matteo Rattoti (via Christian Tietze): This repository demonstrates a bug in NSFileWrapper where overwriting a file with the same content using -[NSFileWrapper writeToURL:options:originalContentsURL:error:] sometimes causes the file&rsquo;s modification date to be set in the past instead of updating to the current time. I think I saw someone speculate that this is due to it not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2025-03-11T00:39:02Z","apple_news_api_id":"3ef5839c-b7cd-437f-94a8-f7206b9eb135","apple_news_api_modified_at":"2025-03-12T13:57:40Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/APvWDnLfNQ3-UqPcga56xNQ","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[1395,131,69,30,2385,2598,71,364],"class_list":["post-46999","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-apple-file-system-apfs","tag-bug","tag-cocoa","tag-mac","tag-macos-14-sonoma","tag-macos-15-sequoia","tag-programming","tag-time"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/46999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=46999"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/46999\/revisions"}],"predecessor-version":[{"id":47025,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/46999\/revisions\/47025"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=46999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=46999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=46999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}