11//! Extended attributes.
22
33use crate :: convert_res;
4- use alloc:: vec;
54use core:: ffi:: CStr ;
6- use core:: ptr :: copy_nonoverlapping ;
5+ use core:: mem :: MaybeUninit ;
76use core:: slice;
87use libc:: { c_char, c_int, c_void, size_t, ssize_t} ;
98use rustix:: fd:: BorrowedFd ;
@@ -20,18 +19,10 @@ unsafe extern "C" fn getxattr(
2019
2120 let path = CStr :: from_ptr ( path) ;
2221 let name = CStr :: from_ptr ( name) ;
23- // `slice::from_raw_parts_mut` assumes that the memory is initialized,
24- // which our C API here doesn't guarantee. Since rustix currently requires
25- // a slice, use a temporary copy.
26- let mut buf = vec ! [ 0 ; len] ;
27- match convert_res ( rustix:: fs:: getxattr ( path, name, & mut buf) ) {
28- Some ( size) => {
29- // If `size` is 0, `value` could be null.
30- if size != 0 {
31- copy_nonoverlapping ( buf. as_ptr ( ) , value. cast ( ) , size) ;
32- }
33- size as ssize_t
34- }
22+ let buf = slice:: from_raw_parts_mut ( value. cast :: < MaybeUninit < u8 > > ( ) , len) ;
23+
24+ match convert_res ( rustix:: fs:: getxattr ( path, name, buf) ) {
25+ Some ( ( init, _uninit) ) => init. len ( ) as ssize_t ,
3526 None => -1 ,
3627 }
3728}
@@ -47,18 +38,10 @@ unsafe extern "C" fn lgetxattr(
4738
4839 let path = CStr :: from_ptr ( path) ;
4940 let name = CStr :: from_ptr ( name) ;
50- // `slice::from_raw_parts_mut` assumes that the memory is initialized,
51- // which our C API here doesn't guarantee. Since rustix currently requires
52- // a slice, use a temporary copy.
53- let mut buf = vec ! [ 0 ; len] ;
54- match convert_res ( rustix:: fs:: lgetxattr ( path, name, & mut buf) ) {
55- Some ( size) => {
56- // If `size` is 0, `value` could be null.
57- if size != 0 {
58- copy_nonoverlapping ( buf. as_ptr ( ) , value. cast ( ) , size) ;
59- }
60- size as ssize_t
61- }
41+ let buf = slice:: from_raw_parts_mut ( value. cast :: < MaybeUninit < u8 > > ( ) , len) ;
42+
43+ match convert_res ( rustix:: fs:: lgetxattr ( path, name, buf) ) {
44+ Some ( ( init, _uninit) ) => init. len ( ) as ssize_t ,
6245 None => -1 ,
6346 }
6447}
@@ -74,18 +57,10 @@ unsafe extern "C" fn fgetxattr(
7457
7558 let fd = BorrowedFd :: borrow_raw ( fd) ;
7659 let name = CStr :: from_ptr ( name) ;
77- // `slice::from_raw_parts_mut` assumes that the memory is initialized,
78- // which our C API here doesn't guarantee. Since rustix currently requires
79- // a slice, use a temporary copy.
80- let mut buf = vec ! [ 0 ; len] ;
81- match convert_res ( rustix:: fs:: fgetxattr ( fd, name, & mut buf) ) {
82- Some ( size) => {
83- // If `size` is 0, `value` could be null.
84- if size != 0 {
85- copy_nonoverlapping ( buf. as_ptr ( ) , value. cast ( ) , size) ;
86- }
87- size as ssize_t
88- }
60+ let buf = slice:: from_raw_parts_mut ( value. cast :: < MaybeUninit < u8 > > ( ) , len) ;
61+
62+ match convert_res ( rustix:: fs:: fgetxattr ( fd, name, buf) ) {
63+ Some ( ( init, _uninit) ) => init. len ( ) as ssize_t ,
8964 None => -1 ,
9065 }
9166}
@@ -155,18 +130,10 @@ unsafe extern "C" fn listxattr(path: *const c_char, list: *mut c_char, len: size
155130 libc ! ( libc:: listxattr( path, list, len) ) ;
156131
157132 let path = CStr :: from_ptr ( path) ;
158- // `slice::from_raw_parts_mut` assumes that the memory is initialized,
159- // which our C API here doesn't guarantee. Since rustix currently requires
160- // a slice, use a temporary copy.
161- let mut buf = vec ! [ 0 ; len] ;
162- match convert_res ( rustix:: fs:: listxattr ( path, & mut buf) ) {
163- Some ( size) => {
164- // If `size` is 0, `value` could be null.
165- if size != 0 {
166- copy_nonoverlapping ( buf. as_ptr ( ) , list. cast ( ) , size) ;
167- }
168- size as ssize_t
169- }
133+ let buf = slice:: from_raw_parts_mut ( list. cast :: < MaybeUninit < u8 > > ( ) , len) ;
134+
135+ match convert_res ( rustix:: fs:: listxattr ( path, buf) ) {
136+ Some ( ( init, _uninit) ) => init. len ( ) as ssize_t ,
170137 None => -1 ,
171138 }
172139}
@@ -176,18 +143,10 @@ unsafe extern "C" fn llistxattr(path: *const c_char, list: *mut c_char, len: siz
176143 libc ! ( libc:: llistxattr( path, list, len) ) ;
177144
178145 let path = CStr :: from_ptr ( path) ;
179- // `slice::from_raw_parts_mut` assumes that the memory is initialized,
180- // which our C API here doesn't guarantee. Since rustix currently requires
181- // a slice, use a temporary copy.
182- let mut buf = vec ! [ 0 ; len] ;
183- match convert_res ( rustix:: fs:: llistxattr ( path, & mut buf) ) {
184- Some ( size) => {
185- // If `size` is 0, `value` could be null.
186- if size != 0 {
187- copy_nonoverlapping ( buf. as_ptr ( ) , list. cast ( ) , size) ;
188- }
189- size as ssize_t
190- }
146+ let buf = slice:: from_raw_parts_mut ( list. cast :: < MaybeUninit < u8 > > ( ) , len) ;
147+
148+ match convert_res ( rustix:: fs:: llistxattr ( path, buf) ) {
149+ Some ( ( init, _uninit) ) => init. len ( ) as ssize_t ,
191150 None => -1 ,
192151 }
193152}
@@ -197,18 +156,10 @@ unsafe extern "C" fn flistxattr(fd: c_int, list: *mut c_char, len: size_t) -> ss
197156 libc ! ( libc:: flistxattr( fd, list, len) ) ;
198157
199158 let fd = BorrowedFd :: borrow_raw ( fd) ;
200- // `slice::from_raw_parts_mut` assumes that the memory is initialized,
201- // which our C API here doesn't guarantee. Since rustix currently requires
202- // a slice, use a temporary copy.
203- let mut buf = vec ! [ 0 ; len] ;
204- match convert_res ( rustix:: fs:: flistxattr ( fd, & mut buf) ) {
205- Some ( size) => {
206- // If `size` is 0, `value` could be null.
207- if size != 0 {
208- copy_nonoverlapping ( buf. as_ptr ( ) , list. cast ( ) , size) ;
209- }
210- size as ssize_t
211- }
159+ let buf = slice:: from_raw_parts_mut ( list. cast :: < MaybeUninit < u8 > > ( ) , len) ;
160+
161+ match convert_res ( rustix:: fs:: flistxattr ( fd, buf) ) {
162+ Some ( ( init, _uninit) ) => init. len ( ) as ssize_t ,
212163 None => -1 ,
213164 }
214165}
0 commit comments