<commit_before> /* 
 * File:   wbt_file.c
 * Author: Fcten
 *
 * Created on 2014年11月12日, 上午10:44
 */

#include "../event/wbt_event.h"
#include "wbt_module.h"
#include "wbt_memory.h"
#include "wbt_log.h"
#include "wbt_file.h"
#include "wbt_rbtree.h"
#include "wbt_time.h"
#include "wbt_gzip.h"

wbt_module_t wbt_module_file = {
    wbt_string("file"),
    wbt_file_init,
    wbt_file_exit
};

wbt_file_t tmp;

#define WBT_MAX_OPEN_FILES 1024

wbt_rb_t wbt_file_rbtree;

extern wbt_atomic_t wbt_wating_to_exit;

wbt_str_t MIME_TYPE[][2] = {
    { wbt_string("bmq"),  wbt_string("text/plain") },
    { wbt_string("html"), wbt_string("text/html")  },
    { wbt_string("css"),  wbt_string("text/css")   },
    { wbt_string("js"),   wbt_string("application/javascript") },
    { wbt_string("png"),  wbt_string("image/png")  },
    { wbt_string("jpg"),  wbt_string("image/jpeg") },
    { wbt_string("gif"),  wbt_string("image/gif")  },
    { wbt_string("json"), wbt_string("application/json") },
    { wbt_string("xml"),  wbt_string("text/xml")   },
    { wbt_string("txt"),  wbt_string("text/plain") },
    { wbt_string("jpeg"), wbt_string("image/jpeg") },
    { wbt_string("svg"),  wbt_string("image/svg+xml") },
    { wbt_string("ico"),  wbt_string("image/x-icon") }
};

wbt_mime_t wbt_file_mime_type(wbt_str_t * file_path) {
    wbt_mime_t i;
    wbt_str_t ext;

    for( i = MIME_TEXT ; i < MIME_LENGTH ; i++ ) {
        if( file_path->len <= MIME_TYPE[i][0].len + 1 ) {
            continue;
        }
        if( file_path->str[file_path->len - MIME_TYPE[i][0].len - 1] != '.' ) {
            continue;
        }
        ext.str = file_path->str + file_path->len - MIME_TYPE[i][0].len;
        ext.len = MIME_TYPE[i][0].len;
        if( wbt_strnicmp(&ext, &MIME_TYPE[i][0], MIME_TYPE[i][0].len ) == 0 ) {
            return i;
        }
    }
    
    return MIME_UNKNOWN;
}

void wbt_file_cleanup_recursive(wbt_rb_node_t *node) {
    /* 从叶节点递归处理至根节点 */
    if(node) {
        wbt_file_cleanup_recursive(node->left);
        wbt_file_cleanup_recursive(node->right);
        
        wbt_file_t * tmp_file = (wbt_file_t *)node->value.str;
        if( tmp_file->refer == 0 && wbt_cur_mtime - tmp_file->last_use_mtime > 10000 ) {
            wbt_log_debug("closed fd:%d %.*s\n", tmp_file->fd, node->key.len, node->key.str.s);
            wbt_close_file(tmp_file->fd);<commit_msg>Bugfix: 修复 MIME 判断<commit_after> /* 
 * File:   wbt_file.c
 * Author: Fcten
 *
 * Created on 2014年11月12日, 上午10:44
 */

#include "../event/wbt_event.h"
#include "wbt_module.h"
#include "wbt_memory.h"
#include "wbt_log.h"
#include "wbt_file.h"
#include "wbt_rbtree.h"
#include "wbt_time.h"
#include "wbt_gzip.h"

wbt_module_t wbt_module_file = {
    wbt_string("file"),
    wbt_file_init,
    wbt_file_exit
};

wbt_file_t tmp;

#define WBT_MAX_OPEN_FILES 1024

wbt_rb_t wbt_file_rbtree;

extern wbt_atomic_t wbt_wating_to_exit;

wbt_str_t MIME_TYPE[][2] = {
    { wbt_string("bmq"),  wbt_string("text/plain") },
    { wbt_string("html"), wbt_string("text/html")  },
    { wbt_string("css"),  wbt_string("text/css")   },
    { wbt_string("js"),   wbt_string("application/javascript") },
    { wbt_string("png"),  wbt_string("image/png")  },
    { wbt_string("jpg"),  wbt_string("image/jpeg") },
    { wbt_string("gif"),  wbt_string("image/gif")  },
    { wbt_string("json"), wbt_string("application/json") },
    { wbt_string("xml"),  wbt_string("text/xml")   },
    { wbt_string("txt"),  wbt_string("text/plain") },
    { wbt_string("jpeg"), wbt_string("image/jpeg") },
    { wbt_string("svg"),  wbt_string("image/svg+xml") },
    { wbt_string("ico"),  wbt_string("image/x-icon") }
};

wbt_mime_t wbt_file_mime_type(wbt_str_t * file_path) {
    wbt_mime_t i;
    wbt_str_t ext;

    for( i = MIME_HTML ; i < MIME_LENGTH ; i++ ) {
        if( file_path->len <= MIME_TYPE[i][0].len + 1 ) {
            continue;
        }
        if( file_path->str[file_path->len - MIME_TYPE[i][0].len - 1] != '.' ) {
            continue;
        }
        ext.str = file_path->str + file_path->len - MIME_TYPE[i][0].len;
        ext.len = MIME_TYPE[i][0].len;
        if( wbt_strnicmp(&ext, &MIME_TYPE[i][0], MIME_TYPE[i][0].len ) == 0 ) {
            return i;
        }
    }
    
    return MIME_UNKNOWN;
}

void wbt_file_cleanup_recursive(wbt_rb_node_t *node) {
    /* 从叶节点递归处理至根节点 */
    if(node) {
        wbt_file_cleanup_recursive(node->left);
        wbt_file_cleanup_recursive(node->right);
        
        wbt_file_t * tmp_file = (wbt_file_t *)node->value.str;
        if( tmp_file->refer == 0 && wbt_cur_mtime - tmp_file->last_use_mtime > 10000 ) {
            wbt_log_debug("closed fd:%d %.*s\n", tmp_file->fd, node->key.len, node->key.str.s);
            wbt_close_file(tmp_file->fd);
{'fraction_non_alphanumeric': 0.1746899306285474, 'fraction_numerical': 0.013453857473197394, 'mean_word_length': 3.1518324607329844, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5592302', 'n_tokens_mistral': 2066, 'n_tokens_neox': 2024, 'n_words': 262}