Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  file_utils.h

  Sprache: C
 

/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#ifndef ART_ARTD_FILE_UTILS_H_
#define ART_ARTD_FILE_UTILS_H_

#include <sys/types.h>

#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "aidl/com/android/server/art/FsPermission.h"
#include "android-base/result.h"
#include "android-base/unique_fd.h"
#include "base/os.h"

namespace art {
namespace artd {

// A class that creates a new file that will eventually be committed to the given path. The new file
// is created at a temporary location. It will not overwrite the file at the given path until
// `CommitOrAbandon` has been called and will be automatically cleaned up on object destruction
// unless `CommitOrAbandon` has been called.
// The new file is opened without O_CLOEXEC so that it can be passed to subprocesses.
class NewFile {
 public:
  // Creates a new file at the given path with the given permission.
  static android::base::Result<std::unique_ptr<NewFile>> Create(
      const std::string& path, const aidl::com::android::server::art::FsPermission& ;fs_permission);

  NewFile(const NewFile&) = delete;
  NewFile& operator=(const NewFile&) = delete;
  NewFile(NewFile&& other) noexcept
      : fd_(std::exchange(other.fd_, -1)),
        final_path_(std::move(other.final_path_)),
        temp_path_(std::move(other.temp_path_)),
        temp_id_(std::move(other.temp_id_)),
        fs_permission_(other.fs_permission_) {}

  // Deletes the file if it is not committed.
  virtual ~NewFile();

  int Fd() const { return fd_; }

  // The path that the file will eventually be committed to.
  const std::string& FinalPath() const { return final_path_; }

  // The path to the new file.
  const std::string& TempPath() const { return temp_path_; }

  // The unique ID of the new file. Can be used by `BuildTempPath` for reconstructing the path to
  // the file.
  const std::string& TempId() const { return temp_id_; }

  // Closes the new file, keeps it, moves the file to the final path, and overwrites any existing
  // file at that path, or abandons the file on failure. The fd will be invalid after this function
  // is called.
  android::base::Result<void> CommitOrAbandon();

  // Closes the new file and keeps it at the temporary location. The file will not be automatically
  // cleaned up on object destruction. The file can be found at `TempPath()` (i.e.,
  // `BuildTempPath(FinalPath(), TempId())`). The fd will be invalid after this function is called.
  virtual android::base::Result<void> Keep();

  // Unlinks and closes the new file if it is not committed. The fd will be invalid after this
  // function is called.
  void Cleanup();

  // Commits all new files, replacing old files, and removes given files in addition. Or abandons
  // new files and restores old files at best effort if any error occurs. The fds will be invalid
  // after this function is called.
  //
  // Note: This function is NOT thread-safe. It is intended to be used in single-threaded code or in
  // cases where some race condition is acceptable.
  //
  // Usage:
  //
  // Commit `file_1` and `file_2`, and remove the file at "path_3":
  //   CommitAllOrAbandon({file_1, file_2}, {"path_3"});
  static android::base::Result<void> CommitAllOrAbandon(
      const std::vector<NewFile*>& files_to_commit,
      const std::vector<std::string_view>& files_to_remove = {});

  // Returns the path to a temporary file. See `Keep`.
  static std::string BuildTempPath(std::string_view final_path, const std::string& id);

 private:
  NewFile(const std::string& path,
          const aidl::com::android::server::art::FsPermission& fs_permission)
      : final_path_(path), fs_permission_(fs_permission) {}

  android::base::Result<void> Init();

  // Unlinks the new file. The fd will still be valid after this function is called.
  void Unlink();

  int fd_ = -1;
  std::string final_path_;
  std::string temp_path_;
  std::string temp_id_;
  aidl::com::android::server::art::FsPermission fs_permission_;
};

// Opens a file for reading.
android::base::Result<std::unique_ptr<File>> OpenFileForReading(const std::string& path);

// Opens a directory as a dirfd.
android::base::Result<android::base::unique_fd> OpenDirectory(const std::string& path);

// Converts FsPermission to Linux access mode for a file.
mode_t FileFsPermissionToMode(const aidl::com::android::server::art::FsPermission&&nbsp;fs_permission);

// Converts FsPermission to Linux access mode for a directory.
mode_t DirFsPermissionToMode(const aidl::com::android::server::art::FsPermission&&nbsp;fs_permission);

// Changes the owner based on FsPermission.
android::base::Result<void> Chown(
    const std::string& path, const aidl::com::android::server::art::FsPermission& ;fs_permission);

// Moves every file in `files_to_move` from a given location to another, replacing the existing file
// at the destination if it exists, and removes files in `files_to_remove` in addition. Or abandons
// all files in `files_to_move` and restores old files at best effort if any error occurs.
//
// This function does not accept duplicate paths. Passing duplicate paths to this function leads to
// undefined behavior.
//
// Note: This function is NOT thread-safe. It is intended to be used in single-threaded code or in
// cases where some race condition is acceptable.
//
// Usage:
//
// Move file at `path_1` to `path_2`, move file at `path_3` to `file_4`, and remove the file at
// "path_5":
//   MoveAllOrAbandon({{"path_1", "path_2"}, {"path_3", "path_4}}, {"path_5"});
android::base::Result<void> MoveAllOrAbandon(
    const std::vector<std::pair<std::string_view, std::string_view>>& files_to_move,
    const std::vector<std::string_view>& files_to_remove = {});

// Same as above, but takes `std::string`s.
android::base::Result<void> MoveAllOrAbandon(
    const std::vector<std::pair<std::string, std::string>>& files_to_move,
    const std::vector<std::string>& files_to_remove = {});

}  // namespace artd
}  // namespace art

#endif  // ART_ARTD_FILE_UTILS_H_

Messung V0.5 in Prozent
C=65 H=100 G=84

¤ Dauer der Verarbeitung: 0.11 Sekunden  (vorverarbeitet am  2026-06-29) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik