2016-05-12 23:43:17 +03:00
|
|
|
/*
|
2017-01-21 09:02:33 +03:00
|
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
2016-05-12 23:43:17 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Hash.h"
|
|
|
|
#include "TreeEntry.h"
|
2017-04-14 21:31:48 +03:00
|
|
|
#include "eden/fs/utils/PathFuncs.h"
|
2016-05-12 23:43:17 +03:00
|
|
|
|
|
|
|
#include <folly/String.h>
|
2017-03-17 23:21:33 +03:00
|
|
|
#include <iosfwd>
|
2016-05-12 23:43:17 +03:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
|
|
|
|
class Hash;
|
|
|
|
|
|
|
|
enum class TreeEntryType {
|
|
|
|
BLOB,
|
|
|
|
TREE,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class FileType {
|
|
|
|
// The values for these enum entries correspond to the ones we would expect to
|
|
|
|
// find in <sys/stat.h>. We hardcode them just in case the values in the
|
|
|
|
// users's <sys/stat.h> are different. Note that Git performs a similar check:
|
|
|
|
// https://github.com/git/git/blob/v2.7.4/configure.ac#L912-L917
|
|
|
|
DIRECTORY = 0040,
|
|
|
|
REGULAR_FILE = 0100,
|
|
|
|
SYMLINK = 0120,
|
|
|
|
};
|
|
|
|
|
|
|
|
class TreeEntry {
|
|
|
|
public:
|
|
|
|
explicit TreeEntry(
|
|
|
|
const Hash& hash,
|
|
|
|
folly::StringPiece name,
|
|
|
|
FileType fileType,
|
|
|
|
uint8_t ownerPermissions)
|
|
|
|
: fileType_(fileType),
|
|
|
|
ownerPermissions_(ownerPermissions),
|
|
|
|
hash_(hash),
|
|
|
|
name_(PathComponentPiece(name)) {}
|
|
|
|
|
|
|
|
const Hash& getHash() const {
|
|
|
|
return hash_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PathComponent& getName() const {
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeEntryType getType() const {
|
|
|
|
return fileType_ == FileType::DIRECTORY ? TreeEntryType::TREE
|
|
|
|
: TreeEntryType::BLOB;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileType getFileType() const {
|
|
|
|
return fileType_;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:53:00 +03:00
|
|
|
uint8_t getOwnerPermissions() const {
|
2016-05-12 23:43:17 +03:00
|
|
|
return ownerPermissions_;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:53:00 +03:00
|
|
|
mode_t getMode() const {
|
|
|
|
mode_t mode = static_cast<mode_t>(fileType_) << 9;
|
|
|
|
// We should always honor the explicit owner permissions.
|
|
|
|
mode |= ownerPermissions_ << 6;
|
|
|
|
|
|
|
|
// We propagate the 'r' and 'x' values for the owner to group and other.
|
|
|
|
mode |= (ownerPermissions_ & 0b101) << 3;
|
|
|
|
mode |= (ownerPermissions_ & 0b101);
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2016-12-21 03:23:22 +03:00
|
|
|
/**
|
|
|
|
* Extract the owner permissions from a mode_t value.
|
|
|
|
*
|
|
|
|
* This can be used to construct the ownerPermissions parameter needed for
|
|
|
|
* the TreeEntry constructor.
|
|
|
|
*/
|
|
|
|
static uint8_t modeToOwnerPermissions(mode_t mode) {
|
|
|
|
return (mode >> 6) & 0b111;
|
|
|
|
}
|
|
|
|
|
2017-10-26 02:49:58 +03:00
|
|
|
std::string toLogString() const;
|
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
private:
|
2016-07-07 23:35:39 +03:00
|
|
|
FileType fileType_;
|
|
|
|
uint8_t ownerPermissions_;
|
|
|
|
Hash hash_;
|
|
|
|
PathComponent name_;
|
2016-05-12 23:43:17 +03:00
|
|
|
};
|
2017-03-17 23:21:33 +03:00
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, TreeEntryType type);
|
2017-07-05 21:17:03 +03:00
|
|
|
bool operator==(const TreeEntry& entry1, const TreeEntry& entry2);
|
|
|
|
bool operator!=(const TreeEntry& entry1, const TreeEntry& entry2);
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
}
|