bool CFile::Flush() { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot flush closed file.")); bool flushed = (FLUSH_FD(m_fd) != -1); SYSCALL_CHECK(flushed, wxT("flushing file")); return flushed; }
bool CFile::Close() { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot close closed file.")); bool closed = (close(m_fd) != -1); SYSCALL_CHECK(closed, wxT("closing file")); m_fd = fd_invalid; return closed; }
bool CFile::SetLength(size_t new_len) { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot set length when no file is open.")); #ifdef __WXMSW__ int result = chsize(m_fd, new_len); #else int result = ftruncate(m_fd, new_len); #endif SYSCALL_CHECK((result != -1), wxT("truncating file")); return (result != -1); }
static void getSocketAddress(char *hostName, int socketFamily, int socketType, struct sockaddr *socketAddress) /* find a `socketAddress` on `hostName` of `socketFamily` and `socketType` */ { /* * looks on `hostName` (either a host name or -- for AF_INET -- a * string representation of a dotted decimal IP number) for a * socket `socketFamily` (AF_*) and `socketType` (SOCK_*) and sets * `*socketAddress` to its sockaddr. */ struct addrinfo *addrInfoResponses; struct addrinfo addrInfoQuery; // provide the canonical name of the hostName addrInfoQuery.ai_flags = AI_CANONNAME; addrInfoQuery.ai_family = socketFamily; addrInfoQuery.ai_socktype = socketType; addrInfoQuery.ai_protocol = 0; addrInfoQuery.ai_addrlen = 0; addrInfoQuery.ai_canonname = NULL; addrInfoQuery.ai_addr = NULL; addrInfoQuery.ai_next = NULL; SYSCALL_CHECK(getaddrinfo(hostName,NULL , &addrInfoQuery, &addrInfoResponses)); // a single entry should be returned assert(addrInfoResponses != NULL); if (addrInfoResponses->ai_next != NULL) { fprintf(stderr, "warning: in getSocketAddress(), multiple addrinfo" " records found -- returning the first one\n"); } *socketAddress = *addrInfoResponses->ai_addr; freeaddrinfo(addrInfoResponses); }
void daemonizeMe(const char *cmd) { int fd, fd0, fd1, fd2; pid_t pid; struct rlimit rlMaxFd; struct sigaction sa; /* * Clear file creation mask. */ umask(0); /* * Get maximum number of file descriptors. */ SYSCALL_CHECK(getrlimit(RLIMIT_NOFILE, &rlMaxFd)); /* * Become a session leader to lose controlling TTY. */ SYSCALL_CHECK(pid = fork()); if (pid != 0) /* parent */ exit(0); setsid(); /* * Ensure future opens won't allocate controlling TTYs. */ sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; SYSCALL_CHECK(sigaction(SIGHUP, &sa, NULL)); SYSCALL_CHECK(pid = fork()); if (pid != 0) /* parent */ exit(0); /* * Change the current working directory to the root so * we won't prevent file systems from being unmounted. */ SYSCALL_CHECK(chdir("/")); /* * Close all open file descriptors. If there's no maximum file * descriptor, use MAX_FD_DEFAULT. */ if (rlMaxFd.rlim_max == RLIM_INFINITY) rlMaxFd.rlim_max = MAX_FD_DEFAULT; for (fd = 0; fd < rlMaxFd.rlim_max; fd++) close(fd); // ignore error (fd not open) /* * Attach file descriptors 0, 1, and 2 to /dev/null. */ SYSCALL_CHECK(fd0 = open("/dev/null", O_RDWR)); fd1 = dup(0); fd2 = dup(0); /* * Initialize the log file. Remember that you can read the system * log with dmesg(1). */ openlog(cmd, LOG_CONS, LOG_DAEMON); if (fd0 != 0 || fd1 != 1 || fd2 != 2) { syslog(LOG_ERR, "unexpected file descriptors %d %d %d", fd0, fd1, fd2); exit(1); } }
bool CFile::Open(const wxString& fileName, OpenMode mode, int accessMode) { MULE_VALIDATE_PARAMS(!fileName.IsEmpty(), wxT("CFile: Cannot open, empty path.")); #ifdef __linux__ int flags = O_BINARY | O_LARGEFILE; #else int flags = O_BINARY; #endif switch ( mode ) { case read: flags |= O_RDONLY; break; case write_append: if (CheckFileExists(fileName)) { flags |= O_WRONLY | O_APPEND; break; } //else: fall through as write_append is the same as write if the // file doesn't exist case write: flags |= O_WRONLY | O_CREAT | O_TRUNC; break; case write_excl: flags |= O_WRONLY | O_CREAT | O_EXCL; break; case read_write: flags |= O_RDWR; break; } if (IsOpened()) { Close(); } // When opening files, we will always first try to create an ANSI file name, // even if that means an extended ANSI file name. Only if it is not possible // to do that, we fall back to UTF-8 file names. This is unicode safe and is // the only way to guarantee that we can open any file in the file system, // even if it is not an UTF-8 valid sequence. // // Test if it is possible to use an ANSI name Unicode2CharBuf tmpFileName = unicode2char(fileName); if (tmpFileName) { // Use an ANSI name m_fd = open(tmpFileName, flags, accessMode); } if (m_fd == fd_invalid) { // Wrong conversion or can't open. // Try an UTF-8 name m_fd = open(unicode2UTF8(fileName), flags, accessMode); } m_filePath = fileName; SYSCALL_CHECK(m_fd != fd_invalid, wxT("opening file")); return IsOpened(); }