⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stream.c

📁 WinCE5.0部分核心源码
💻 C
📖 第 1 页 / 共 5 页
字号:
 *      ERROR_SUCCESS (0) if successful, non-zero if not
 *
 *  NOTES
 *      As an optimization, OpenStream remembers the physical block and
 *      within that block of the stream's DIRENTRY.  This will save
 *      us from having to perform a OpenStream/FindNext combination
 *      in CommitStream every time we need to update a stream's DIRENTRY.
 *
 *      As an added optimization, OpenStream also records the parent's OID
 *      if a parent stream was specified when the stream was created.  Thus,
 *      CommitStream has all the information it needs to post a CEOID_CHANGED
 *      message if the stream changed, again without having to open the
 *      parent stream.
 */

DWORD CommitStream(PDSTREAM pstm, BOOL fAll)
{
    DWORD dwError = ERROR_SUCCESS;

    ASSERT(OWNCRITICALSECTION(&pstm->s_cs));

    // NOTE: CommitStream refuses to update an unmounted stream because
    // the volume the stream was associated with may have been unmounted and
    // RECYCLED, in which case this stream is dead and its directory entry
    // can never be safely updated.  The stream will eventually go away when
    // all the apps with handles to the stream close their handles.

    if (!(pstm->s_flags & STF_UNMOUNTED)) {

        // Commit all dirty buffers associated with this stream.  There
        // is no return value from this function.  Any held stream buffer will
        // be released as well.

        if (!fAll)
            dwError = ReleaseStreamBuffer(pstm, FALSE);
        else
            dwError = CommitAndReleaseStreamBuffers(pstm);

        // The rest of this is for non-FAT, non-root, non-directory
        // streams only (ie, streams with DIRENTRY's that should be updated).

        if (!(pstm->s_flags & STF_VOLUME)) {

            SYSTEMTIME stLocal;

            GetLocalTime(&stLocal);

            // A stream gets a new access time if it's a new day, unless
            // STF_ACCESS_TIME is set, in which case someone has already set a
            // specific access time.

            
            if ((pstm->s_pvol->v_flags & VOLF_UPDATE_ACCESS) && !(pstm->s_flags & STF_ACCESS_TIME)) {

                SYSTEMTIME stTmp;
                FILETIME ftTmp1, ftTmp2;

                stTmp = stLocal;
                stTmp.wDayOfWeek = stTmp.wHour = stTmp.wMinute = stTmp.wSecond = stTmp.wMilliseconds = 0;

                // Now that I've truncated the local system time to midnight,
                // I need to switch back to non-local time to perform the
                // necessary comparison.

                DEBUGMSG(ZONE_STREAMS && ZONE_LOGIO,(DBGTEXT("FATFS!CommitStream: current (truncated) local system time: y=%d,m=%d,d=%d,h=%d,m=%d,s=%d\r\n"), stTmp.wYear, stTmp.wMonth, stTmp.wDay, stTmp.wHour, stTmp.wMinute, stTmp.wSecond));
                SystemTimeToFileTime(&stTmp, &ftTmp1);

                DEBUGMSG(ZONE_STREAMS && ZONE_LOGIO,(DBGTEXT("FATFS!CommitStream: current (truncated) local file time: %08x,%08x\r\n"), ftTmp1.dwLowDateTime, ftTmp1.dwHighDateTime));
                LocalFileTimeToFileTime(&ftTmp1, &ftTmp2);

                DEBUGMSG(ZONE_STREAMS && ZONE_LOGIO,(DBGTEXT("FATFS!CommitStream: current (truncated) file time: %08x,%08x\r\n"), ftTmp2.dwLowDateTime, ftTmp2.dwHighDateTime));
                if (pstm->s_ftAccess.dwLowDateTime != ftTmp2.dwLowDateTime ||
                    pstm->s_ftAccess.dwHighDateTime != ftTmp2.dwHighDateTime) {

                    pstm->s_ftAccess = ftTmp2;
                    pstm->s_flags |= STF_ACCESS_TIME | STF_DIRTY_INFO;
                }
            }

            if (pstm->s_flags & STF_DIRTY) {

                PBUF pbuf = NULL;

                // We don't specify a stream for FindBuffer, because we're
                // not trying to find a buffer for THIS stream, but rather its
                // directory's stream.

                // Also note this means we need to call CommitBuffer and UnholdBuffer
                // when we're done (instead of the usual ModifyStreamBuffer).

                if (pstm->s_blkDir == INVALID_BLOCK) {
                    DEBUGMSGBREAK(TRUE,(DBGTEXT("FATFS!CommitStream: invalid blkDir!\r\n")));
                    dwError = ERROR_INVALID_BLOCK;
                }
                else
#ifdef TFAT
                    if (!pstm->s_pvol->v_fTfat)
#endif        
                        dwError = FindBuffer(pstm->s_pvol, pstm->s_blkDir, NULL, FALSE, &pbuf);

                if (dwError == ERROR_SUCCESS) 
                {
                    PDIRENTRY pde;
                    DWORD     clusFirst;
                
#ifdef TFAT
                    if (pstm->s_pvol->v_fTfat) {                       
                        DWORD     blkDir;

                        LockFAT (pstm->s_pvol);

                        blkDir = pstm->s_blkDir;
                        ASSERT(pstm->s_offDir<pstm->s_pvol->v_cbClus);
                        
                        // A dir cluster is being modified, lets clone it first
                        CloneDirCluster(pstm->s_pvol, pstm->s_pstmParent, blkDir, &blkDir);

                        if (!(dwError = FindBuffer(pstm->s_pvol, blkDir, NULL, FALSE, &pbuf)))
                        {
                            pde = (PDIRENTRY)(pbuf->b_pdata + pstm->s_offDir);
                            dwError = ModifyBuffer(pbuf, pde, sizeof(DIRENTRY));
                        }

                        UnlockFAT (pstm->s_pvol);                       
                    }
                    else 
#endif
                    {
                        pde = (PDIRENTRY)(pbuf->b_pdata + pstm->s_offDir);
                        dwError = ModifyBuffer(pbuf, pde, sizeof(DIRENTRY));               
                    }

                    if (!dwError) {

                        pstm->s_attr |= ATTR_ARCHIVE;
                        pde->de_attr = pstm->s_attr;

                        pde->de_size = pstm->s_size;

                        clusFirst = pstm->s_clusFirst;
                        if (clusFirst == UNKNOWN_CLUSTER)
                            clusFirst = NO_CLUSTER;
                        SETDIRENTRYCLUSTER(pstm->s_pvol, pde, clusFirst);

                        if (pstm->s_flags & STF_CREATE_TIME) {
                            FileTimeToDOSTime(&pstm->s_ftCreate,
                                            &pde->de_createdDate,
                                            &pde->de_createdTime,
                                            &pde->de_createdTimeMsec);
                            pstm->s_flags &= ~STF_CREATE_TIME;
                        }

                        // A dirty data stream always gets a new write time, unless
                        // STF_WRITE_TIME is set, in which case someone has already set a
                        // specific write time.

                        if ((pstm->s_flags & (STF_DIRTY_DATA | STF_WRITE_TIME)) == STF_DIRTY_DATA) {

                            // We could simply call GetSystemTimeAsFileTime,
                            // with s_ftWrite as the destination, but if both
                            // last access and last write times are being updated,
                            // I prefer to insure that both times are identical.

                            FILETIME ftTmp;
                            SystemTimeToFileTime(&stLocal, &ftTmp);
                            LocalFileTimeToFileTime(&ftTmp, &pstm->s_ftWrite);
                            pstm->s_flags |= STF_WRITE_TIME;
                        }

                        if (pstm->s_flags & STF_WRITE_TIME) {
                            FileTimeToDOSTime(&pstm->s_ftWrite,
                                            &pde->de_date,
                                            &pde->de_time, NULL);
                            pstm->s_flags &= ~STF_WRITE_TIME;
                        }

                        if (pstm->s_flags & STF_ACCESS_TIME) {
                            FileTimeToDOSTime(&pstm->s_ftAccess,
                                            &pde->de_lastAccessDate, NULL, NULL);
                            pstm->s_flags &= ~STF_ACCESS_TIME;
                        }

                        if (fAll) {

                            BOOL fTempWriteThru = FALSE;            

                            // If stream is write-through, but directory stream is not, temporarily make it so that the 
                            // modified directory buffers will be write-through.            
                            if (pbuf->b_pstm && (pstm->s_flags & STF_WRITETHRU) && !(pbuf->b_pstm->s_flags & STF_WRITETHRU)) {
                                 fTempWriteThru = TRUE;
                                 pbuf->b_pstm->s_flags |= STF_WRITETHRU;
                            }
                            
                            dwError = CommitBuffer(pbuf, FALSE);
                            
                            if (fTempWriteThru && pbuf->b_pstm)
                                pbuf->b_pstm->s_flags &= ~STF_WRITETHRU;
                        }
                    }
                    // TEST_BREAK
                    PWR_BREAK_NOTIFY(41);

                    // Unhold the buffer that FindBuffer held for us.
                    // If FindBuffer failed, pbuf will be NULL.
                    if (pbuf) {
                        UnholdBuffer(pbuf);
                    }

#if 0
                    if (!dwError)
                        FILESYSTEMNOTIFICATION(pstm->s_pvol, DB_CEOID_CHANGED, 0, SHCNE_UPDATEITEM, &pstm->s_sid, &pstm->s_sidParent, NULL, NULL, NULL, DBGTEXTW("CommitStream"));
#endif                  

                    // Even if the buffer couldn't be written, the dirt has still
                    // moved from the stream's data structure to the buffer

                    pstm->s_flags &= ~(STF_DIRTY | STF_DIRTY_CLUS);
                }

                // If there was some error getting a buffer however, note that the
                // stream remains dirty.
            }
        }
    }
    else {

        // CommitAndReleaseStreamBuffers would have insured the release for us,
        // but since we can't do a commit, we'll have to insure the release ourselves.

        ReleaseStreamBuffer(pstm, FALSE);
        dwError = !(pstm->s_flags & STF_DIRTY)? ERROR_SUCCESS : ERROR_ACCESS_DENIED;
    }

    DEBUGMSG(ZONE_STREAMS || ZONE_ERRORS && dwError,(DBGTEXT("FATFS!CommitStream returned %d for '%.11hs'\r\n"), dwError, pstm->s_achOEM));
    return dwError;
}


