]>
git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/unix++.cpp
604a08b7566020874db3c4b3f0aca52d3ebda1de
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
)
53 secdebug("unixio", "open(%s,0x%x,0x%x) = %d", path
, flags
, mode
, mFd
);
56 void FileDesc::close()
59 checkError(::close(mFd
));
60 secdebug("unixio", "close(%d)", mFd
);
67 // Filedescoid operations
69 size_t FileDesc::read(void *addr
, size_t length
)
71 switch (ssize_t rc
= ::read(mFd
, addr
, length
)) {
72 case 0: // end-of-source
73 if (length
== 0) { // check for errors, but don't set mAtEnd unless we have to
74 secdebug("unixio", "%d zero read (ignored)", mFd
);
78 secdebug("unixio", "%d end of data", mFd
);
82 return 0; // no data, unknown end-of-source status
83 UnixError::throwMe(); // throw error
89 size_t FileDesc::write(const void *addr
, size_t length
)
91 ssize_t rc
= ::write(mFd
, addr
, length
);
102 // I/O with integral positioning.
103 // These don't affect file position and the atEnd() flag; and they
104 // don't make allowances for asynchronous I/O.
106 size_t FileDesc::read(void *addr
, size_t length
, off_t position
)
108 return checkError(::pread(mFd
, addr
, length
, position
));
111 size_t FileDesc::write(const void *addr
, size_t length
, off_t position
)
113 return checkError(::pwrite(mFd
, addr
, length
, position
));
118 // Waiting (repeating) I/O
120 size_t FileDesc::readAll(void *addr
, size_t length
)
123 while (length
> 0 && !atEnd()) {
124 size_t size
= read(addr
, length
);
125 addr
= increment(addr
, size
);
132 size_t FileDesc::readAll(string
&value
)
137 if (size_t size
= read(buffer
, sizeof(buffer
))) {
138 s
+= string(buffer
, size
);
143 return value
.length();
147 void FileDesc::writeAll(const void *addr
, size_t length
)
150 size_t size
= write(addr
, length
);
151 addr
= increment(addr
, size
);
160 size_t FileDesc::seek(off_t position
, int whence
)
162 return checkError(::lseek(mFd
, position
, whence
));
165 size_t FileDesc::position() const
167 return checkError(::lseek(mFd
, 0, SEEK_CUR
));
174 void *FileDesc::mmap(int prot
, size_t length
, int flags
, off_t offset
, void *addr
)
176 if (!(flags
& (MAP_PRIVATE
| MAP_SHARED
))) // one is required
177 flags
|= MAP_PRIVATE
;
178 void *result
= ::mmap(addr
, length
? length
: fileSize(), prot
, flags
, mFd
, offset
);
179 if (result
== MAP_FAILED
)
180 UnixError::throwMe();
186 // Basic fcntl support
188 int FileDesc::fcntl(int cmd
, void *arg
) const
190 int rc
= ::fcntl(mFd
, cmd
, arg
);
191 secdebug("unixio", "%d fcntl(%d,%p) = %d", mFd
, cmd
, arg
, rc
);
192 return checkError(rc
);
199 void FileDesc::setFlag(int flag
, bool on
) const
201 if (flag
) { // if there's anything at all to do...
202 int oldFlags
= flags();
203 flags(on
? (oldFlags
| flag
) : (oldFlags
& ~flag
));
209 // Duplication operations
211 FileDesc
FileDesc::dup() const
213 return FileDesc(checkError(::dup(mFd
)), atEnd());
216 FileDesc
FileDesc::dup(int newFd
) const
218 return FileDesc(checkError(::dup2(mFd
, newFd
)), atEnd());
223 // Advisory locking, fcntl style
225 void FileDesc::lock(int type
, const Pos
&pos
)
227 LockArgs
args(type
, pos
);
228 IFDEBUG(args
.debug(fd(), "lock"));
229 checkError(fcntl(F_SETLKW
, &args
));
232 bool FileDesc::tryLock(int type
, const Pos
&pos
)
234 LockArgs
args(type
, pos
);
235 IFDEBUG(args
.debug(fd(), "tryLock"));
237 fcntl(F_SETLK
, &args
);
239 } catch (const UnixError
&err
) {
240 if (err
.error
== EAGAIN
)
249 void FileDesc::LockArgs::debug(int fd
, const char *what
)
251 secdebug("fdlock", "%d %s %s:%ld(%ld)", fd
, what
,
252 (l_whence
== SEEK_SET
) ? "ABS" : (l_whence
== SEEK_CUR
) ? "REL" : "END",
253 long(l_start
), long(l_len
));
262 int FileDesc::ioctl(int cmd
, void *arg
) const
264 int rc
= ::ioctl(mFd
, cmd
, arg
);
266 UnixError::throwMe();
274 void FileDesc::setAttr(const char *name
, const void *value
, size_t length
,
275 u_int32_t position
/* = 0 */, int options
/* = 0 */)
277 checkError(::fsetxattr(mFd
, name
, value
, length
, position
, options
));
280 ssize_t
FileDesc::getAttrLength(const char *name
)
282 ssize_t rc
= ::fgetxattr(mFd
, name
, NULL
, 0, 0, 0);
288 UnixError::throwMe();
293 ssize_t
FileDesc::getAttr(const char *name
, void *value
, size_t length
,
294 u_int32_t position
/* = 0 */, int options
/* = 0 */)
296 ssize_t rc
= ::fgetxattr(mFd
, name
, value
, length
, position
, options
);
302 UnixError::throwMe();
307 void FileDesc::removeAttr(const char *name
, int options
/* = 0 */)
309 if (::fremovexattr(mFd
, name
, options
))
312 if (!(options
& XATTR_REPLACE
)) // somewhat mis-using an API flag here...
313 return; // attribute not found; we'll call that okay
316 UnixError::throwMe();
320 size_t FileDesc::listAttr(char *value
, size_t length
, int options
/* = 0 */)
322 return checkError(::flistxattr(mFd
, value
, length
, options
));
326 void FileDesc::setAttr(const std::string
&name
, const std::string
&value
, int options
/* = 0 */)
328 return setAttr(name
, value
.c_str(), value
.size(), 0, options
);
331 std::string
FileDesc::getAttr(const std::string
&name
, int options
/* = 0 */)
333 char buffer
[4096]; //@@@ auto-expand?
334 ssize_t length
= getAttr(name
, buffer
, sizeof(buffer
), 0, options
);
336 return string(buffer
, length
);
345 void FileDesc::fstat(UnixStat
&st
) const
347 if (::fstat(mFd
, &st
))
348 UnixError::throwMe();
351 size_t FileDesc::fileSize() const
358 bool FileDesc::isA(int mode
) const
362 return (st
.st_mode
& S_IFMT
) == mode
;
366 void FileDesc::chown(uid_t uid
)
368 checkError(::fchown(mFd
, uid
, gid_t(-1)));
371 void FileDesc::chown(uid_t uid
, gid_t gid
)
373 checkError(::fchown(mFd
, uid
, gid
));
376 void FileDesc::chgrp(gid_t gid
)
378 checkError(::fchown(mFd
, uid_t(-1), gid
));
381 void FileDesc::chmod(mode_t mode
)
383 checkError(::fchmod(mFd
, mode
));
386 void FileDesc::chflags(u_int flags
)
388 checkError(::fchflags(mFd
, flags
));
392 FILE *FileDesc::fdopen(const char *form
)
394 //@@@ pick default value for 'form' based on chracteristics of mFd
395 return ::fdopen(mFd
, form
);
400 // Signals and signal masks
402 SigSet
sigMask(SigSet set
, int how
/* = SIG_SETMASK */)
405 checkError(::sigprocmask(how
, &set
.value(), &old
));
411 // Make or use a directory, open-style.
413 // Flags are to be interpreted like open(2) flags; particularly
414 // O_CREAT make the directory if not present
415 // O_EXCL fail if the directory is present
416 // Other open(2) flags are currently ignored.
418 // Yes, it's a function.
420 void makedir(const char *path
, int flags
, mode_t mode
)
423 if (!stat(path
, &st
)) {
425 UnixError::throwMe(EEXIST
);
426 if (!S_ISDIR(st
.st_mode
))
427 UnixError::throwMe(ENOTDIR
);
428 secdebug("makedir", "%s exists", path
);
433 if (errno
!= ENOENT
|| !(flags
& O_CREAT
))
434 UnixError::throwMe();
436 // ENOENT and creation enabled
437 if (::mkdir(path
, mode
)) {
438 if (errno
== EEXIST
&& !(flags
& O_EXCL
))
439 return; // fine (race condition, resolved)
440 UnixError::throwMe();
442 secdebug("makedir", "%s created", path
);
447 // Open, read/write, close a (small) file on disk
449 int ffprintf(const char *path
, int flags
, mode_t mode
, const char *format
, ...)
451 FileDesc
fd(path
, flags
, mode
);
452 FILE *f
= fd
.fdopen("w");
454 va_start(args
, format
);
455 int rc
= vfprintf(f
, format
, args
);
458 UnixError::throwMe();
462 int ffscanf(const char *path
, const char *format
, ...)
464 if (FILE *f
= fopen(path
, "r")) {
466 va_start(args
, format
);
467 int rc
= vfscanf(f
, format
, args
);
472 UnixError::throwMe();
476 } // end namespace IPPlusPlus
477 } // end namespace Security