Boost:copy_file 失败,访问被拒绝,但没有权限问题

发布于 2024-10-02 17:36:23 字数 1389 浏览 2 评论 0原文

我编写了以下例程,以便将目录中的所有文件复制到子目录中 然后删除它们,但我一直在 copy_fail 上拒绝访问,这看起来误导了我。刚刚创建的目标目录中路径正确,文件存在且权限不是只读的。

有什么建议如何寻找问题的根源吗?

我尝试调试,但没有 boost::filesystem 源代码。

任何建议表示赞赏。

void
moveConfigurationFileToSubDirectory()
{
 // TODO: Catch errors.

 boost::filesystem::path full_path( boost::filesystem::current_path() );

 // Create directory subdir if not exist
 boost::filesystem::path subdirPath(kSubdirectory);
    if ( !boost::filesystem::exists(subdirPath) )
 {
  PLog::DEV.Development(devVerbose, "%s: creating directory %s", __FUNCTION__, subdirPath.string());
  boost::filesystem::create_directories(subdirPath);
 } else
  PLog::DEV.Development(devVerbose, "%s: directory %s exist", __FUNCTION__, subdirPath.string());

 // Iterate through the configuration files defined in the static array
 // copy all files with overwrite flag, if successfully delete file (looks like there is not remove)
 for (int i = 0; i < kNumberOfConfigurationFiles; i++)
 {
  boost::filesystem::path currentConfigurationFile(kConfigurationFiles[i]);

  try
  {
   boost::filesystem::copy_file(currentConfigurationFile, subdirPath, boost::filesystem::copy_option::overwrite_if_exists);
   boost::filesystem::remove(currentConfigurationFile);
  }
  catch (exception& e)
  {
   PLog::DEV.Development(devError, "%s: exception - %s", __FUNCTION__, e.what());
  }
 }
}

I wrote the following routine in order to copy all files in a directory to a subdirectory
and then remove them, but I keep getting an access denied on the copy_fail which looks misleading to me. Paths are corrects, files exist and permission are not read-only in the destination directory just created.

Any suggestion how to hunt the source of the problem?

I tried to debug, but I don't have the boost::filesystem source code.

Any suggestion is appreciated.

void
moveConfigurationFileToSubDirectory()
{
 // TODO: Catch errors.

 boost::filesystem::path full_path( boost::filesystem::current_path() );

 // Create directory subdir if not exist
 boost::filesystem::path subdirPath(kSubdirectory);
    if ( !boost::filesystem::exists(subdirPath) )
 {
  PLog::DEV.Development(devVerbose, "%s: creating directory %s", __FUNCTION__, subdirPath.string());
  boost::filesystem::create_directories(subdirPath);
 } else
  PLog::DEV.Development(devVerbose, "%s: directory %s exist", __FUNCTION__, subdirPath.string());

 // Iterate through the configuration files defined in the static array
 // copy all files with overwrite flag, if successfully delete file (looks like there is not remove)
 for (int i = 0; i < kNumberOfConfigurationFiles; i++)
 {
  boost::filesystem::path currentConfigurationFile(kConfigurationFiles[i]);

  try
  {
   boost::filesystem::copy_file(currentConfigurationFile, subdirPath, boost::filesystem::copy_option::overwrite_if_exists);
   boost::filesystem::remove(currentConfigurationFile);
  }
  catch (exception& e)
  {
   PLog::DEV.Development(devError, "%s: exception - %s", __FUNCTION__, e.what());
  }
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私藏温柔 2024-10-09 17:36:23

您必须为 subdirPath 指定所需的文件名,而不仅仅是路径。 boost 的 copy_file 不够聪明,无法知道通过指定目录名,您希望文件与源具有相同的名称。

You have to specify the filename you want for subdirPath and not just the path. boost's copy_file isn't smart enough to know that by specifying a directory name, you want the file to have the same name as the source.

过潦 2024-10-09 17:36:23

在什么操作系统上运行?如果在 Linux/Unix 上,那么您是否考虑过保存源文件的目录的权限(您正在删除 currentConfigurationFile,这意味着保存该文件的目录必须具有写入权限)?

What OS are running this on? If on Linux/Unix then have you considered permissions on directory holding your source files (you are removing currentConfigurationFile, this means that directory holding that file must have write permission)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文