Invalid cross-device link error with boost filesystem

爱⌒轻易说出口 提交于 2019-12-10 12:36:49

问题


I am trying to move a file from a location to another using boost::filesystem. I used boost::filesystem::rename function but when I try to do that I have the following error.

terminate called after throwing an instance of 
'boost::filesystem::filesystem_error'
what():  boost::filesystem::rename: Invalid cross-device link: 
"./file_A.csv",    "/opt/data/file_B.csv"
Aborted (core dumped)

I understood that the problem is that I am trying to move a file from one folder into another mounted on a different volume.

Is there any solution different from

  1. COPYING the file and then DELETE it (it gives me some feeling of security).
  2. wrapping mv in a call to std::systen?

Is there any other funciton in boost::filesystem for what I want to achieve? I cannot find it myself.

I am working with g++ and linux.


回答1:


If renaming a file (ultimately through the rename() library call, whether it's wrapped up in boost:: or anything else) fails because the source and destination are on different file systems, the only option is to then copy the file and delete the original after verifying that the copy was complete and successful. This is what /bin/mv does - it first tries a rename(), and if the error code returned by it's failure indicates a cross-device link situation, it falls back to a copy and remove scenario.



来源:https://stackoverflow.com/questions/24209886/invalid-cross-device-link-error-with-boost-filesystem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!