fssim_core.c

来自「最新MTK手机软件源码」· C语言 代码 · 共 1,826 行 · 第 1/5 页

C
1,826
字号

            hDir = CreateFile(drv_name, GENERIC_READ | GENERIC_WRITE,
                              FILE_SHARE_READ, NULL, OPEN_EXISTING,
                              FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);

            if (hDir != INVALID_HANDLE_VALUE) {

                CloseHandle(hDir);

            } else {

                /* the directory doesn't exist */

                /* try to create the directory */

                if (CreateDirectory(drv_name, NULL) == 0) {

                    retval = GetLastError();

                    fssim_printf(("FSSim_Init(): fail to create directory %s, error code = %u\n", drv_name, retval));

                    ASSERT(0);
                }
            }

        } /* for (i = 0; i < FSSIM_MAX_DRIVE; i++) */

        /* initialize fssim_drvmap */

        memset(&fssim_drvmap, 0, sizeof(FSSIM_DRVMAP_T));

        /* initialize fssim_file */

        memset(&fssim_file, 0, sizeof(FSSIM_FILE_T) * (1 + FSSIM_MAX_FILE) );

        /* initialize fssim_vfgen 
         * XXX: Simulator use dynamic array for virtual filename generate and lookup
         * XXX: while there's no such table on target platform.
         */
        memset(&fssim_vfgen, 0, sizeof(FSSIM_VFTABLE_T) );
        fssim_vfgen.allocat_size = 8;
        fssim_vfgen.data = (FSSIM_VIRTUAL_FILENAME_T *)
            malloc( sizeof(FSSIM_VIRTUAL_FILENAME_T) * fssim_vfgen.allocat_size );
        ASSERT( fssim_vfgen.data != NULL );
        memset(fssim_vfgen.data, 0, sizeof(FSSIM_VIRTUAL_FILENAME_T) * fssim_vfgen.allocat_size );

        isinit = 1;
    }
    __finally {

        if (isinit == 0) {

            fssim_printf(("FSSIM error: fail to initialize\n"));

            ASSERT(0);
        }

        FS_MappingDrive(FSSIM_SYSTEM_DRIVE, FSSIM_REMAP_SYSTEM_DRIVE);
        fssim_get_drive_watermark( &(fssim_drvinfo[0]) );
    }
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_alloc_fh                                                   */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is used to allocate a new file handle.             */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      file handle                                                      */
/*                                                                       */
/*************************************************************************/
static int fssim_alloc_fh()
{
    int i;
    static unsigned int fhseq = 0x1077;

    if (fssim_mutex == NULL) {
        fssim_init();
    }

    fssim_take_mutex(fssim_mutex);

    for (i = 1; i <= FSSIM_MAX_FILE; i++, fhseq++) {

        if (fssim_file[i].state == STATE_FREE) {

            fssim_file[i].state  = STATE_INUSE;
            fssim_file[i].unique = fhseq++;
            fssim_file[i].taskid = fssim_get_task_self_id();
            fssim_file[i].drive  = -1;

            break;
        }
    }

    fssim_give_mutex(fssim_mutex);

    if (i == (FSSIM_MAX_FILE + 1)) {

        /* file handles are exhausted */

        fssim_printf(("fssim_new_fh(): file handles are exhausted\n"));

        ASSERT(0);
        return -1;
    } else
        return i;
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_free_fh                                                    */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is used to free a new file handle.                 */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      fh  -  file handle                                               */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/*************************************************************************/
static void fssim_free_fh(const int fh)
{
    if (fssim_mutex == NULL) {
        fssim_init();
    }

    fssim_take_mutex(fssim_mutex);

    if (fssim_file[fh].state == STATE_INUSE)
        ASSERT(fssim_file[fh].handle == INVALID_HANDLE_VALUE);

    memset( &(fssim_file[fh]), 0, sizeof(FSSIM_FILE_T));

    fssim_file[fh].state = STATE_FREE;

    fssim_give_mutex(fssim_mutex);

}



/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_conv_fn                                                    */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is used to convert a file name.                    */
/*      (x:\  ==>  DRIVEx\)                                              */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      dst  -  destination buffer                                       */
/*      src  -  source file name                                         */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/*************************************************************************/
int fssim_get_default_drive(void);
static void fssim_conv_fn(TCHAR *dst, const TCHAR *src)
{
    TCHAR *pch, *pchdst;

    ASSERT(dst != NULL && src != NULL);

    /* save dst */
    pchdst = dst;

    if (wcslen(src) == 2 && _wcsicmp(src + 1, L":") == 0) {

        /* only drive name is specified (X: only) */

        /* copy root directory name first */

        wcscpy(dst, fssim_root_dir);

        dst += wcslen(dst);

        /* copy drive directory name (DRIVE_X) */

        wcscpy(dst, FSSIM_DRIVE_DIR);

        dst += wcslen(dst);

        *(dst - 2) = *src;
    } else if (wcsncmp(src + 1, L":\\", wcslen(L":\\")) != 0) {

        /* no drive name is specified , must be relative path */

        /* get default drive and */
        wcscpy(dst, fssim_drvinfo[fssim_get_default_drive()].dir_name);

        dst += wcslen(dst);

        /* check if \\ append ? */
        if (*(dst - 2) != L'\\') {

            *dst++ = L'\\';
        }

        /* copy directly */
        wcscpy(dst, src);

    } else {

        /* copy root directory name first */

        wcscpy(dst, fssim_root_dir);

        dst += wcslen(dst);

        /* copy drive directory name (DRIVE_X) */

        wcscpy(dst, FSSIM_DRIVE_DIR);

        dst += wcslen(dst);

        pch = (TCHAR *)src;

        *(dst - 2) = *pch;

        pch += wcslen(L"X:\\");

        /* copy source file name next */

        wcscpy(dst, pch);
    }

    /* resotre dst */
    dst = pchdst;

    pch = wcsstr(dst, FSSIM_DRIVE_DIR_PREFIX);

    /* check if the drive name exists */
    if (pch != NULL) {

        if (fssim_drvmap.original != 0) {

            /* try to map the drive letter */

            if (!wcsnicmp(pch + wcslen(FSSIM_DRIVE_DIR_PREFIX), &(fssim_drvmap.mapped), 1)) {

                /* the drive name is equal to the to-be-mapped drive letter */

                wcsncpy(pch + wcslen(FSSIM_DRIVE_DIR_PREFIX), &(fssim_drvmap.original), 1);
            }
        }
    }
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_retrieve_fn                                                */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is used to retrieve a file name.                   */
/*      (DRIVEx\  ==>  x:\)                                              */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      filename  -  destination buffer                                  */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/*************************************************************************/
static void fssim_retrieve_fn(TCHAR *filename)
{
    TCHAR strbuf[MAX_PATH], *pch;

#ifdef DEBUG_FSSIM
    fssim_printf(("fssim_retrieve_fn(): filename = "));
    fssim_print_str(filename);
#endif  /* DEBUG_FSSIM */

    /* build a string of pattern "\\DRIVE_" */

    wcscpy(strbuf, L"\\");

    wcscpy(strbuf + 1, FSSIM_DRIVE_DIR);

    wcscpy(strbuf + wcslen(strbuf) - 2, L"\0");   /* remove "X" and "//" */

#ifdef DEBUG_FSSIM
    fssim_printf(("fssim_retrieve_fn(): strbuf = "));
    fssim_print_str(strbuf);
#endif  /* DEBUG_FSSIM */

    /* find "\\DRIVE_" */

    pch = wcsstr(filename, strbuf);

    if (pch == NULL) {

        fssim_printf(("fssim_retrieve_fn(): cannot find a legal drive name\n"));

        fssim_print_str(filename);

        ASSERT(0);
    } else {

#ifdef DEBUG_FSSIM
        fssim_printf(("fssim_retrieve_fn(): pch = "));
        fssim_print_str(pch);
#endif  /* DEBUG_FSSIM */

        /* move pch to the string of drive name (C or D or E or...) */

        pch += wcslen(strbuf);

#ifdef DEBUG_FSSIM
        fssim_printf(("fssim_retrieve_fn(): pch = "));
        fssim_print_str(pch);
#endif  /* DEBUG_FSSIM */

        /* copy the drive name */
        filename[0] = pch[0];
        wcscpy(filename + 1, L":");

        /* copy the director name after the drive name */

        wcscpy(filename + wcslen(filename), pch + 1);

        if (!_wcsicmp(filename + wcslen(filename) - 1, L":")) {

            /* there is no "\\" following ":" */

            /* append ":" */

            wcscpy(filename + wcslen(filename), L"\\");
        }

#ifdef DEBUG_FSSIM
        fssim_printf(("fssim_retrieve_fn(): retrieving filename = "));
        fssim_print_str(filename);
#endif  /* DEBUG_FSSIM */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?