📄 pthread_diskm.c
字号:
/* set tabstop=4 *//******************************************************************************** * * * Copyright(C) 2004 Penta-Micro * * * * ALL RIGHT RESERVED * * * * This software is the property of Penta-Micro and is furnished under * * license by Penta-Micro. This software may be used only in accordance * * with the terms of said license. This copyright notice may not be * * removed, modified or obliterated without the prior written permission * * of Penta-Micro. * * * * This software may not be copyed, transmitted, provided to or otherwise * * made available to any other person, company, corporation or other entity * * except as specified in the terms of said license. * * * * No right, title, ownership or other interest in the software is hereby * * granted or transferred. * * * * The information contained herein is subject to change without notice and * * should not be construed as a commitment by Penta-Micro. * * * ******************************************************************************** MODULE NAME: PTHREAD_DISKM.C REVISION HISTORY: Date Ver Name Description ---------- --- --------------------- ----------------------------------------- 09/06/2004 2.0 CheulBeck(whitefe) Created 07/20/2005 2.1 JiGwanKang(xchannel) Modified ............................................................................... DESCRIPTION: This Module contains functions for Disk Management. ...............................................................................*/ /** ************************************************************************* ** ** includes ** ************************************************************************* **/#include <ctype.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <sys/vfs.h> /* statfs() */#include <sys/stat.h> /* mkdir() */#include <dirent.h> /* readdir() */#include "main.h"#include "pthread_diskm.h" /** ************************************************************************* ** ** defines ** ************************************************************************* **/#define m_DEBUG(format, args...) printf(format, ## args);fflush(stdout);//#define m_DEBUG(format, args...) #define m_MSG(format, args...) printf(format, ## args);fflush(stdout);#define m_ERROR(format, args...) printf(format, ## args);fflush(stdout); /** ************************************************************************* ** ** typedefs ** ************************************************************************* **/ /** ************************************************************************* ** ** globals ** ************************************************************************* **/extern PTHREAD_STATE *gp_state_thread;extern SETUP_PARAM *gp_setup_param;extern UNS8 MAIN_DB_NAME[32];extern UNS8 TEMP_DB_NAME[32];extern UNS32 gv_cur_disk_id;extern UNS16 g_rec_time_list[]; /** ************************************************************************* ** ** locals ** ************************************************************************* **//** ************************************************************************* ** ** forward declarations ** ************************************************************************* **/void *pthread_diskm(void *args){ S8 str_rec_dir[80]; m_DEBUG("\t!!! pthread DISKM !!!\n"); DISK_PARAM *pdp; STATE_DISKM *psdm; pdp = &gp_setup_param->sys.disk; psdm = &gp_state_thread->state_diskm; /* 2nd. check hard disk attached at current system */ check_disk_attached(); /* 3rd. check free usage */ if (check_disk_full() == TRUE) { m_MSG("\t!!! DISK SPACE FULL !!!\n"); if (pdp->over_wt_flag) { /* 4th. if disk is full, delete file */ if (delete_files_until_disk_sufficient() == FAILURE) { m_ERROR("pthread_diskm.c:error In function 'delete_files_until_disk_sufficient'\n"); } } /* for hdd overwrite flag */ else psdm->state = FULL; } else { m_MSG("\t!!! DISK SPACE SUFFICIENT !!!\n"); } while (1) { if (gp_state_thread->state_enc.state == BUSY) { if (check_disk_full() == TRUE) { m_MSG("\t!!! DISK SPACE FULL !!!\n"); if (pdp->over_wt_flag) { if (delete_files_until_disk_sufficient() == FAILURE) { m_ERROR("pthread_diskm.c:error In function 'delete_files_until_disk_sufficient'\n"); } } /* for hdd overwrite flag */ else psdm->state = FULL; } else { m_MSG("\t!!! DISK SPACE SUFFICIENT !!!\n"); } } sleep(60); } /* end of while (!QUIT_SDVR_MAIN) */ return 0;}void check_disk_attached(void){ S32 i; S8 locate_disk[12]; struct statfs s; DISK_PARAM *pdp; pdp = &gp_setup_param->sys.disk; for (i = 0; i < MAX_DISK_NUM; i++) { if (i == 0) { strcpy(locate_disk, DISKA); } else if (i == 1) { strcpy(locate_disk, DISKB); } else if (i == 2) { strcpy(locate_disk, DISKC); } else if (i == 3) { strcpy(locate_disk, DISKD); } if (check_mount_for_disk(locate_disk) == TRUE) { pdp->alive_hdd[i] = TRUE; m_MSG("\t!!! MOUNTED /HDD%c !!!\n", 'A' + i); } else { pdp->alive_hdd[i] = FALSE; } }}BOOL check_mount_for_disk(S8 * pattern){ S32 i = 0; S32 file_loc; S8 cmp_buf[13]; FILE *fd; if ((fd = fopen("/proc/mounts", "rb")) < 0) { m_ERROR("pthread_diskm.c:error In function 'fopen'\n"); return FALSE; } file_loc =0; for (i = 0; i < 1000; i++) { fgets(cmp_buf, strlen(pattern)+1, fd); if (strcmp(cmp_buf, pattern) == 0) { fclose(fd); return TRUE; } else { file_loc++; } fseek(fd, file_loc, SEEK_SET); } fclose(fd); return FALSE;}BOOL check_disk_full(void){ S32 i; S32 bsize, tsize, fsize, asize, min_space; S8 locate_disk[10]; struct statfs s[MAX_DISK_NUM]; DISK_PARAM *pdp; pdp = &gp_setup_param->sys.disk; for (i = 0; i < MAX_DISK_NUM; i++) { if (pdp->alive_hdd[i] == TRUE) { GET_DIR_NAME(&locate_disk, i); statfs(locate_disk, &s[i]); bsize = s[i].f_bsize >> 10; /* in kbytes */ tsize = (bsize * s[i].f_blocks) >> 10; /* in MByte */ fsize = (bsize * s[i].f_bfree) >> 10; /* in MByte */ asize = (bsize * s[i].f_bavail) >> 10; /* in MByte */ /* free space(5%) secure for virtual memory and log files(ext3) */ /* minimun space = 5% space + current recoding size */ min_space = (tsize * 0.05) + (((g_rec_time_list[pdp->slice_time])*60)*MAXIMUM_SEC_MB); if (fsize > min_space) { m_MSG("\t!!! DISK[%c] USAGE TOTAL(%dMB), FREE(%dMB)\n", 'A'+i, tsize, asize); /* current recoding hard disk set */ gv_cur_disk_id = i; return FALSE; } } } return TRUE;}RETURN delete_files_until_disk_sufficient(){ if (delete_one_file() == FAILURE) { m_ERROR("pthread_diskm.c:error In function 'delete_one_file'\n"); return FALSE; } sleep(1); while(1) { if (check_disk_full() == TRUE) { if (delete_one_file() == FAILURE) { m_ERROR("pthread_diskm.c:error In function 'delete_one_file'\n"); return FAILURE; } } else { break; } } return SUCCESS;}RETURN delete_one_file(){ S32 i; S8 str_temp[40]; S8 str_delete[40]; FILE *fd; DB_RECORD pdr; STATE_DB *psdb; psdb = &gp_state_thread->state_db; /* LOCK "MAIN_DB" file */ sem_wait(&psdb->search_sem); fd = fopen(MAIN_DB_NAME, "rb"); if (fd == NULL) { m_ERROR("pthread_diskm.c:error In function 'fopen'\n"); return FAILURE; } /* get delete struct Info */ fread(&pdr, sizeof(S8), sizeof(DB_RECORD), fd); /* Delete DB file close */ fclose(fd); /* UNLOCK "MAIN_DB" file */ sem_post(&psdb->search_sem); m_MSG("\t!!! DELETE : %s\n", pdr.fname); /* delete one file */ if (unlink(pdr.fname) == -1) { perror("unlink err"); m_ERROR("pthread_diskm.c:error In function 'unlink'\n"); } /* delete record DB Info */ if (delete_db_info(pdr) == FAILURE) { m_ERROR("pthread_diskm.c:error In function 'delete_db_info'\n"); return FAILURE; } return SUCCESS; }RETURN delete_db_info(DB_RECORD pdr){ S32 count; FILE *fd, *fd_t; DB_RECORD p_dr; UNS8 sub_db_name[32], temp_db_name[32]; STATE_DB *psdb; psdb = &gp_state_thread->state_db; /* LOCK "DB FILE" file */ sem_wait(&psdb->search_sem); if (!(fd=fopen(MAIN_DB_NAME, "rb"))) { perror("pthread_diskm.c:error In function 'main db fopen'"); return FAILURE; } /* temporary file open */ strcpy(temp_db_name, TEMP_DB_NAME); if (!(fd_t = fopen(temp_db_name, "wb"))) { m_ERROR("pthread_diskm.c:error In function 'temp db fopen'\n"); return FAILURE; } /* file list condition check */ count = 0; while (fread(&p_dr, sizeof(S8), sizeof(DB_RECORD), fd) > 0) { if ( !strncmp(p_dr.fname, pdr.fname, strlen(pdr.fname)) ) { count ++; m_DEBUG("\t!!! DELETE MAIN DB INFO : %s\n", pdr.fname); } else { /* move to main db location pointer */ if (p_dr.prev_db_fp) { p_dr.prev_db_fp = p_dr.prev_db_fp - sizeof(DB_RECORD) * count; } p_dr.next_db_fp = p_dr.next_db_fp - sizeof(DB_RECORD) * count; #ifndef LOOPTHROUGH_MODE fwrite(&p_dr, sizeof(DB_RECORD), sizeof(S8), fd_t);#endif } } /* Temporary DB file close */ fclose(fd_t); /* Delete main DB file close */ fclose(fd); /* original main db file delete */ if (remove(MAIN_DB_NAME) == -1) { m_ERROR("pthread_diskm.c:error In function 'main db remove'\n"); } /* update main db file remove */ if (rename(temp_db_name, MAIN_DB_NAME) == -1) { m_ERROR("pthread_diskm.c:error In function 'main db rename'\n"); } /* sub db file open */ SUB_SEARCH_DB(&sub_db_name, (pdr.fname[4] - 'a')); if (!(fd=fopen(sub_db_name, "rb"))) { perror("pthread_diskm.c:error In function 'sub db fopen'"); /* UNLOCK "DB FILE" file */ sem_post(&psdb->search_sem); return FAILURE; } /* temporary file open */ TEMP_SEARCH_DB(&temp_db_name, (pdr.fname[4] - 'a')); if (!(fd_t = fopen(temp_db_name, "wb"))) { m_ERROR("pthread_diskm.c:error In function 'temp db fopen'\n"); /* UNLOCK "DB FILE" file */ sem_post(&psdb->search_sem); return FAILURE; } /* file list condition check */ count = 0; while (fread(&p_dr, sizeof(S8), sizeof(DB_RECORD), fd) > 0) { /* warnning : At filename length conditional comparison */ if ( !strncmp(p_dr.fname, pdr.fname+6, 22) ) { count ++; m_DEBUG("\t!!! DELETE SUB DB INFO : %s\n", pdr.fname); } else { /* move to db location pointer */ if (p_dr.prev_db_fp) { p_dr.prev_db_fp = p_dr.prev_db_fp - sizeof(DB_RECORD) * count; } p_dr.next_db_fp = p_dr.next_db_fp - sizeof(DB_RECORD) * count; #ifndef LOOPTHROUGH_MODE fwrite(&p_dr, sizeof(DB_RECORD), sizeof(S8), fd_t);#endif } } /* Temporary DB file close */ fclose(fd_t); /* Delete sub DB file close */ fclose(fd); /* original sub db file delete */ if (remove(sub_db_name) == -1) { m_ERROR("pthread_diskm.c:error In function 'sub db remove'\n"); } /* update sub db file remove */ if (rename(temp_db_name, sub_db_name) == -1) { m_ERROR("pthread_diskm.c:error In function 'sub db rename'\n"); } /* UNLOCK "DB FILE" file */ sem_post(&psdb->search_sem); return SUCCESS; }/* end of pthread_diskm.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -