Skip to content

Commit

Permalink
Remove pub modifiers from remove_dir_all module
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Mar 28, 2017
1 parent 2f129ba commit 110c4be
Showing 1 changed file with 59 additions and 55 deletions.
114 changes: 59 additions & 55 deletions src/rustup-utils/src/remove_dir_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(non_snake_case)]

use winapi::{
FileBasicInfo,
FILE_BASIC_INFO,
Expand Down Expand Up @@ -52,6 +54,8 @@ use winapi::{
SECURITY_SQOS_PRESENT,
FSCTL_GET_REPARSE_POINT,
BY_HANDLE_FILE_INFORMATION,
IO_REPARSE_TAG_SYMLINK,
IO_REPARSE_TAG_MOUNT_POINT,
};

use kernel32::{
Expand Down Expand Up @@ -154,7 +158,7 @@ pub fn remove_dir_all(path: &Path) -> io::Result<()> {
}


pub fn readdir(p: &Path) -> io::Result<ReadDir> {
fn readdir(p: &Path) -> io::Result<ReadDir> {
let root = p.to_path_buf();
let star = p.join("*");
let path = try!(to_u16s(&star));
Expand Down Expand Up @@ -228,21 +232,21 @@ fn remove_item(path: &Path, ctx: &mut RmdirContext) -> io::Result<()> {

macro_rules! compat_fn {
($module:ident: $(
pub fn $symbol:ident($($argname:ident: $argtype:ty),*)
fn $symbol:ident($($argname:ident: $argtype:ty),*)
-> $rettype:ty {
$($body:expr);*
}
)*) => ($(
#[allow(unused_variables)]
pub unsafe fn $symbol($($argname: $argtype),*) -> $rettype {
unsafe fn $symbol($($argname: $argtype),*) -> $rettype {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::mem;
use std::ffi::CString;
type F = unsafe extern "system" fn($($argtype),*) -> $rettype;

lazy_static! { static ref PTR: AtomicUsize = AtomicUsize::new(0);}

pub fn lookup(module: &str, symbol: &str) -> Option<usize> {
fn lookup(module: &str, symbol: &str) -> Option<usize> {
let mut module: Vec<u16> = module.encode_utf16().collect();
module.push(0);
let symbol = CString::new(symbol).unwrap();
Expand All @@ -255,7 +259,7 @@ macro_rules! compat_fn {
}
}

pub fn store_func(ptr: &AtomicUsize, module: &str, symbol: &str,
fn store_func(ptr: &AtomicUsize, module: &str, symbol: &str,
fallback: usize) -> usize {
let value = lookup(module, symbol).unwrap_or(fallback);
ptr.store(value, Ordering::SeqCst);
Expand All @@ -281,16 +285,16 @@ macro_rules! compat_fn {

compat_fn! {
kernel32:
pub fn GetFinalPathNameByHandleW(_hFile: HANDLE,
_lpszFilePath: LPCWSTR,
_cchFilePath: DWORD,
_dwFlags: DWORD) -> DWORD {
fn GetFinalPathNameByHandleW(_hFile: HANDLE,
_lpszFilePath: LPCWSTR,
_cchFilePath: DWORD,
_dwFlags: DWORD) -> DWORD {
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
}
pub fn SetFileInformationByHandle(_hFile: HANDLE,
_FileInformationClass: FILE_INFO_BY_HANDLE_CLASS,
_lpFileInformation: LPVOID,
_dwBufferSize: DWORD) -> BOOL {
fn SetFileInformationByHandle(_hFile: HANDLE,
_FileInformationClass: FILE_INFO_BY_HANDLE_CLASS,
_lpFileInformation: LPVOID,
_dwBufferSize: DWORD) -> BOOL {
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
}
}
Expand All @@ -303,7 +307,7 @@ fn cvt(i: i32) -> io::Result<i32> {
}
}

pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> io::Result<Vec<u16>> {
fn to_u16s<S: AsRef<OsStr>>(s: S) -> io::Result<Vec<u16>> {
fn inner(s: &OsStr) -> io::Result<Vec<u16>> {
let mut maybe_result: Vec<u16> = s.encode_wide().collect();
if maybe_result.iter().any(|&u| u == 0) {
Expand All @@ -316,7 +320,7 @@ pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> io::Result<Vec<u16>> {
inner(s.as_ref())
}

pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
match v.iter().position(|c| *c == 0) {
// don't include the 0
Some(i) => &v[..i],
Expand Down Expand Up @@ -371,16 +375,16 @@ fn fill_utf16_buf<F1, F2, T>(mut f1: F1, f2: F2) -> io::Result<T>
}

#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct FilePermissions { readonly: bool }
struct FilePermissions { readonly: bool }

impl FilePermissions {
pub fn new() -> FilePermissions { Default::default() }
pub fn readonly(&self) -> bool { self.readonly }
pub fn set_readonly(&mut self, readonly: bool) { self.readonly = readonly }
fn new() -> FilePermissions { Default::default() }
fn readonly(&self) -> bool { self.readonly }
fn set_readonly(&mut self, readonly: bool) { self.readonly = readonly }
}

#[derive(Clone)]
pub struct OpenOptions {
struct OpenOptions {
// generic
read: bool,
write: bool,
Expand All @@ -398,7 +402,7 @@ pub struct OpenOptions {
}

impl OpenOptions {
pub fn new() -> OpenOptions {
fn new() -> OpenOptions {
OpenOptions {
// generic
read: false,
Expand All @@ -416,8 +420,8 @@ impl OpenOptions {
security_attributes: 0,
}
}
pub fn custom_flags(&mut self, flags: u32) { self.custom_flags = flags; }
pub fn access_mode(&mut self, access_mode: u32) { self.access_mode = Some(access_mode); }
fn custom_flags(&mut self, flags: u32) { self.custom_flags = flags; }
fn access_mode(&mut self, access_mode: u32) { self.access_mode = Some(access_mode); }

fn get_access_mode(&self) -> io::Result<DWORD> {
const ERROR_INVALID_PARAMETER: i32 = 87;
Expand Down Expand Up @@ -467,10 +471,10 @@ impl OpenOptions {
}
}

pub struct File { handle: Handle }
struct File { handle: Handle }

impl File {
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
let path = try!(to_u16s(path));
let handle = unsafe {
CreateFileW(path.as_ptr(),
Expand All @@ -488,7 +492,7 @@ impl File {
}
}

pub fn file_attr(&self) -> io::Result<FileAttr> {
fn file_attr(&self) -> io::Result<FileAttr> {
unsafe {
let mut info: BY_HANDLE_FILE_INFORMATION = mem::zeroed();
try!(cvt(GetFileInformationByHandle(self.handle.raw(),
Expand All @@ -511,7 +515,7 @@ impl File {
}
}

pub fn set_attributes(&self, attr: DWORD) -> io::Result<()> {
fn set_attributes(&self, attr: DWORD) -> io::Result<()> {
let mut info = FILE_BASIC_INFO {
CreationTime: 0, // do not change
LastAccessTime: 0, // do not change
Expand All @@ -529,7 +533,7 @@ impl File {
Ok(())
}

pub fn rename(&self, new: &Path, replace: bool) -> io::Result<()> {
fn rename(&self, new: &Path, replace: bool) -> io::Result<()> {
// &self must be opened with DELETE permission
use std::iter;
#[cfg(target_arch = "x86")]
Expand Down Expand Up @@ -560,7 +564,7 @@ impl File {
Ok(())
}
}
pub fn set_perm(&self, perm: FilePermissions) -> io::Result<()> {
fn set_perm(&self, perm: FilePermissions) -> io::Result<()> {
let attr = try!(self.file_attr()).attributes;
if perm.readonly == (attr & FILE_ATTRIBUTE_READONLY != 0) {
Ok(())
Expand All @@ -571,7 +575,7 @@ impl File {
}
}

pub fn handle(&self) -> &Handle { &self.handle }
fn handle(&self) -> &Handle { &self.handle }

fn reparse_point<'a>(&self,
space: &'a mut [u8; MAXIMUM_REPARSE_DATA_BUFFER_SIZE])
Expand All @@ -595,7 +599,7 @@ impl File {


#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub enum FileType {
enum FileType {
Dir, File, SymlinkFile, SymlinkDir, ReparsePoint, MountPoint,
}

Expand All @@ -616,8 +620,8 @@ impl FileType {
}
}

pub fn is_dir(&self) -> bool { *self == FileType::Dir }
pub fn is_symlink_dir(&self) -> bool {
fn is_dir(&self) -> bool { *self == FileType::Dir }
fn is_symlink_dir(&self) -> bool {
*self == FileType::SymlinkDir || *self == FileType::MountPoint
}
}
Expand All @@ -635,21 +639,21 @@ impl DirEntry {
}
}

pub fn path(&self) -> PathBuf {
fn path(&self) -> PathBuf {
self.root.join(&self.file_name())
}

pub fn file_name(&self) -> OsString {
fn file_name(&self) -> OsString {
let filename = truncate_utf16_at_nul(&self.data.cFileName);
OsString::from_wide(filename)
}

pub fn file_type(&self) -> io::Result<FileType> {
fn file_type(&self) -> io::Result<FileType> {
Ok(FileType::new(self.data.dwFileAttributes,
/* reparse_tag = */ self.data.dwReserved0))
}

pub fn metadata(&self) -> io::Result<FileAttr> {
fn metadata(&self) -> io::Result<FileAttr> {
Ok(FileAttr {
attributes: self.data.dwFileAttributes,
creation_time: self.data.ftCreationTime,
Expand All @@ -668,12 +672,12 @@ impl DirEntry {



pub struct DirEntry {
struct DirEntry {
root: Arc<PathBuf>,
data: WIN32_FIND_DATAW,
}

pub struct ReadDir {
struct ReadDir {
handle: FindNextFileHandle,
root: Arc<PathBuf>,
first: Option<WIN32_FIND_DATAW>,
Expand Down Expand Up @@ -707,7 +711,7 @@ impl Iterator for ReadDir {


#[derive(Clone)]
pub struct FileAttr {
struct FileAttr {
attributes: DWORD,
creation_time: FILETIME,
last_access_time: FILETIME,
Expand All @@ -717,13 +721,13 @@ pub struct FileAttr {
}

impl FileAttr {
pub fn perm(&self) -> FilePermissions {
fn perm(&self) -> FilePermissions {
FilePermissions {
readonly: self.attributes & FILE_ATTRIBUTE_READONLY != 0
}
}

pub fn file_type(&self) -> FileType {
fn file_type(&self) -> FileType {
FileType::new(self.attributes, self.reparse_tag)
}

Expand All @@ -733,20 +737,20 @@ impl FileAttr {
}

#[repr(C)]
pub struct REPARSE_DATA_BUFFER {
pub ReparseTag: c_uint,
pub ReparseDataLength: c_ushort,
pub Reserved: c_ushort,
pub rest: (),
struct REPARSE_DATA_BUFFER {
ReparseTag: c_uint,
ReparseDataLength: c_ushort,
Reserved: c_ushort,
rest: (),
}

pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;
const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;


/// An owned container for `HANDLE` object, closing them on Drop.
///
/// All methods are inherited through a `Deref` impl to `RawHandle`
pub struct Handle(RawHandle);
struct Handle(RawHandle);

use std::ops::Deref;

Expand All @@ -756,13 +760,13 @@ use std::ops::Deref;
/// This does **not** drop the handle when it goes out of scope, use `Handle`
/// instead for that.
#[derive(Copy, Clone)]
pub struct RawHandle(HANDLE);
struct RawHandle(HANDLE);

unsafe impl Send for RawHandle {}
unsafe impl Sync for RawHandle {}

impl Handle {
pub fn new(handle: HANDLE) -> Handle {
fn new(handle: HANDLE) -> Handle {
Handle(RawHandle::new(handle))
}
}
Expand All @@ -779,11 +783,11 @@ impl Drop for Handle {
}

impl RawHandle {
pub fn new(handle: HANDLE) -> RawHandle {
fn new(handle: HANDLE) -> RawHandle {
RawHandle(handle)
}

pub fn raw(&self) -> HANDLE { self.0 }
fn raw(&self) -> HANDLE { self.0 }
}

struct FindNextFileHandle(HANDLE);
Expand All @@ -810,12 +814,12 @@ fn move_item(file: &File, ctx: &mut RmdirContext) -> io::Result<()> {
Ok(())
}

pub fn set_perm(path: &Path, perm: FilePermissions) -> io::Result<()> {
fn set_perm(path: &Path, perm: FilePermissions) -> io::Result<()> {
let mut opts = OpenOptions::new();
opts.access_mode(FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES);
opts.custom_flags(FILE_FLAG_BACKUP_SEMANTICS);
let file = try!(File::open(path, &opts));
file.set_perm(perm)
}

pub const VOLUME_NAME_DOS: DWORD = 0x0;
const VOLUME_NAME_DOS: DWORD = 0x0;

0 comments on commit 110c4be

Please sign in to comment.