Re: [golang-dev] os.Rename between different partitions

4,400 views
Skip to first unread message

Ian Lance Taylor

unread,
May 3, 2013, 8:07:44 PM5/3/13
to Mateus Braga, golan...@googlegroups.com
On Fri, May 3, 2013 at 4:07 PM, Mateus Braga <mateus....@gmail.com> wrote:
>
> I was coding a program that uses os.Rename(oldFile, newFile) and I get an
> error saying invalid cross-device link when oldFile and newFile are in
> different partitions.
> Is that expected or is it a bug? Why that happens?

That is expected, at least on Unix systems. os.Rename calls the
rename system call, and the rename system call can not rename files
across devices. That's because renaming on a file on a single device
on Unix just requires changing two directory entries. Renaming a file
across devices requires copying the bytes, and as such is potentially
a much longer and more error-prone operation.

Ian

Daniel Theophanes

unread,
May 3, 2013, 8:09:57 PM5/3/13
to golan...@googlegroups.com
You can't "move" a file on different partitions, because you have to copy bytes around. If you want to move or rename a file, you are talking about a file system just pointing to the same bytes, just from a different location. Thus if you get that error you should just copy and delete the file. Might want to ask on golang-nuts before posting here.

On Friday, May 3, 2013 4:07:19 PM UTC-7, Mateus Braga wrote:
Hi everyone,

I was coding a program that uses os.Rename(oldFile, newFile) and I get an error saying invalid cross-device link when oldFile and newFile are in different partitions. 
Is that expected or is it a bug? Why that happens?

Thank you

nitin.co...@gmail.com

unread,
Jan 30, 2018, 8:52:08 AM1/30/18
to golang-dev
Did you find any solution ? I'm having the exact similar issue.
Message has been deleted

yungm...@gmail.com

unread,
Jan 15, 2019, 1:33:58 PM1/15/19
to golang-dev


On Tuesday, January 30, 2018 at 8:52:08 AM UTC-5, nitin.co...@gmail.com wrote:
Did you find any solution ? I'm having the exact similar issue.

Resubmitting that code block, as it was doing a copy not a move under this case as the file was not being removed after the ioWrite was completed. There is a definite issue with this implementation that the file would exist at two locations for the time it's being managed. 
 
 func customRename(prevPath, newPath string, mode os.FileMode) error {
     err := os.Rename(prevPath, newPath)
    if err != nil {
         byteArr, err2 := ioutil.ReadFile(prevPath)
             if err2 != nil {
                       return err2
            }

                err2 = ioutil.WriteFile(newPath, byteArr, mode)
        if err2 == nil {
                       // Remove the file iff it was able to be written
                       _ = os.Remove(prevPath)
        } else {
                       log.Printf("unable to write the file out to the new path. Previous: %s --> New: %s\n", prevPath, newPath)
                      // Remove any partial file data that may have been written in the case of unfulfilled writes.
                  _ = os.Remove(newPath)
         }

                return err2
    }
      return err
}

Bryan C. Mills

unread,
Jan 15, 2019, 1:47:53 PM1/15/19
to yungm...@gmail.com, golang-dev
Folks, golang-dev is for discussion of development of the Go project itself.

Please take usage discussions to a more appropriate venue (see http://golang.org/wiki/Questions).

--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages