Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a384fb76 authored by Mårten Kongstad's avatar Mårten Kongstad Committed by Ryan Mitchell
Browse files

idmap2: create in-memory FRRO before creating file

Re-order the serialization of an FRRO and creation of the backing file.
This prevents a dangling (empty) file if the serialization fails.

Bug: 189963636
Test: manual: cmd overlay fabricate <bad parameters>, verify no file created in /data/resource-cache
Change-Id: I1af88c6d2d1c3a881beecfb50ccaf541a249f39b
parent 2db6ff2c
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -262,17 +262,17 @@ Status Idmap2Service::createFabricatedOverlay(
                                    path.c_str(), uid));
  }

  const auto frro = builder.Build();
  if (!frro) {
    return error(StringPrintf("failed to serialize '%s:%s': %s", overlay.packageName.c_str(),
                              overlay.overlayName.c_str(), frro.GetErrorMessage().c_str()));
  }
  // Persist the fabricated overlay.
  umask(kIdmapFilePermissionMask);
  std::ofstream fout(path);
  if (fout.fail()) {
    return error("failed to open frro path " + path);
  }
  const auto frro = builder.Build();
  if (!frro) {
    return error(StringPrintf("failed to serialize '%s:%s': %s", overlay.packageName.c_str(),
                              overlay.overlayName.c_str(), frro.GetErrorMessage().c_str()));
  }
  auto result = frro->ToBinaryStream(fout);
  if (!result) {
    unlink(path.c_str());