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

📄 fswrite.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
#endif /* QUOTA_ENABLED */
    }

    /*-----------------------------------------------------------------*/
    /* Else we're writing over old stuff, get the next sector.         */
    /*-----------------------------------------------------------------*/
    else
    {
      sect_num = (ui32)stream->curr_ptr.sector;

      /*---------------------------------------------------------------*/
      /* Set the dirty flag to dirty old because it's an old sector,   */
      /* unless it's a creatn() file, in which case it's as new. Also, */
      /* for dirty old sectors, set stream flag to modified.           */
      /*---------------------------------------------------------------*/
      if (link_ptr->comm->mode & S_CREATN)
      {
        /*-------------------------------------------------------------*/
        /* For NAND/MLC ensure sector hasn't previously been written.  */
        /*-------------------------------------------------------------*/
        if (Flash->type != FFS_NOR && !Flash->empty_sect(sect_num))
        {
          stream->flags |= FCB_MOD;
          dirty_flag = DIRTY_OLD;
          Flash->cache.dirty_old = TRUE;
        }
        else
        {
          dirty_flag = DIRTY_NEW;
          Flash->cache.dirty_new = TRUE;
        }
      }
      else
      {
        stream->flags |= FCB_MOD;
        dirty_flag = DIRTY_OLD;
        Flash->cache.dirty_old = TRUE;
      }

      /*---------------------------------------------------------------*/
      /* Set "adjust size" flag only if we go past the end of file.    */
      /*---------------------------------------------------------------*/
      adjust_size = (remaining > link_ptr->comm->one_past_last.offset) &&
       (link_ptr->comm->one_past_last.sector == stream->curr_ptr.sector);

      /*---------------------------------------------------------------*/
      /* Set "skip read" flag if able to skip reading sector into cache*/
      /*---------------------------------------------------------------*/
      skip_read = (remaining >= Flash->sect_sz);
    }

    /*-----------------------------------------------------------------*/
    /* NAND/MLC creatn files should skip cache reads.                  */
    /*-----------------------------------------------------------------*/
    if (Flash->type != FFS_NOR && (link_ptr->comm->mode & S_CREATN))
      skip_read = TRUE;

    /*-----------------------------------------------------------------*/
    /* Get pointer to the sector.                                      */
    /*-----------------------------------------------------------------*/
    status = GetSector(&Flash->cache, (int)sect_num, skip_read,
                       link_ptr->comm, &stream->cached);
    if (status != GET_OK)
    {
      if (stream->cached && FreeSector(&stream->cached, &Flash->cache))
        return -1;
      return (int)(len - remaining);
    }
    PfAssert(stream->cached->file_ptr == link_ptr->comm);

    /*-----------------------------------------------------------------*/
    /* If FlashWrprWr() will use a new sector, keep track of it and    */
    /* set the dirty flag for this sector.                             */
    /*-----------------------------------------------------------------*/
    if (((CacheEntry *)stream->cached)->dirty == CLEAN)
    {
      if (dirty_flag == DIRTY_OLD)
        --Flash->free_sects;
      ((CacheEntry *)stream->cached)->dirty = dirty_flag;
    }

    /*-----------------------------------------------------------------*/
    /* Get the pointer to the sector data.                             */
    /*-----------------------------------------------------------------*/
    sector = ((CacheEntry *)stream->cached)->sector;

    /*-----------------------------------------------------------------*/
    /* If what's left fits into this sector, it's the last sector to   */
    /* be written (maybe only partially).                              */
    /*-----------------------------------------------------------------*/
    if (remaining <= Flash->sect_sz)
    {
      /*---------------------------------------------------------------*/
      /* Use memcpy to copy everything that's left into current sect.  */
      /*---------------------------------------------------------------*/
      memcpy(sector, buf, remaining);

      /*---------------------------------------------------------------*/
      /* If we've written a whole sector, current pointer will be -1,  */
      /* else it will have the right sector number and offset.         */
      /*---------------------------------------------------------------*/
      if (remaining == Flash->sect_sz)
      {
        if (stream->curr_ptr.sector == link_ptr->comm->last_sect)
          stream->curr_ptr.sector = (ui16)-1;
        else
        {
          stream->curr_ptr.sector = Flash->sect_tbl[sect_num].next;
          ++stream->curr_ptr.sect_off;
        }
        stream->curr_ptr.offset = 0;

        /*-------------------------------------------------------------*/
        /* If one past last needs to be modified, do it.               */
        /*-------------------------------------------------------------*/
        if (sect_num == link_ptr->comm->last_sect)
        {
          /*-----------------------------------------------------------*/
          /* Adjust file size.                                         */
          /*-----------------------------------------------------------*/
          link_ptr->comm->size += adjust_size * (Flash->sect_sz -
                                  link_ptr->comm->one_past_last.offset);
          link_ptr->comm->one_past_last.offset = 0;
          link_ptr->comm->one_past_last.sector = (ui16)-1;
        }

        /*-------------------------------------------------------------*/
        /* Free current sector because we've filled it.                */
        /*-------------------------------------------------------------*/
        if (FreeSector(&stream->cached, &Flash->cache))
          return -1;
      }
      else
      {
        stream->curr_ptr.offset = (ui16)remaining;

        /*-------------------------------------------------------------*/
        /* If one past last needs to be modified, modify it.           */
        /*-------------------------------------------------------------*/
        if (stream->curr_ptr.sector ==
                                  link_ptr->comm->one_past_last.sector &&
            stream->curr_ptr.offset >
                                    link_ptr->comm->one_past_last.offset)
        {
          /*-----------------------------------------------------------*/
          /* Adjust file size.                                         */
          /*-----------------------------------------------------------*/
          link_ptr->comm->size += (stream->curr_ptr.offset -
                                   link_ptr->comm->one_past_last.offset);
          link_ptr->comm->one_past_last.offset =
                                           (ui16)stream->curr_ptr.offset;
        }
      }
    }

    /*-----------------------------------------------------------------*/
    /* Else have to write the whole sector.                            */
    /*-----------------------------------------------------------------*/
    else
    {
      /*---------------------------------------------------------------*/
      /* Use memcpy to write the whole sector.                         */
      /*---------------------------------------------------------------*/
      memcpy(sector, buf, Flash->sect_sz);

      /*---------------------------------------------------------------*/
      /* Update remaining and buf.                                     */
      /*---------------------------------------------------------------*/
      remaining -= Flash->sect_sz;
      buf = (void *)((ui32)buf + Flash->sect_sz);

      /*---------------------------------------------------------------*/
      /* If we're at the end of the file, from now on get new sectors. */
      /*---------------------------------------------------------------*/
      if (sect_num == link_ptr->comm->last_sect)
      {
        /*-------------------------------------------------------------*/
        /* Adjust file size.                                           */
        /*-------------------------------------------------------------*/
        link_ptr->comm->size += adjust_size * (Flash->sect_sz -
                                link_ptr->comm->one_past_last.offset);
        stream->curr_ptr.sector = (ui16)-1;
        stream->curr_ptr.offset = 0;
        link_ptr->comm->one_past_last.sector = (ui16)-1;
        link_ptr->comm->one_past_last.offset = 0;
      }

      /*---------------------------------------------------------------*/
      /* Else go to the next sector in the file.                       */
      /*---------------------------------------------------------------*/
      else
      {
        stream->curr_ptr.sector = Flash->sect_tbl[sect_num].next;
        stream->curr_ptr.offset = 0;
        ++stream->curr_ptr.sect_off;
      }

      /*---------------------------------------------------------------*/
      /* Free current sector because it's full.                        */
      /*---------------------------------------------------------------*/
      if (FreeSector(&stream->cached, &Flash->cache))
        return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Check for recycles when writing over old data.                  */
    /*-----------------------------------------------------------------*/
    if (dirty_flag == DIRTY_OLD &&
        FlashRecChck(SKIP_CHECK_FULL) == RECYCLE_FAILED)
      return (int)(len - remaining);
  }

  /*-------------------------------------------------------------------*/
  /* Set access and modification times and return length written.      */
  /*-------------------------------------------------------------------*/
  link_ptr->comm->mod_time = link_ptr->comm->ac_time = OsSecCount;
  return (int)len;
}
#endif /* NUM_FFS_VOLS */

⌨️ 快捷键说明

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