/*  RewindStream
 *
 *  ENTRY
 *      pstm - pointer to DSTREAM
 *      pos - position within DSTREAM to rewind to, or INVALID_POS to reinitialize
 *
 *  EXIT
 *      None
 */

void RewindStream(PDSTREAM pstm, DWORD pos)
{
    
    if ((pos == INVALID_POS) || (pos < pstm->s_run.r_start) ||
        ((pstm->s_run.r_clusNext == UNKNOWN_CLUSTER) && (pstm->s_clusFirst != UNKNOWN_CLUSTER))) {
       pstm->s_run.r_start = 0;
       pstm->s_run.r_end = 0;
       pstm->s_run.r_clusThis = UNKNOWN_CLUSTER;
       pstm->s_run.r_clusNext = pstm->s_clusFirst;
       pstm->s_run.r_clusPrev = NO_CLUSTER;
    }
}


/*  SeekStream
 *
 *  ENTRY
 *      pstm - pointer to DSTREAM
 *      pos - position within DSTREAM to seek to
 *
 *  EXIT
 *      0 if successful, error code if not
 *      (eg, ERROR_HANDLE_EOF if end of cluster chain reached)
 */

DWORD SeekStream(PDSTREAM pstm, DWORD pos)
{
    DWORD dwError;
    ASSERT(OWNCRITICALSECTION(&pstm->s_cs));

    if ((pos == INVALID_POS) || (pos < pstm->s_run.r_start) ||
    ((pstm->s_run.r_clusNext == UNKNOWN_CLUSTER) && (pstm->s_clusFirst != UNKNOWN_CLUSTER))) {

        RewindStream(pstm, pos);
    }

    // Locate the data run which contains the position requested.

    do {
        
        if (pos < pstm->s_run.r_end)
            return ERROR_SUCCESS;

        DEBUGMSG(ZONE_LOGIO && (pstm->s_flags & STF_DEMANDPAGED),(DBGTEXT("FATFS!SeekStream(DP): begin unpack\r\n")));
        dwError = UnpackRun(pstm);
        DEBUGMSG(ZONE_LOGIO && (pstm->s_flags & STF_DEMANDPAGED),(DBGTEXT("FATFS!SeekStream(DP): end unpack\r\n")));

        if (dwError)
            break;

    } while (TRUE);

    return dwError;
}


