/** * rootPath: queue��root·�� * queueName: * maxFileSize: �����ļ���С */ CFileQueue::CFileQueue(const char *rootPath, const char *queueName, int maxFileSize) { char tmp[256]; sprintf(tmp, "%s/%s", rootPath, queueName); m_queuePath = strdup(tmp); m_maxFileSize = maxFileSize; m_infoFd = -1; m_readFd = -1; m_writeFd = -1; // ����Ŀ¼ if (CFileUtil::mkdirs(m_queuePath) == false) { TBSYS_LOG(ERROR, "����Ŀ¼ʧ��: %s", m_queuePath); return; } // ͷ�ļ� sprintf(tmp, "%s/header.dat", m_queuePath); m_infoFd = open(tmp, O_RDWR|O_CREAT, 0600); if (m_infoFd == -1) { TBSYS_LOG(ERROR, "�����ļ�ʧ��: %s", tmp); return; } // ����ͷ��Ϣ if (read(m_infoFd, &m_head, sizeof(m_head)) != sizeof(m_head)) { memset(&m_head, 0, sizeof(m_head)); m_head.read_seqno = 1; m_head.write_seqno = 1; } // ����д if (openWriteFile() == EXIT_FAILURE) { return; } struct stat st; if (fstat(m_writeFd, &st) == 0) { m_head.write_filesize = st.st_size; } // �� if (openReadFile() == EXIT_FAILURE) { close(m_writeFd); m_writeFd = -1; return; } // �ָ���¼ if (m_head.exit_status == 0) { recoverRecord(); } m_head.exit_status = 0; }
/** * rootPath: queue的root路径 * queueName: * maxFileSize: 最大文件大小 */ CFileQueue::CFileQueue(char *rootPath, char *queueName, int maxFileSize) { char tmp[256]; sprintf(tmp, "%s/%s", rootPath, queueName); m_queuePath = strdup(tmp); m_maxFileSize = maxFileSize; m_infoFd = -1; m_readFd = -1; m_writeFd = -1; // 创建目录 if (CFileUtil::mkdirs(m_queuePath) == false) { SYS_LOG(ERROR, "create directory failed: %s", m_queuePath); return; } // 头文件 sprintf(tmp, "%s/header.dat", m_queuePath); m_infoFd = open(tmp, O_RDWR|O_CREAT, 0644); if (m_infoFd == -1) { SYS_LOG(ERROR, "open file failed: %s", tmp); return; } // 读进头信息 if (read(m_infoFd, &m_head, sizeof(m_head)) != sizeof(m_head)) { memset(&m_head, 0, sizeof(m_head)); m_head.read_seqno = 1; m_head.write_seqno = 1; } // 打开写 if (openWriteFile() == CTFO_EXIT_FAILURE) { return; } struct stat st; if (fstat(m_writeFd, &st) == 0) { m_head.write_filesize = st.st_size; } // 打开读 if (openReadFile() == CTFO_EXIT_FAILURE) { close(m_writeFd); m_writeFd = -1; return; } // 恢复记录 if (m_head.exit_status == 0) { recoverRecord(); } m_head.exit_status = 0; }