]>
git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/unix++.cpp
2 * Copyright (c) 2000-2001,2003-2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // unix++ - C++ layer for basic UNIX facilities
29 #include <security_utilities/memutils.h>
30 #include <security_utilities/debugging.h>
31 #include <sys/xattr.h>
36 namespace UnixPlusPlus
{
38 using LowLevelMemoryUtilities::increment
;
42 // Canonical open of a file descriptor. All other open operations channel through this.
43 // Note that we abuse the S_IFMT mode flags as operational options.
45 void FileDesc::open(const char *path
, int flags
, mode_t mode
)
47 if ((mFd
= ::open(path
, flags
, mode
& ~S_IFMT
)) == -1) {
48 if (errno
== ENOENT
&& (mode
& S_IFMT
) == modeMissingOk
) {
55 secdebug("unixio", "open(%s,0x%x,0x%x) = %d", path
, flags
, mode
, mFd
);
58 void FileDesc::close()
61 checkError(::close(mFd
));
62 secdebug("unixio", "close(%d)", mFd
);
69 // Filedescoid operations
71 size_t FileDesc::read(void *addr
, size_t length
)
73 switch (ssize_t rc
= ::read(mFd
, addr
, length
)) {
74 case 0: // end-of-source
75 if (length
== 0) { // check for errors, but don't set mAtEnd unless we have to
76 secdebug("unixio", "%d zero read (ignored)", mFd
);
80 secdebug("unixio", "%d end of data", mFd
);
84 return 0; // no data, unknown end-of-source status
85 UnixError::throwMe(); // throw error
91 size_t FileDesc::write(const void *addr
, size_t length
)
93 ssize_t rc
= ::write(mFd
, addr
, length
);
104 // I/O with integral positioning.
105 // These don't affect file position and the atEnd() flag; and they
106 // don't make allowances for asynchronous I/O.
108 size_t FileDesc::read(void *addr
, size_t length
, size_t position
)
110 return checkError(::pread(mFd
, addr
, length
, position
));
113 size_t FileDesc::write(const void *addr
, size_t length
, size_t position
)
115 return checkError(::pwrite(mFd
, addr
, length
, position
));
120 // Waiting (repeating) I/O
122 size_t FileDesc::readAll(void *addr
, size_t length
)
125 while (length
> 0 && !atEnd()) {
126 size_t size
= read(addr
, length
);
127 addr
= increment(addr
, size
);
134 size_t FileDesc::readAll(string
&value
)
139 if (size_t size
= read(buffer
, sizeof(buffer
))) {
140 s
+= string(buffer
, size
);
145 return value
.length();
149 void FileDesc::writeAll(const void *addr
, size_t length
)
152 size_t size
= write(addr
, length
);
153 addr
= increment(addr
, size
);
162 #warning Cast to size_t may loose precision, only a problem for large files.
164 size_t FileDesc::seek(size_t position
, int whence
)
166 return (size_t)checkError(::lseek(mFd
, position
, whence
));
169 size_t FileDesc::position() const
171 return (size_t)checkError(::lseek(mFd
, 0, SEEK_CUR
));
178 void *FileDesc::mmap(int prot
, size_t length
, int flags
, size_t offset
, void *addr
)
180 if (!(flags
& (MAP_PRIVATE
| MAP_SHARED
))) // one is required
181 flags
|= MAP_PRIVATE
;
182 void *result
= ::mmap(addr
, length
? length
: fileSize(), prot
, flags
, mFd
, offset
);
183 if (result
== MAP_FAILED
)
184 UnixError::throwMe();
190 // Basic fcntl support
192 int FileDesc::fcntl(int cmd
, void *arg
) const
194 int rc
= ::fcntl(mFd
, cmd
, arg
);
195 secdebug("unixio", "%d fcntl(%d,%p) = %d", mFd
, cmd
, arg
, rc
);
196 return checkError(rc
);
203 void FileDesc::setFlag(int flag
, bool on
) const
205 if (flag
) { // if there's anything at all to do...
206 int oldFlags
= flags();
207 flags(on
? (oldFlags
| flag
) : (oldFlags
& ~flag
));
213 // Duplication operations
215 FileDesc
FileDesc::dup() const
217 return FileDesc(checkError(::dup(mFd
)), atEnd());
220 FileDesc
FileDesc::dup(int newFd
) const
222 return FileDesc(checkError(::dup2(mFd
, newFd
)), atEnd());
227 // Advisory locking, fcntl style
229 void FileDesc::lock(int type
, const Pos
&pos
)
231 LockArgs
args(type
, pos
);
232 IFDEBUG(args
.debug(fd(), "lock"));
233 checkError(fcntl(F_SETLKW
, &args
));
236 bool FileDesc::tryLock(int type
, const Pos
&pos
)
238 LockArgs
args(type
, pos
);
239 IFDEBUG(args
.debug(fd(), "tryLock"));
241 fcntl(F_SETLK
, &args
);
243 } catch (const UnixError
&err
) {
244 if (err
.error
== EAGAIN
)
253 void FileDesc::LockArgs::debug(int fd
, const char *what
)
255 secdebug("fdlock", "%d %s %s:%ld(%ld)", fd
, what
,
256 (l_whence
== SEEK_SET
) ? "ABS" : (l_whence
== SEEK_CUR
) ? "REL" : "END",
257 long(l_start
), long(l_len
));
266 int FileDesc::ioctl(int cmd
, void *arg
) const
268 int rc
= ::ioctl(mFd
, cmd
, arg
);
270 UnixError::throwMe();
278 void FileDesc::setAttr(const char *name
, const void *value
, size_t length
,
279 u_int32_t position
/* = 0 */, int options
/* = 0 */)
281 checkError(::fsetxattr(mFd
, name
, value
, length
, position
, options
));
284 ssize_t
FileDesc::getAttrLength(const char *name
)
286 ssize_t rc
= ::fgetxattr(mFd
, name
, NULL
, 0, 0, 0);
292 UnixError::throwMe();
297 ssize_t
FileDesc::getAttr(const char *name
, void *value
, size_t length
,
298 u_int32_t position
/* = 0 */, int options
/* = 0 */)
300 ssize_t rc
= ::fgetxattr(mFd
, name
, value
, length
, position
, options
);
306 UnixError::throwMe();
311 void FileDesc::removeAttr(const char *name
, int options
/* = 0 */)
313 if (::fremovexattr(mFd
, name
, options
))
316 if (!(options
& XATTR_REPLACE
)) // somewhat mis-using an API flag here...
317 return; // attribute not found; we'll call that okay
320 UnixError::throwMe();
324 size_t FileDesc::listAttr(char *value
, size_t length
, int options
/* = 0 */)
326 return checkError(::flistxattr(mFd
, value
, length
, options
));
330 void FileDesc::setAttr(const std::string
&name
, const std::string
&value
, int options
/* = 0 */)
332 return setAttr(name
, value
.c_str(), value
.size(), 0, options
);
335 std::string
FileDesc::getAttr(const std::string
&name
, int options
/* = 0 */)
337 char buffer
[4096]; //@@@ auto-expand?
338 ssize_t length
= getAttr(name
, buffer
, sizeof(buffer
), 0, options
);
340 return string(buffer
, length
);
349 void FileDesc::fstat(UnixStat
&st
) const
351 if (::fstat(mFd
, &st
))
352 UnixError::throwMe();
355 size_t FileDesc::fileSize() const
359 return (size_t)st
.st_size
;
362 bool FileDesc::isA(int mode
) const
366 return (st
.st_mode
& S_IFMT
) == mode
;
370 void FileDesc::chown(uid_t uid
)
372 checkError(::fchown(mFd
, uid
, gid_t(-1)));
375 void FileDesc::chown(uid_t uid
, gid_t gid
)
377 checkError(::fchown(mFd
, uid
, gid
));
380 void FileDesc::chgrp(gid_t gid
)
382 checkError(::fchown(mFd
, uid_t(-1), gid
));
385 void FileDesc::chmod(mode_t mode
)
387 checkError(::fchmod(mFd
, mode
));
390 void FileDesc::chflags(u_int flags
)
392 checkError(::fchflags(mFd
, flags
));
396 FILE *FileDesc::fdopen(const char *form
)
398 //@@@ pick default value for 'form' based on chracteristics of mFd
399 return ::fdopen(mFd
, form
);
404 // Signals and signal masks
406 SigSet
sigMask(SigSet set
, int how
/* = SIG_SETMASK */)
409 checkError(::sigprocmask(how
, &set
.value(), &old
));
415 // Make or use a directory, open-style.
417 // Flags are to be interpreted like open(2) flags; particularly
418 // O_CREAT make the directory if not present
419 // O_EXCL fail if the directory is present
420 // Other open(2) flags are currently ignored.
422 // Yes, it's a function.
424 void makedir(const char *path
, int flags
, mode_t mode
)
427 if (!stat(path
, &st
)) {
429 UnixError::throwMe(EEXIST
);
430 if (!S_ISDIR(st
.st_mode
))
431 UnixError::throwMe(ENOTDIR
);
432 secdebug("makedir", "%s exists", path
);
437 if (errno
!= ENOENT
|| !(flags
& O_CREAT
))
438 UnixError::throwMe();
440 // ENOENT and creation enabled
441 if (::mkdir(path
, mode
)) {
442 if (errno
== EEXIST
&& !(flags
& O_EXCL
))
443 return; // fine (race condition, resolved)
444 UnixError::throwMe();
446 secdebug("makedir", "%s created", path
);
451 // Open, read/write, close a (small) file on disk
453 int ffprintf(const char *path
, int flags
, mode_t mode
, const char *format
, ...)
455 FileDesc
fd(path
, flags
, mode
);
456 FILE *f
= fd
.fdopen("w");
458 va_start(args
, format
);
459 int rc
= vfprintf(f
, format
, args
);
462 UnixError::throwMe();
466 int ffscanf(const char *path
, const char *format
, ...)
468 if (FILE *f
= fopen(path
, "r")) {
470 va_start(args
, format
);
471 int rc
= vfscanf(f
, format
, args
);
476 UnixError::throwMe();
480 } // end namespace IPPlusPlus
481 } // end namespace Security