Ignore:
Timestamp:
Apr 14, 2002, 1:42:05 AM (23 years ago)
Author:
umoeller
Message:

Lots of changes from the last three weeks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/dosh.c

    r146 r153  
    11891189 *      --  DRVFL_CHECKLONGNAMES: drive should always be
    11901190 *          checked for longname support. If this is
    1191  *          set, we will try a DosOpen on the drive
    1192  *          to see if it supports long filenames
    1193  *          (unless it's a "well-known" file-system
    1194  *          and we know it does). In enabled, the
    1195  *          DFL_SUPPORTS_LONGNAMES flag is reliable.
     1191 *          set, we will try a DosOpen("\\long.name.file")
     1192 *          on the drive to see if it supports long
     1193 *          filenames (unless it's a "well-known"
     1194 *          file-system and we know it does). If enabled,
     1195 *          the DFL_SUPPORTS_LONGNAMES flag is reliable.
     1196 *          Note that this does not check for what special
     1197 *          characters are supported in file names.
    11961198 *
    11971199 *      This should return only one of the following:
     
    12221224 *          returns NO_ERROR for the given drive.
    12231225 *          This will rule out invalid drive letters
    1224  *          and drives that are presently locked.
     1226 *          and drives that are presently locked by
     1227 *          CHKDSK or something.
    12251228 *
    12261229 *      2)  If so, check whether XDISKINFO.flDevice
     
    21902193
    21912194/*
    2192  *@@ doshOpenExisting:
    2193  *      opens an existing file for read-write access. Does
    2194  *      not create a new file if the file doesn't exist.
    2195  *
    2196  *      This is just a simple wrapper around DosOpen.
    2197  *
    2198  *      ulOpenFlags is passed to DosOpen. Should be one
    2199  *      of:
    2200  *
    2201  *      --  for read-only access:
    2202  *
    2203  +              OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY
    2204  *
    2205  *      --  for read-write access:
    2206  *
    2207  +              OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE
    2208  *
    2209  *      In addition, you can specify
    2210  *
    2211  +          OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_RANDOM
    2212  +                          | OPEN_FLAGS_NOINHERIT
    2213  *
    2214  *@@added V0.9.13 (2001-06-14) [umoeller]
    2215  */
    2216 
    2217 /*
    2218 APIRET doshOpenExisting(PCSZ pcszFilename,   // in: file name
    2219                         ULONG ulOpenFlags,          // in: open flags
    2220                         HFILE *phf)                 // out: OS/2 file handle
    2221 {
    2222     ULONG ulAction;
    2223     return (DosOpen((PSZ)pcszFilename,
    2224                     phf,
    2225                     &ulAction,
    2226                     0,          // cbFile
    2227                     0,          // attributes
    2228                     OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
    2229                     ulOpenFlags,
    2230                     NULL));     // EAs
    2231 }
    2232 */
     2195 *@@category: Helpers\Control program helpers\File management\XFILEs
     2196 */
     2197
     2198/* ******************************************************************
     2199 *
     2200 *   XFILEs
     2201 *
     2202 ********************************************************************/
    22332203
    22342204/*
     
    22952265                PXFILE *ppFile)
    22962266{
    2297     APIRET arc = NO_ERROR;
     2267    APIRET  arc = NO_ERROR;
    22982268
    22992269    ULONG   fsOpenFlags = 0,
     
    24092379    }
    24102380    else
    2411         arc = ERROR_INVALID_PARAMETER;
     2381        if (!arc)       // V0.9.19 (2002-04-02) [umoeller]
     2382            arc = ERROR_INVALID_PARAMETER;
    24122383
    24132384    return (arc);
    24142385}
    2415 
    2416 /*
    2417  *@@ doshLockFile:
    2418  *
    2419  *@@added V0.9.16 (2001-10-19) [umoeller]
    2420  */
    2421 
    2422 /* APIRET doshLockFile(PXFILE pFile)
    2423 {
    2424     if (!pFile)
    2425         return (ERROR_INVALID_PARAMETER);
    2426 
    2427     if (!pFile->hmtx)
    2428         // first call:
    2429         return (DosCreateMutexSem(NULL,
    2430                                   &pFile->hmtx,
    2431                                   0,
    2432                                   TRUE));        // request!
    2433 
    2434     return (DosRequestMutexSem(pFile->hmtx, SEM_INDEFINITE_WAIT));
    2435 } */
    2436 
    2437 /*
    2438  *@@ doshUnlockFile:
    2439  *
    2440  *@@added V0.9.16 (2001-10-19) [umoeller]
    2441  */
    2442 
    2443 /* APIRET doshUnlockFile(PXFILE pFile)
    2444 {
    2445     if (pFile)
    2446         return (DosReleaseMutexSem(pFile->hmtx));
    2447 
    2448     return (ERROR_INVALID_PARAMETER);
    2449 } */
    24502386
    24512387/*
     
    24832419 *@@added V0.9.13 (2001-06-14) [umoeller]
    24842420 *@@changed V0.9.16 (2001-12-18) [umoeller]: now with XFILE, and always using FILE_BEGIN
     2421 *@@chaanged V0.9.19 (2002-04-02) [umoeller]: added params checking
    24852422 */
    24862423
    24872424APIRET doshReadAt(PXFILE pFile,
    24882425                  ULONG ulOffset,   // in: offset to read from (from beginning of file)
    2489                   PULONG pcb,       // in: bytes to read, out: bytes read
     2426                  PULONG pcb,       // in: bytes to read, out: bytes read (req.)
    24902427                  PBYTE pbData,     // out: read buffer (must be cb bytes)
    24912428                  ULONG fl)         // in: DRFL_* flags
    24922429{
    24932430    APIRET arc = NO_ERROR;
    2494     ULONG cb = *pcb;
     2431    ULONG cb;
    24952432    ULONG ulDummy;
    24962433
    2497     // if (!(arc = doshLockFile(pFile)))   // this checks for pFile
    2498     {
    2499         *pcb = 0;
    2500 
    2501         // check if we have the data in the cache already;
    2502 
    2503         if (    (pFile->pbCache)
    2504                 // first byte must be in cache
    2505              && (ulOffset >= pFile->ulReadFrom)
    2506                 // last byte must be in cache
    2507              && (    ulOffset + cb
    2508                   <= pFile->ulReadFrom + pFile->cbCache
    2509                 )
     2434    if (!pFile || !pcb)
     2435        // V0.9.19 (2002-04-02) [umoeller]
     2436        return ERROR_INVALID_PARAMETER;
     2437
     2438    cb = *pcb;
     2439    *pcb = 0;
     2440
     2441    // check if we have the data in the cache already;
     2442
     2443    if (    (pFile->pbCache)
     2444            // first byte must be in cache
     2445         && (ulOffset >= pFile->ulReadFrom)
     2446            // last byte must be in cache
     2447         && (    ulOffset + cb
     2448              <= pFile->ulReadFrom + pFile->cbCache
     2449            )
     2450       )
     2451    {
     2452        // alright, return data from cache simply
     2453        ULONG ulOfsInCache = ulOffset - pFile->ulReadFrom;
     2454
     2455        memcpy(pbData,
     2456               pFile->pbCache + ulOfsInCache,
     2457               cb);
     2458        *pcb = cb;
     2459
     2460        #ifdef DEBUG_DOSOPEN
     2461        _Pmpf((__FUNCTION__ " %s: data is fully in cache",
     2462                    pFile->pszFilename));
     2463        _Pmpf(("  caller wants %d bytes from %d",
     2464                    cb, ulOffset));
     2465        _Pmpf(("  we got %d bytes from %d",
     2466                    pFile->cbCache, pFile->ulReadFrom));
     2467        _Pmpf(("  so copied %d bytes from cache ofs %d",
     2468                    cb, ulOfsInCache));
     2469        #endif
     2470    }
     2471    else
     2472    {
     2473        // data is not in cache:
     2474        // check how much it is... for small amounts,
     2475        // we load the cache first
     2476        if (    (cb <= 4096 - 512)
     2477             && (!(fl & DRFL_NOCACHE))
    25102478           )
    25112479        {
    2512             // alright, return data from cache simply
    2513             ULONG ulOfsInCache = ulOffset - pFile->ulReadFrom;
    2514 
    2515             memcpy(pbData,
    2516                    pFile->pbCache + ulOfsInCache,
    2517                    cb);
    2518             *pcb = cb;
    2519 
    25202480            #ifdef DEBUG_DOSOPEN
    2521             _Pmpf((__FUNCTION__ " %s: data is fully in cache",
    2522                         pFile->pszFilename));
     2481            _Pmpf((__FUNCTION__ " %s: filling cache anew",
     2482                    pFile->pszFilename));
    25232483            _Pmpf(("  caller wants %d bytes from %d",
    25242484                        cb, ulOffset));
    2525             _Pmpf(("  we got %d bytes from %d",
     2485            #endif
     2486
     2487            // OK, then fix the offset to read from
     2488            // to a multiple of 512 to get a full sector
     2489            pFile->ulReadFrom = ulOffset / 512L * 512L;
     2490            // and read 4096 bytes always plus the
     2491            // value we cut off above
     2492            pFile->cbCache = 4096;
     2493
     2494            #ifdef DEBUG_DOSOPEN
     2495            _Pmpf(("  getting %d bytes from %d",
    25262496                        pFile->cbCache, pFile->ulReadFrom));
    2527             _Pmpf(("  so copied %d bytes from cache ofs %d",
    2528                         cb, ulOfsInCache));
    25292497            #endif
    2530         }
    2531         else
    2532         {
    2533             // data is not in cache:
    2534             // check how much it is... for small amounts,
    2535             // we load the cache first
    2536             if (    (cb <= 4096 - 512)
    2537                  && (!(fl & DRFL_NOCACHE))
    2538                )
    2539             {
    2540                 #ifdef DEBUG_DOSOPEN
    2541                 _Pmpf((__FUNCTION__ " %s: filling cache anew",
    2542                         pFile->pszFilename));
    2543                 _Pmpf(("  caller wants %d bytes from %d",
    2544                             cb, ulOffset));
    2545                 #endif
    2546 
    2547                 // OK, then fix the offset to read from
    2548                 // to a multiple of 512 to get a full sector
    2549                 pFile->ulReadFrom = ulOffset / 512L * 512L;
    2550                 // and read 4096 bytes always plus the
    2551                 // value we cut off above
    2552                 pFile->cbCache = 4096;
    2553 
    2554                 #ifdef DEBUG_DOSOPEN
    2555                 _Pmpf(("  getting %d bytes from %d",
    2556                             pFile->cbCache, pFile->ulReadFrom));
    2557                 #endif
    2558 
    2559                 // free old cache
    2560                 if (pFile->pbCache)
    2561                     free(pFile->pbCache);
    2562 
    2563                 // allocate new cache
    2564                 if (!(pFile->pbCache = (PBYTE)malloc(pFile->cbCache)))
    2565                     arc = ERROR_NOT_ENOUGH_MEMORY;
    2566                 else
    2567                 {
    2568                     ULONG ulOfsInCache = 0;
    2569 
    2570                     if (!(arc = DosSetFilePtr(pFile->hf,
    2571                                               (LONG)pFile->ulReadFrom,
    2572                                               FILE_BEGIN,
    2573                                               &ulDummy)))
    2574                     {
    2575                         if (!(arc = DosRead(pFile->hf,
    2576                                             pFile->pbCache,
    2577                                             pFile->cbCache,
    2578                                             &ulDummy)))
    2579                         {
    2580                             // got data:
    2581                             #ifdef DEBUG_DOSOPEN
    2582                             _Pmpf(("        %d bytes read", ulDummy));
    2583                             #endif
    2584 
    2585                             pFile->cbCache = ulDummy;
    2586 
    2587                             // check bounds
    2588                             ulOfsInCache = ulOffset - pFile->ulReadFrom;
    2589 
    2590                             /*
    2591                             if (ulOfsInCache + cb > pFile->cbCache)
    2592                             {
    2593                                 cb = pFile->cbCache - ulOfsInCache;
    2594                                 if (fl & DRFL_FAILIFLESS)
    2595                                     arc = ERROR_NO_DATA;
    2596                             }
    2597                             */
    2598                         }
    2599                     }
    2600 
    2601                     if (!arc)
    2602                     {
    2603                         // copy to caller
    2604                         memcpy(pbData,
    2605                                pFile->pbCache + ulOfsInCache,
    2606                                cb);
    2607                         *pcb = cb;
    2608 
    2609                         #ifdef DEBUG_DOSOPEN
    2610                         _Pmpf(("  so copied %d bytes from cache ofs %d",
    2611                                     cb, ulOfsInCache));
    2612                         #endif
    2613                     }
    2614                     else
    2615                     {
    2616                         free(pFile->pbCache);
    2617                         pFile->pbCache = NULL;
    2618                     }
    2619                 } // end else if (!(pFile->pbCache = (PBYTE)malloc(pFile->cbCache)))
    2620             }
     2498
     2499            // free old cache
     2500            if (pFile->pbCache)
     2501                free(pFile->pbCache);
     2502
     2503            // allocate new cache
     2504            if (!(pFile->pbCache = (PBYTE)malloc(pFile->cbCache)))
     2505                arc = ERROR_NOT_ENOUGH_MEMORY;
    26212506            else
    26222507            {
    2623                 // read uncached:
    2624                 #ifdef DEBUG_DOSOPEN
    2625                 _Pmpf(("  " __FUNCTION__ " %s: reading uncached",
    2626                             pFile->pszFilename));
    2627                 _Pmpf(("      caller wants %d bytes from %d",
    2628                             cb, ulOffset));
    2629                 #endif
     2508                ULONG ulOfsInCache = 0;
    26302509
    26312510                if (!(arc = DosSetFilePtr(pFile->hf,
    2632                                           (LONG)ulOffset,
     2511                                          (LONG)pFile->ulReadFrom,
    26332512                                          FILE_BEGIN,
    26342513                                          &ulDummy)))
    26352514                {
    26362515                    if (!(arc = DosRead(pFile->hf,
    2637                                         pbData,
    2638                                         cb,
     2516                                        pFile->pbCache,
     2517                                        pFile->cbCache,
    26392518                                        &ulDummy)))
    26402519                    {
    2641                         if (    (fl & DRFL_FAILIFLESS)
    2642                              && (ulDummy != cb)
    2643                            )
    2644                             arc = ERROR_NO_DATA;
    2645                         else
    2646                             *pcb = ulDummy;     // bytes read
     2520                        // got data:
     2521                        #ifdef DEBUG_DOSOPEN
     2522                        _Pmpf(("        %d bytes read", ulDummy));
     2523                        #endif
     2524
     2525                        pFile->cbCache = ulDummy;
     2526
     2527                        // check bounds
     2528                        ulOfsInCache = ulOffset - pFile->ulReadFrom;
     2529
     2530                        /*
     2531                        if (ulOfsInCache + cb > pFile->cbCache)
     2532                        {
     2533                            cb = pFile->cbCache - ulOfsInCache;
     2534                            if (fl & DRFL_FAILIFLESS)
     2535                                arc = ERROR_NO_DATA;
     2536                        }
     2537                        */
    26472538                    }
     2539                }
     2540
     2541                if (!arc)
     2542                {
     2543                    // copy to caller
     2544                    memcpy(pbData,
     2545                           pFile->pbCache + ulOfsInCache,
     2546                           cb);
     2547                    *pcb = cb;
     2548
     2549                    #ifdef DEBUG_DOSOPEN
     2550                    _Pmpf(("  so copied %d bytes from cache ofs %d",
     2551                                cb, ulOfsInCache));
     2552                    #endif
     2553                }
     2554                else
     2555                {
     2556                    free(pFile->pbCache);
     2557                    pFile->pbCache = NULL;
     2558                }
     2559            } // end else if (!(pFile->pbCache = (PBYTE)malloc(pFile->cbCache)))
     2560        }
     2561        else
     2562        {
     2563            // read uncached:
     2564            #ifdef DEBUG_DOSOPEN
     2565            _Pmpf(("  " __FUNCTION__ " %s: reading uncached",
     2566                        pFile->pszFilename));
     2567            _Pmpf(("      caller wants %d bytes from %d",
     2568                        cb, ulOffset));
     2569            #endif
     2570
     2571            if (!(arc = DosSetFilePtr(pFile->hf,
     2572                                      (LONG)ulOffset,
     2573                                      FILE_BEGIN,
     2574                                      &ulDummy)))
     2575            {
     2576                if (!(arc = DosRead(pFile->hf,
     2577                                    pbData,
     2578                                    cb,
     2579                                    &ulDummy)))
     2580                {
     2581                    if (    (fl & DRFL_FAILIFLESS)
     2582                         && (ulDummy != cb)
     2583                       )
     2584                        arc = ERROR_NO_DATA;
     2585                    else
     2586                        *pcb = ulDummy;     // bytes read
    26482587                }
    26492588            }
    26502589        }
    2651 
    2652         // doshUnlockFile(pFile);
    26532590    }
    26542591
     
    27372674
    27382675            if (!arc)
    2739                 // if (!(arc = doshLockFile(pFile)))   // this checks for pFile
     2676            {
     2677                ULONG cbWritten;
     2678                if (!(arc = DosWrite(pFile->hf,
     2679                                     (pszNew)
     2680                                            ? pszNew
     2681                                            : (PSZ)pbData,
     2682                                     cb,
     2683                                     &cbWritten)))
    27402684                {
    2741                     ULONG cbWritten;
    2742                     if (!(arc = DosWrite(pFile->hf,
    2743                                          (pszNew)
    2744                                                 ? pszNew
    2745                                                 : (PSZ)pbData,
    2746                                          cb,
    2747                                          &cbWritten)))
    2748                     {
    2749                         pFile->cbCurrent += cbWritten;
    2750                         // invalidate the cache
    2751                         FREE(pFile->pbCache);
    2752                     }
    2753 
    2754                     // doshUnlockFile(pFile);
     2685                    pFile->cbCurrent += cbWritten;
     2686                    // invalidate the cache
     2687                    FREE(pFile->pbCache);
    27552688                }
     2689            }
    27562690
    27572691            if (pszNew)
     
    27782712{
    27792713    APIRET arc = NO_ERROR;
    2780     // if (!(arc = doshLockFile(pFile)))   // this checks for pFile
    2781     {
    2782         ULONG cbWritten;
    2783         if (!(arc = DosSetFilePtr(pFile->hf,
    2784                                   (LONG)ulOffset,
    2785                                   FILE_BEGIN,
    2786                                   &cbWritten)))
     2714    ULONG cbWritten;
     2715    if (!(arc = DosSetFilePtr(pFile->hf,
     2716                              (LONG)ulOffset,
     2717                              FILE_BEGIN,
     2718                              &cbWritten)))
     2719    {
     2720        if (!(arc = DosWrite(pFile->hf,
     2721                             (PSZ)pbData,
     2722                             cb,
     2723                             &cbWritten)))
    27872724        {
    2788             if (!(arc = DosWrite(pFile->hf,
    2789                                  (PSZ)pbData,
    2790                                  cb,
    2791                                  &cbWritten)))
    2792             {
    2793                 if (ulOffset + cbWritten > pFile->cbCurrent)
    2794                     pFile->cbCurrent = ulOffset + cbWritten;
    2795             }
     2725            if (ulOffset + cbWritten > pFile->cbCurrent)
     2726                pFile->cbCurrent = ulOffset + cbWritten;
     2727            // invalidate the cache V0.9.19 (2002-04-02) [umoeller]
     2728            FREE(pFile->pbCache);
    27962729        }
    2797 
    2798         // doshUnlockFile(pFile);
    27992730    }
    28002731
     
    28602791/*
    28612792 * doshClose:
     2793 *      closes an XFILE opened by doshOpen and
     2794 *      sets *ppFile to NULL.
    28622795 *
    28632796 *@@added V0.9.16 (2001-10-19) [umoeller]
     
    28732806       )
    28742807    {
    2875         // request the mutex so that we won't be
    2876         // taking the file away under someone's butt
    2877         // if (!(arc = doshLockFile(pFile)))
     2808        // set the ptr to NULL
     2809        *ppFile = NULL;
     2810
     2811        FREE(pFile->pbCache);
     2812        FREE(pFile->pszFilename);
     2813
     2814        if (pFile->hf)
    28782815        {
    2879             // HMTX hmtx = pFile->hmtx;
    2880             // pFile->hmtx = NULLHANDLE;
    2881 
    2882             // now that the file is locked,
    2883             // set the ptr to NULL
    2884             *ppFile = NULL;
    2885 
    2886             FREE(pFile->pbCache);
    2887             FREE(pFile->pszFilename);
    2888 
    2889             if (pFile->hf)
    2890             {
    2891                 DosSetFileSize(pFile->hf, pFile->cbCurrent);
    2892                 DosClose(pFile->hf);
    2893                 pFile->hf = NULLHANDLE;
    2894             }
    2895 
    2896             // doshUnlockFile(pFile);
    2897             // DosCloseMutexSem(pFile->hmtx);
     2816            DosSetFileSize(pFile->hf, pFile->cbCurrent);
     2817            DosClose(pFile->hf);
     2818            pFile->hf = NULLHANDLE;
    28982819        }
    28992820
Note: See TracChangeset for help on using the changeset viewer.