/*  PositionStream - Find cluster containing (or preceding) specified position
 *
 *  ENTRY
 *      pstm - pointer to DSTREAM
 *      pos - offset within stream to seek to
 *
 *  EXIT
 *      There are three general cases that callers are concerned about:
 *
 *          UNKNOWN_CLUSTER:    the specified position does not lie
 *                              within a cluster (ie, is beyond EOF)
 *
 *          NO_CLUSTER:         the specified position does not require a
 *                              cluster (in other words, pos is ZERO)
 *
 *          Valid cluster:      There are two cases here:  if pos is on a
 *                              cluster boundary, we will return the cluster
 *                              PRECEDING the one containing pos, otherwise
 *                              we will return the cluster CONTAINING pos.
 */

DWORD PositionStream(PDSTREAM pstm, DWORD pos, PDWORD pdwClus)
{
    DWORD dwError = ERROR_SUCCESS;

    ASSERT(OWNCRITICALSECTION(&pstm->s_cs));

    if (!pdwClus)
        return ERROR_INVALID_DATA;

    if ((pos == INVALID_POS) || (pos < pstm->s_run.r_start) ||
    ((pstm->s_run.r_clusNext == UNKNOWN_CLUSTER) && (pstm->s_clusFirst != UNKNOWN_CLUSTER))) {

        RewindStream(pstm, pos);
    }

    // Locate the data run which contains the position requested.

    do {

⌨️ 快捷键说明

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