81 Commits

Author SHA1 Message Date
Ben S ce47457bba Improve unreachable error messages 2016-01-27 19:39:19 +00:00
Ben S 78c4b410db Misc stylistic changes
- Extract all the 'if ptr.is_null() .. else ..' logic into a function that's basically the ptr_as_ref feature.
- Start using 'if let' syntax for some of these methods, which I'm not sure was around when I started writing them.
- vec![] -> Vec::new()
- loop -> for i in 0..
- Better comments, too
2016-01-27 19:09:49 +00:00
Ben S e32ec998e9 Switch to using from_utf8_lossy and document this
- Replace the calls to from_utf8_unchecked to from_utf8_lossy. I don't think OSes will actually check the validity of these username strings, and I'm not sure what the best thing to do here is! Error out?
- Remove the casts to *const i8. I think these stemmed from when I didn't know what I was doing.
- Document what this module does
2016-01-27 18:40:17 +00:00
Ben S 8e61c47f14 Comment OS methods 2016-01-27 16:32:31 +00:00
Ben S 476e31662b Begin to move OS routines to a new module
This tries to mimic the Metadata struct in libc by providing a struct with hidden fields, then an OS-dependent trait that gives you accessors to those fields.

Its goal is to remove any OS-specific things about users and groups -- which there will be a lot of -- from the default, publicly-accessible struct.

This may not seem like a big problem, but you'll see it a lot in mocking code: "unimportant" fields, such as the user's home directory and shell, can now be mocked to include a "sensible" value even when they're not used.
2016-01-26 20:24:36 +00:00
Ben S 28ddc0c667 Correct group ID type! 2016-01-26 20:23:16 +00:00
Ben S 77eefe0176 Rename OSUsers to UsersCache
It's more descriptive to call it a cache when there are functions that do all the *actual* OS users stuff separately now.

Similarly, we may as well call its constructor 'new' instead of 'empty_cache', because it's obvious that it's a cache now.
2016-01-26 19:17:24 +00:00
Ben S 9dadca8993 Move user-switching functions to their own module
Remove `c_int` from the libc public exports list while I'm at it. This might break someone's code, but it shouldn't have been used from this library anyway (only for user switching functions!)
2016-01-26 19:10:48 +00:00
Ben S 80e0c7d6f2 Move large parts of lib into base 2016-01-26 19:05:10 +00:00
Ben S 5e4967a377 Move the traits into their own module 2016-01-26 18:57:39 +00:00
Ben S 543925889c Stop using 'object' terminology! 2016-01-26 18:55:21 +00:00
Benjamin Sago 28d4d68daf Add Default impl for OSUsers 2016-01-16 12:13:17 -10:00
Benjamin Sago 052cf245f4 Split Users trait into Users and Groups traits
It was getting crowded in there! Not only are two groups of six easier to manage than one group of twelve, but the groups functions didn't actually have anything to do with users...
2016-01-14 23:15:53 -10:00
Benjamin Sago aa94ee89db Return Arcs instead of clones, and take &self
The primary goal of this commit was to avoid having to clone the users and groups it produces. The old implementation was written by me when I hadn't *fully* internalised how lifetimes worked, and just used `clone` to get around the error messages. It was only later that I realised how inefficient this actually is -- the values stored in the cache are never used, only their clones are!

Several attempt at this later, I settled on using `Arc` for everything and cloning *those* values instead.

All the gory details of what happened are written in a comment in the OS module. The short version is that it's now possible to change the `Users` trait's methods from taking `&mut self` to just taking `&self`. This is within the allowed uses of `Cell`: implementation details of a method that does actually return the same thing each time, which this is.

The main casualty is that the user now has to know about `Arc` and initialise a user with a username in such a container, which I hope to solve in a later commit.
2016-01-14 23:15:02 -10:00
Benjamin Sago 6a829c7b7d Extract bi-map groups into BiMap struct
The pairing of forward and backward maps was repeated twice: once for users, and once for groups. This commit refactors them into a BiMap struct that contains HashMaps of the relevant types.

Note that these new structs are just dumb structs: they don't implement any BiMap functionality, and all the other pieces of code don't do anything different with the maps -- they just refer to the struct's fields, making this a smaller change than it could be.
2016-01-13 18:01:08 +13:00
Benjamin Sago 04f52c4de9 Remove unnecessary 'pub's in C structs
These structures are all private to the current module anyway, so these `pub`s do nothing.
2016-01-10 21:15:50 +13:00
Benjamin Sago bb1299a288 Extract OS functions to os module
This commit does two things:

Firstly, it swaps the locations in the code where the actual, unsafe C library operations are done from being done in the OSUsers struct to in the stand-alone functions. This makes more sense, as previously, the stand-alone functions would instantiate a new cache, run a method on it, and then immediately discard the cache. Now the cache uses the methods, not the other way round.

Secondly, it extracts all of the caching methods to its own module, leaving `lib` with a bare-bones users implementation.
2016-01-10 21:14:05 +13:00
Benjamin Sago 83ddcc886c io::Error -> IOError, io::Result -> IOResult 2015-12-22 20:07:05 +11:00
Ben S 0e76551aa3 README improvements
- Stylistic changes
- Fix installation instructions
- Add mock docs
- Upgrade required version of libc
2015-11-27 01:54:53 +00:00
Alexander Polakov 0940e6ae16 get_all_users() API 2015-10-20 14:30:45 +03:00
Ben S 4c966938db Achieve independence from gated features!
This commit makes rust-users runnable on Rust Stable and Beta!

- ToOwned in the collections crate can be imported by `std::borrow::ToOwned`
  instead, removing the collections flag.
- `as_ref` on raw pointers can be null-checked manually, then dereferenced,
  removing the ptr_as_ref flag. The new code reads better, too.
2015-08-02 16:41:19 +01:00
Ben S de6a1601db Update broken comment link... 2015-08-02 15:35:25 +01:00
Konstantin Stepanov 5cab91cf73 fix docs for switch_user_group 2015-07-23 01:40:53 +03:00
Konstantin Stepanov 7c19c92fd7 switch effective uid only 2015-07-21 12:55:44 +03:00
Konstantin Stepanov 184bd434e1 fixes 2015-07-21 12:19:45 +03:00