-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[antlir2_userns] library to handle subordinate id ranges and user nam…
…espace mappings Summary: Write an `antlir2_userns` library to make it easy to translate between user ids in and out of namespaces, and to parse `/etc/sub{ug}id` Test Plan: ``` ❯ buck2 test fbcode//antlir/antlir2/antlir2_userns/... Buck UI: https://www.internalfb.com/buck2/45a669c1-758a-4b71-accb-ee8fd5b9d055 Test UI: https://www.internalfb.com/intern/testinfra/testrun/6473924634463602 Network: Up: 41 MiB Down: 344 MiB (reSessionID-77ad53f6-8546-4b15-823e-eee074b91b06) Jobs completed: 66. Time elapsed: 3.2s. Cache hits: 50%. Commands: 2 (cached: 1, remote: 0, local: 1) Tests finished: Pass 28. Fail 0. Fatal 0. Skip 0. Build failure 0 ``` Reviewed By: sergeyfd Differential Revision: D47293866 fbshipit-source-id: ff969a0c116900a47693c20f94adf6977e0f5ae0
- Loading branch information
1 parent
dda6f8c
commit 135215c
Showing
4 changed files
with
717 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
load("//antlir/bzl:build_defs.bzl", "rust_library") | ||
|
||
rust_library( | ||
name = "antlir2_userns", | ||
srcs = glob(["src/**/*.rs"]), | ||
test_deps = [ | ||
"rstest", | ||
], | ||
deps = [ | ||
"anyhow", | ||
"serde", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
use std::fmt::Debug; | ||
use std::fmt::Display; | ||
use std::ops::Add; | ||
use std::ops::Sub; | ||
use std::str::FromStr; | ||
|
||
use serde::Deserialize; | ||
use serde::Serialize; | ||
|
||
pub mod ns; | ||
pub mod subid; | ||
|
||
pub trait Id: | ||
Debug | ||
+ Copy | ||
+ From<u32> | ||
+ Into<u32> | ||
+ PartialEq | ||
+ Eq | ||
+ PartialOrd | ||
+ Ord | ||
+ Add<IdOffset, Output = Self> | ||
+ Sub<IdOffset, Output = Self> | ||
+ FromStr<Err = std::num::ParseIntError> | ||
+ Display | ||
{ | ||
fn as_u32(self) -> u32; | ||
} | ||
|
||
macro_rules! int_type { | ||
($t:ident) => { | ||
#[derive( | ||
Debug, | ||
Copy, | ||
Clone, | ||
PartialEq, | ||
Eq, | ||
PartialOrd, | ||
Ord, | ||
Hash, | ||
Serialize, | ||
Deserialize | ||
)] | ||
#[serde(transparent)] | ||
#[repr(transparent)] | ||
pub struct $t(u32); | ||
|
||
impl From<$t> for u32 { | ||
fn from(t: $t) -> u32 { | ||
t.0 | ||
} | ||
} | ||
|
||
impl From<u32> for $t { | ||
fn from(u: u32) -> Self { | ||
Self(u) | ||
} | ||
} | ||
|
||
impl FromStr for $t { | ||
type Err = std::num::ParseIntError; | ||
|
||
fn from_str(s: &str) -> Result<Self, std::num::ParseIntError> { | ||
s.parse().map(Self) | ||
} | ||
} | ||
|
||
impl Display for $t { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! id_type { | ||
($t:ident) => { | ||
int_type!($t); | ||
|
||
impl Add<IdOffset> for $t { | ||
type Output = $t; | ||
|
||
fn add(self, offset: IdOffset) -> $t { | ||
Self::from(self.0 + offset.0) | ||
} | ||
} | ||
|
||
impl Sub<IdOffset> for $t { | ||
type Output = $t; | ||
|
||
fn sub(self, offset: IdOffset) -> $t { | ||
Self::from(self.0 - offset.0) | ||
} | ||
} | ||
|
||
impl Id for $t { | ||
fn as_u32(self) -> u32 { | ||
self.0 | ||
} | ||
} | ||
}; | ||
} | ||
|
||
id_type!(Uid); | ||
id_type!(Gid); | ||
int_type!(IdOffset); | ||
|
||
impl IdOffset { | ||
pub fn as_u32(self) -> u32 { | ||
self.0 | ||
} | ||
} | ||
|
||
impl Add for IdOffset { | ||
type Output = Self; | ||
|
||
fn add(self, rhs: Self) -> Self::Output { | ||
Self(self.0 + rhs.0) | ||
} | ||
} | ||
|
||
impl Sub for IdOffset { | ||
type Output = Self; | ||
|
||
fn sub(self, rhs: Self) -> Self::Output { | ||
Self(self.0 - rhs.0) | ||
} | ||
} |
Oops, something went wrong.