简体   繁体   English

如何确保boost :: filesystem :: remove不会尝试删除另一个进程使用的文件?

[英]How to be sure that boost::filesystem::remove does not try to delete a file that is used by another process?

I want to upload a file to AWS S3 bucket. 我想将文件上传到AWS S3存储桶。 Before doing this I create a .gz file from it to reduce its storage. 在执行此操作之前,我从中创建一个.gz文件以减少其存储。 After uploading it I want to delete the .gz file again with boost::filesystem::remove. 上传后,我想使用boost :: filesystem :: remove再次删除.gz文件。

It seems like that the upload is blocking the file it is uploading and I wasn't able to find a way to wait for the complete result so the file wouldn't be locked anymore. 似乎上传阻止了它正在上传的文件,我无法找到一种方法来等待完整的结果,因此该文件将不再被锁定。

The removing works with a different call in the main(). 删除工作与main()中的另一个调用一起使用。 Directly after the uploading call it does not work and boost operations.hpp throws an exception. 在上载调用之后,它立即不起作用并进行增强操作。hpp引发异常。

The exception says that the file is already used by another process. 异常表明该文件已被另一个进程使用。

int main(int argc, char *argv[])
{
boost::filesystem::path path{ C:/Development/test.gz };

//deletes the file correctly if called
//boost::filesystem::remove(path); 

//upload file first, then delete it
putObject(path)
}

void putObject(path, s3client)
{
auto input_data = Aws::MakeShared<Aws::FStream>("", path.string(),
std::ios_base::in | std::ios_base::binary);

auto request = Aws::S3::Model::PutObjectRequest();
request.WithBucket("bucketName").WithKey(key);
request.SetBody(input_data);
request.SetMetadata(metadata);

    //upload file to s3 bucket
s3client->PutObject(request);

//throws exception if called - file is in use
boost::filesystem::remove(path);

}

boost operations.hpp that throws the exception (line 664): 引发异常的boostoperations.hpp(第664行):

inline
// For standardization, if the committee doesn't like "remove", consider  
"eliminate"
bool remove(const path& p)           {return detail::remove(p);}

Is there a way to be sure that the file will be deleted if it isn't blocked anymore? 有没有一种方法可以确保如果文件不再被阻止,该文件将被删除?

When you try to delete the file, your Aws::FStream is still open. 当您尝试删除文件时,您的Aws :: FStream仍处于打开状态。 So you just have to close it before you try to delete it. 因此,您只需要将其关闭,然后再尝试将其删除。

I think you can simply call 我想你可以简单地打电话

input_data->close();

before deleting the file. 删除文件之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM