{"id":446,"date":"2005-09-28T05:02:16","date_gmt":"2005-09-28T05:02:16","guid":{"rendered":"https:\/\/www.docbug.com\/blog\/archives\/446"},"modified":"2005-09-28T05:02:16","modified_gmt":"2005-09-28T05:02:16","slug":"osx-mv-and-file-renameto-strangeness","status":"publish","type":"post","link":"https:\/\/www.docbug.com\/blog\/archives\/446","title":{"rendered":"OSX mv and File.renameTo() strangeness"},"content":{"rendered":"<p>I&#8217;ve come across an annoying behavior in OSX which I&#8217;m documenting here mostly in the hopes that anyone else struggling to track down a similar bug will find this post in Google. (This&#8217;ll probably be quite dull to non-Unix geeks&#8230;)<\/p>\n<p>My original symptom:<\/p>\n<p>Java&#8217;s <code>File.renameTo<\/code> command won&#8217;t work when moving files from <code>\/tmp<\/code> to a user directory encrypted with FileVault.<\/p>\n<p>The actual cause (near as I can tell):<\/p>\n<ul>\n<li>\n<p>In Darwin\/OSX (and in BSD), when a file is copied or created in a new directory it automatically takes on the GID (Group ID) of the target directory.<\/p>\n<li>\n<p>A file that is renamed (using the <code>mv<\/code> command or Java&#8217;s <code>File.renameTo<\/code>) should not change its GID, even if the target directory&#8217;s GID is different.<\/p>\n<\/li>\n<li>\n<p>The <code>\/tmp<\/code> directory is set with the group &#8220;wheel,&#8221; which before OSX 10.2 users with admin privileges were in but that&#8217;s no longer the case. This means normal users may not change a file to the group &#8220;wheel&#8221; without invoking admin privileges.<\/p>\n<\/li>\n<\/ul>\n<p>So here&#8217;s what was happening. First I created a new file in <code>\/tmp<\/code>. The group ID on the file was automatically set to &#8220;wheel&#8221; on creation because that&#8217;s the GID for <code>\/tmp<\/code>. Moving the file to another directory on the same disk works just fine because under the hood the OS is just swapping around pointers on the disk. However, when I tried to move the file to a directory on a different virtual disk (which is how OSX thinks of FileVault), it first copies the data and then tries to change the group ID of the newly created file to &#8220;wheel,&#8221; which it doesn&#8217;t have permission to do. If I use <code>mv<\/code> to do the move I get an error message but otherwise the file is moved correctly (albeit with my own group ID instead of <i>wheel<\/i>). If I use the Java routine <code>File.renameTo(destination)<\/code> it simply returns <i>false<\/i> (failure) and refuses to do the move \u2014 I suspect it realizes it can&#8217;t do it perfectly so it doesn&#8217;t even try.<\/p>\n<p>You can get the same effect just moving a file from <code>\/tmp<\/code> to an external firewire drive. In the snippit below, the directory <code>~bug\/<\/code> is on the same local disk as <code>\/tmp<\/code> and <code>\/Volumes\/disk2\/<\/code> is a mounted firewire disk:<\/p>\n<p>  <code> <\/p>\n<p>$ <b>ls -ld \/tmp\/<\/b><br \/> drwxrwxrwt   19 root  wheel  646 Sep 27 20:54 \/tmp\/<\/p>\n<p>$ <b>groups<\/b><br \/> bug appserveradm appserverusr admin<\/p>\n<p>$ <b>touch \/tmp\/test1 \/tmp\/test2<\/b><\/p>\n<p>$ <b>ls -l \/tmp\/test*<\/b><br \/> -rw-r--r--   1 bug  wheel  0 Sep 27 20:54 \/tmp\/test1<br \/> -rw-r--r--   1 bug  wheel  0 Sep 27 20:54 \/tmp\/test2<\/p>\n<p>$ <b>mv \/tmp\/test1 ~bug\/<\/b><\/p>\n<p>$ <b>ls -l ~bug\/test1<\/b><br \/> -rw-r--r--   1 bug  wheel  0 Sep 27 20:54 test1<\/p>\n<p>$ <b>mv \/tmp\/test2 \/Volumes\/disk2\/<\/b><br \/> mv: \/Volumes\/Blackjack\/test2: set owner\/group (was: 502\/0): Operation not permitted<\/p>\n<p>$ <b>ls -l \/Volumes\/disk2\/test2<\/b><br \/> -rw-r--r--   1 bug  bug  0 Sep 27 20:54 \/Volumes\/disk2\/test2<br \/> <\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve come across an annoying behavior in OSX which I&#8217;m documenting here mostly in the hopes that anyone else struggling to track down a similar bug will find this post in Google. (This&#8217;ll probably be quite dull to non-Unix geeks&#8230;)<\/p>\n<p>My original symptom:<\/p>\n<p>Java&#8217;s <code>File.renameTo<\/code> command won&#8217;t work when moving files from <code>\/tmp<\/code> to a user directory encrypted with FileVault.<\/p>\n<p>The actual cause (near as I can tell):<\/p>\n<ul>\n<li>\n<p>In Darwin\/OSX (and in BSD), when a file is copied or created in a new directory it automatically takes on the GID (Group ID) of the target directory.<\/p>\n<li>\n<p>A file that is renamed (using the <code>mv<\/code> command or Java&#8217;s <code>File.renameTo<\/code>) should not change its GID, even if the target directory&#8217;s GID is different.<\/p>\n<\/li>\n<li>\n<p>The <code>\/tmp<\/code> directory is set with the group &#8220;wheel,&#8221; which before OSX 10.2 users with admin privileges were in but that&#8217;s no longer the case. This means normal users may not change a file to the group &#8220;wheel&#8221; without invoking admin privileges.<\/p>\n<\/li>\n<\/ul>\n<p>So here&#8217;s what was happening. First I created a new file in <code>\/tmp<\/code>. The group ID on the file was automatically set to &#8220;wheel&#8221; on creation because that&#8217;s the GID for <code>\/tmp<\/code>. Moving the file to another directory on the same disk works just fine because under the hood the OS is just swapping around pointers on the disk. However, when I tried to move the file to a directory on a different virtual disk (which is how OSX thinks of FileVault), it first copies the data and then tries to change the group ID of the newly created file to &#8220;wheel,&#8221; which it doesn&#8217;t have permission to do. If I use <code>mv<\/code> to do the move I get an error message but otherwise the file is moved correctly (albeit with my own group ID instead of <i>wheel<\/i>). If I use the Java routine <code>File.renameTo(destination)<\/code> it simply returns <i>false<\/i> (failure) and refuses to do the move \u2014 I suspect it realizes it can&#8217;t do it perfectly so it doesn&#8217;t even try.<\/p>\n<p>You can get the same effect just moving a file from <code>\/tmp<\/code> to an external firewire drive. In the snippit below, the directory <code>~bug\/<\/code> is on the same local disk as <code>\/tmp<\/code> and <code>\/Volumes\/disk2\/<\/code> is a mounted firewire disk:<\/p>\n<p>  <code> <\/p>\n<p>$ <b>ls -ld \/tmp\/<\/b><br \/> drwxrwxrwt   19 root  wheel  646 Sep 27 20:54 \/tmp\/<\/p>\n<p>$ <b>groups<\/b><br \/> bug appserveradm appserverusr admin<\/p>\n<p>$ <b>touch \/tmp\/test1 \/tmp\/test2<\/b><\/p>\n<p>$ <b>ls -l \/tmp\/test*<\/b><br \/> -rw-r--r--   1 bug  wheel  0 Sep 27 20:54 \/tmp\/test1<br \/> -rw-r--r--   1 bug  wheel  0 Sep 27 20:54 \/tmp\/test2<\/p>\n<p>$ <b>mv \/tmp\/test1 ~bug\/<\/b><\/p>\n<p>$ <b>ls -l ~bug\/test1<\/b><br \/> -rw-r--r--   1 bug  wheel  0 Sep 27 20:54 test1<\/p>\n<p>$ <b>mv \/tmp\/test2 \/Volumes\/disk2\/<\/b><br \/> mv: \/Volumes\/Blackjack\/test2: set owner\/group (was: 502\/0): Operation not permitted<\/p>\n<p>$ <b>ls -l \/Volumes\/disk2\/test2<\/b><br \/> -rw-r--r--   1 bug  bug  0 Sep 27 20:54 \/Volumes\/disk2\/test2<br \/> <\/code><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[],"tags":[],"class_list":["post-446","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.docbug.com\/blog\/wp-json\/wp\/v2\/posts\/446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.docbug.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.docbug.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.docbug.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.docbug.com\/blog\/wp-json\/wp\/v2\/comments?post=446"}],"version-history":[{"count":0,"href":"https:\/\/www.docbug.com\/blog\/wp-json\/wp\/v2\/posts\/446\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.docbug.com\/blog\/wp-json\/wp\/v2\/media?parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.docbug.com\/blog\/wp-json\/wp\/v2\/categories?post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.docbug.com\/blog\/wp-json\/wp\/v2\/tags?post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}