]> git.proxmox.com Git - rustc.git/blob - src/vendor/nix/src/sys/quota.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / vendor / nix / src / sys / quota.rs
1 use {Errno, Result, NixPath};
2 use libc::{c_int, c_char};
3
4 #[cfg(all(target_os = "linux",
5 any(target_arch = "x86",
6 target_arch = "x86_64",
7 target_arch = "arm")),
8 )]
9 pub mod quota {
10 use libc::{self, c_int};
11
12 pub struct QuotaCmd(pub QuotaSubCmd, pub QuotaType);
13 pub type QuotaSubCmd = c_int;
14
15 impl QuotaCmd {
16 pub fn as_int(&self) -> c_int {
17 ((self.0 << 8) | (self.1 & 0x00ff)) as c_int
18 }
19 }
20
21 // linux quota version >= 2
22 pub const Q_SYNC: QuotaSubCmd = 0x800001;
23 pub const Q_QUOTAON: QuotaSubCmd = 0x800002;
24 pub const Q_QUOTAOFF: QuotaSubCmd = 0x800003;
25 pub const Q_GETFMT: QuotaSubCmd = 0x800004;
26 pub const Q_GETINFO: QuotaSubCmd = 0x800005;
27 pub const Q_SETINFO: QuotaSubCmd = 0x800006;
28 pub const Q_GETQUOTA: QuotaSubCmd = 0x800007;
29 pub const Q_SETQUOTA: QuotaSubCmd = 0x800008;
30
31 pub type QuotaType = c_int;
32
33 pub const USRQUOTA: QuotaType = 0;
34 pub const GRPQUOTA: QuotaType = 1;
35
36 pub type QuotaFmt = c_int;
37
38 pub const QFMT_VFS_OLD: QuotaFmt = 1;
39 pub const QFMT_VFS_V0: QuotaFmt = 2;
40 pub const QFMT_VFS_V1: QuotaFmt = 4;
41
42 libc_bitflags!(
43 #[derive(Default)]
44 pub flags QuotaValidFlags: u32 {
45 QIF_BLIMITS,
46 QIF_SPACE,
47 QIF_ILIMITS,
48 QIF_INODES,
49 QIF_BTIME,
50 QIF_ITIME,
51 QIF_LIMITS,
52 QIF_USAGE,
53 QIF_TIMES,
54 QIF_ALL,
55 }
56 );
57
58 #[repr(C)]
59 #[derive(Default,Debug,Copy,Clone)]
60 pub struct Dqblk {
61 pub bhardlimit: u64,
62 pub bsoftlimit: u64,
63 pub curspace: u64,
64 pub ihardlimit: u64,
65 pub isoftlimit: u64,
66 pub curinodes: u64,
67 pub btime: u64,
68 pub itime: u64,
69 pub valid: QuotaValidFlags,
70 }
71 }
72
73 mod ffi {
74 use libc::{c_int, c_char};
75
76 extern {
77 pub fn quotactl(cmd: c_int, special: * const c_char, id: c_int, data: *mut c_char) -> c_int;
78 }
79 }
80
81 use std::ptr;
82
83 fn quotactl<P: ?Sized + NixPath>(cmd: quota::QuotaCmd, special: Option<&P>, id: c_int, addr: *mut c_char) -> Result<()> {
84 unsafe {
85 Errno::clear();
86 let res = try!(
87 match special {
88 Some(dev) => dev.with_nix_path(|path| ffi::quotactl(cmd.as_int(), path.as_ptr(), id, addr)),
89 None => Ok(ffi::quotactl(cmd.as_int(), ptr::null(), id, addr)),
90 }
91 );
92
93 Errno::result(res).map(drop)
94 }
95 }
96
97 pub fn quotactl_on<P: ?Sized + NixPath>(which: quota::QuotaType, special: &P, format: quota::QuotaFmt, quota_file: &P) -> Result<()> {
98 try!(quota_file.with_nix_path(|path| {
99 let mut path_copy = path.to_bytes_with_nul().to_owned();
100 let p: *mut c_char = path_copy.as_mut_ptr() as *mut c_char;
101 quotactl(quota::QuotaCmd(quota::Q_QUOTAON, which), Some(special), format as c_int, p)
102 }))
103 }
104
105 pub fn quotactl_off<P: ?Sized + NixPath>(which: quota::QuotaType, special: &P) -> Result<()> {
106 quotactl(quota::QuotaCmd(quota::Q_QUOTAOFF, which), Some(special), 0, ptr::null_mut())
107 }
108
109 pub fn quotactl_sync<P: ?Sized + NixPath>(which: quota::QuotaType, special: Option<&P>) -> Result<()> {
110 quotactl(quota::QuotaCmd(quota::Q_SYNC, which), special, 0, ptr::null_mut())
111 }
112
113 pub fn quotactl_get<P: ?Sized + NixPath>(which: quota::QuotaType, special: &P, id: c_int, dqblk: &mut quota::Dqblk) -> Result<()> {
114 use std::mem;
115 unsafe {
116 quotactl(quota::QuotaCmd(quota::Q_GETQUOTA, which), Some(special), id, mem::transmute(dqblk))
117 }
118 }
119
120 pub fn quotactl_set<P: ?Sized + NixPath>(which: quota::QuotaType, special: &P, id: c_int, dqblk: &quota::Dqblk) -> Result<()> {
121 use std::mem;
122 let mut dqblk_copy = *dqblk;
123 unsafe {
124 quotactl(quota::QuotaCmd(quota::Q_SETQUOTA, which), Some(special), id, mem::transmute(&mut dqblk_copy))
125 }
126 }