Changeset 654 for soft


Ignore:
Timestamp:
Jul 22, 2015, 2:31:34 PM (9 years ago)
Author:
guerin
Message:

fat32: implement O_TRUNC for _fat_open()

Location:
soft/giet_vm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/applications/shell/main.c

    r643 r654  
    104104    size = info.size;
    105105
    106     dst_fd = giet_fat_open( argv[2] , O_CREATE ); // TODO O_TRUNC
     106    dst_fd = giet_fat_open( argv[2] , O_CREATE | O_TRUNC );
    107107    if (dst_fd < 0)
    108108    {
  • soft/giet_vm/giet_fat32/fat32.c

    r653 r654  
    28002800//   -8  :  "Cannot update FS_INFO sector"
    28012801//   -9  :  "file descriptor array full"
     2802//   -10 :  "cannot truncate file"
    28022803///////////////////////////////////////////////////////////////////////////////
    28032804int _fat_open( char*        pathname,     // absolute path from root
     
    28122813    // get flags
    28132814    unsigned int create    = ((flags & O_CREATE) != 0);
    2814     unsigned int read_only = ((flags & O_RDONLY) != 0);
     2815    unsigned int read_only = ((flags & O_RDONLY) != 0);
     2816    unsigned int truncate  = ((flags & O_TRUNC)  != 0);
    28152817
    28162818#if GIET_DEBUG_FAT
     
    29252927            return -8;
    29262928        }
     2929
     2930        // no need to truncate a new file
     2931        truncate = 0;
    29272932    }
    29282933    else // code == 0
     
    29362941        x , y , p , pathname , child );
    29372942#endif
    2938 
    29392943    }
    29402944
     
    29472951
    29482952    // set file descriptor if an empty slot has been found
    2949     if ( fd_id < GIET_OPEN_FILES_MAX )
    2950     {
    2951         // update file descriptor
    2952         _fat.fd[fd_id].allocated  = 1;
    2953         _fat.fd[fd_id].seek       = 0;
    2954         _fat.fd[fd_id].read_only  = read_only;
    2955         _fat.fd[fd_id].inode      = child;
    2956 
    2957         // increment the refcount
    2958         child->count = child->count + 1;
    2959 
    2960         // releases the lock
     2953    if ( fd_id >= GIET_OPEN_FILES_MAX )
     2954    {
    29612955        _spin_lock_release( &_fat.fat_lock );
     2956        _printf("\n[FAT ERROR] in _fat_open() : File-Descriptors-Array full\n");
     2957        return -9;
     2958    }
     2959
     2960    // update file descriptor
     2961    _fat.fd[fd_id].allocated  = 1;
     2962    _fat.fd[fd_id].seek       = 0;
     2963    _fat.fd[fd_id].read_only  = read_only;
     2964    _fat.fd[fd_id].inode      = child;
     2965
     2966    // increment the refcount
     2967    child->count = child->count + 1;
     2968
     2969    // truncate the file if requested
     2970    if ( truncate && !read_only && !child->is_dir )
     2971    {
     2972        // empty file
     2973        child->size = 0;
     2974        child->levels = _get_levels_from_size( child->size );
     2975
     2976        // release File-Cache (keep root node)
     2977        _release_cache_memory( child->cache, child->levels );
     2978
     2979        // release clusters allocated to file/dir in DATA region
     2980        if ( _clusters_release( child->cluster ) )
     2981        {
     2982            _spin_lock_release( &_fat.fat_lock );
     2983            _printf("\n[FAT ERROR] in _fat_open() : can't truncate file\n");
     2984            return -10;
     2985        }
     2986
     2987        // update parent directory entry (size and cluster index)
     2988        if ( _update_dir_entry( child ) )
     2989        {
     2990            _spin_lock_release( &_fat.fat_lock );
     2991            _printf("\n[FAT ERROR] in _fat_open() : can't truncate file\n");
     2992            return -10;
     2993        }
     2994    }
     2995
     2996    // releases the lock
     2997    _spin_lock_release( &_fat.fat_lock );
    29622998
    29632999#if GIET_DEBUG_FAT
     
    29673003        x , y , p , fd_id , pathname , read_only );
    29683004#endif
    2969         return fd_id;
    2970     }
    2971     else
    2972     {
    2973         _spin_lock_release( &_fat.fat_lock );
    2974         _printf("\n[FAT ERROR] in _fat_open() : File-Descriptors-Array full\n");
    2975         return -9;
    2976     }
     3005    return fd_id;
    29773006} // end _fat_open()
    29783007
     
    33943423        }
    33953424         
    3396         // update parent directory entry (size an cluster index)
     3425        // update parent directory entry (size and cluster index)
    33973426        if ( _update_dir_entry( inode ) )
    33983427        {
  • soft/giet_vm/giet_fat32/fat32.h

    r623 r654  
    139139
    140140#define O_RDONLY                0x01
     141#define O_TRUNC                 0x10
    141142#define O_CREATE                0x20
    142143#endif // _FAT32_SHARED
  • soft/giet_vm/giet_libs/stdio.h

    r647 r654  
    100100#define SEEK_CUR            1          // argument for giet_fat_lseek
    101101
     102#define O_RDONLY            0x01       // argument for giet_fat_open()
     103#define O_TRUNC             0x10       // argument for giet_fat_open()
    102104#define O_CREATE            0x20       // argument for giet_fat_open()
    103 #define O_RDONLY            0x01       // argument for giet_fat_open()
    104105#endif // _FAT32_SHARED
    105106
Note: See TracChangeset for help on using the changeset viewer.