Skip to content

Commit cea6428

Browse files
committed
[rfile] Throw exception for .zip files
1 parent 3d5696c commit cea6428

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

io/io/src/RFile.cxx

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ ROOT::RLogChannel &ROOT::Experimental::Internal::RFileLog()
2828
using ROOT::Experimental::RFile;
2929
using ROOT::Experimental::Internal::RFileLog;
3030

31-
static void CheckExtension(std::string_view path)
32-
{
33-
if (ROOT::EndsWith(path, ".xml")) {
34-
throw ROOT::RException(R__FAIL("ROOT::RFile doesn't support XML files."));
35-
}
36-
37-
if (!ROOT::EndsWith(path, ".root")) {
38-
R__LOG_INFO(RFileLog()) << "ROOT::RFile only supports ROOT files. The preferred file extension is \".root\"";
39-
}
40-
}
41-
4231
namespace {
4332
enum class ENameCycleError {
4433
kNoError,
@@ -186,39 +175,42 @@ RFile::~RFile() = default;
186175

187176
std::unique_ptr<RFile> RFile::Open(std::string_view path)
188177
{
189-
CheckExtension(path);
190-
191178
TDirectory::TContext ctx(nullptr); // XXX: probably not thread safe?
192179
auto tfile = std::unique_ptr<TFile>(TFile::Open(std::string(path).c_str(), "READ_WITHOUT_GLOBALREGISTRATION"));
193180
if (!tfile || tfile->IsZombie())
194181
throw ROOT::RException(R__FAIL("failed to open file " + std::string(path) + " for reading"));
195182

183+
if (tfile->IsRaw())
184+
throw ROOT::RException(R__FAIL("Opened file " + std::string(path) + " is not a ROOT file"));
185+
196186
auto rfile = std::unique_ptr<RFile>(new RFile(std::move(tfile)));
197187
return rfile;
198188
}
199189

200190
std::unique_ptr<RFile> RFile::Update(std::string_view path)
201191
{
202-
CheckExtension(path);
203-
204192
TDirectory::TContext ctx(nullptr); // XXX: probably not thread safe?
205193
auto tfile = std::unique_ptr<TFile>(TFile::Open(std::string(path).c_str(), "UPDATE_WITHOUT_GLOBALREGISTRATION"));
206194
if (!tfile || tfile->IsZombie())
207195
throw ROOT::RException(R__FAIL("failed to open file " + std::string(path) + " for updating"));
208196

197+
if (tfile->IsRaw())
198+
throw ROOT::RException(R__FAIL("Opened file " + std::string(path) + " is not a ROOT file"));
199+
209200
auto rfile = std::unique_ptr<RFile>(new RFile(std::move(tfile)));
210201
return rfile;
211202
}
212203

213204
std::unique_ptr<RFile> RFile::Recreate(std::string_view path)
214205
{
215-
CheckExtension(path);
216-
217206
TDirectory::TContext ctx(nullptr); // XXX: probably not thread safe?
218207
auto tfile = std::unique_ptr<TFile>(TFile::Open(std::string(path).c_str(), "RECREATE_WITHOUT_GLOBALREGISTRATION"));
219208
if (!tfile || tfile->IsZombie())
220209
throw ROOT::RException(R__FAIL("failed to open file " + std::string(path) + " for writing"));
221210

211+
if (tfile->IsRaw())
212+
throw ROOT::RException(R__FAIL("Opened file " + std::string(path) + " is not a ROOT file"));
213+
222214
auto rfile = std::unique_ptr<RFile>(new RFile(std::move(tfile)));
223215
return rfile;
224216
}

io/io/test/rfile.cxx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,23 @@ TEST(RFile, PutOverwrite)
266266
TEST(RFile, WrongExtension)
267267
{
268268
ROOT::RLogScopedVerbosity logVerb(ROOT::ELogLevel::kInfo);
269+
// Root files with unconventional extensions are supported.
269270
{
270271
FileRaii fileGuard("test_rfile_wrong.root.1");
271-
ROOT::TestSupport::CheckDiagsRAII diagsRaii;
272-
diagsRaii.requiredDiag(kInfo, "ROOT.File", "preferred file extension is \".root\"", false);
273272
RFile::Recreate(fileGuard.GetPath());
274273
}
274+
275+
// XML files are not supported.
276+
FileRaii fileGuardXml("test_rfile_wrong.xml");
277+
{
278+
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuardXml.GetPath().c_str(), "RECREATE"));
279+
TH1D h("h", "h", 10, 0, 1);
280+
file->WriteObject(&h, "h");
281+
}
275282
{
276-
FileRaii fileGuard("test_rfile_wrong.xml");
277-
ROOT::TestSupport::CheckDiagsRAII diagsRaii;
278-
EXPECT_THROW(RFile::Recreate(fileGuard.GetPath()), ROOT::RException);
283+
EXPECT_THROW(RFile::Open(fileGuardXml.GetPath()), ROOT::RException);
284+
EXPECT_THROW(RFile::Update(fileGuardXml.GetPath()), ROOT::RException);
285+
EXPECT_THROW(RFile::Recreate(fileGuardXml.GetPath()), ROOT::RException);
279286
}
280287
}
281288

0 commit comments

Comments
 (0)