Difference between revisions of "Contiki Coffee File System"

From Contiki
Jump to: navigation, search
(Source Code)
(cfs_open)
Line 14: Line 14:
  
 
== cfs_open ==
 
== cfs_open ==
 +
<source lang="c">
 +
typedef uint16_t packetbuf_attr_t;
 +
 +
struct packetbuf_attr {
 +
int
 +
cfs_open(const char *name, int flags)
 +
{
 +
  int fd;
 +
  struct file_desc *fdp;
 +
 +
  fd = get_available_fd();
 +
  if(fd < 0) {
 +
    PRINTF("Coffee: Failed to allocate a new file descriptor!\n");
 +
    return -1;
 +
  }
 +
 +
  fdp = &coffee_fd_set[fd];
 +
  fdp->flags = 0;
 +
 +
  fdp->file = find_file(name);
 +
  if(fdp->file == NULL) {
 +
    if((flags & (CFS_READ | CFS_WRITE)) == CFS_READ) {
 +
      return -1;
 +
    }
 +
    fdp->file = reserve(name, page_count(COFFEE_DYN_SIZE), 1, 0);
 +
    if(fdp->file == NULL) {
 +
      return -1;
 +
    }
 +
    fdp->file->end = 0;
 +
  } else if(fdp->file->end == UNKNOWN_OFFSET) {
 +
    fdp->file->end = file_end(fdp->file->page);
 +
  }
 +
 +
  fdp->flags |= flags;
 +
  fdp->offset = flags & CFS_APPEND ? fdp->file->end : 0;
 +
  fdp->file->references++;
 +
 +
  return fd;
 +
}
 +
</source>

Revision as of 13:56, 8 November 2014

Introduction

This tutorial is a introduction to cfs_open function of Coffee file system on Contiki OS 2.7.

You Will Learn

Through this tutorial you will learn about how the Coffee file system works.

Source Code

~/contiki-2.7/core/cfs/cfs-coffee.c ~/contiki-2.7/core/cfs/cfs-coffee.h


cfs_open

typedef uint16_t packetbuf_attr_t;

struct packetbuf_attr {
int
cfs_open(const char *name, int flags)
{
  int fd;
  struct file_desc *fdp;

  fd = get_available_fd();
  if(fd < 0) {
    PRINTF("Coffee: Failed to allocate a new file descriptor!\n");
    return -1;
  }

  fdp = &coffee_fd_set[fd];
  fdp->flags = 0;

  fdp->file = find_file(name);
  if(fdp->file == NULL) {
    if((flags & (CFS_READ | CFS_WRITE)) == CFS_READ) {
      return -1;
    }
    fdp->file = reserve(name, page_count(COFFEE_DYN_SIZE), 1, 0);
    if(fdp->file == NULL) {
      return -1;
    }
    fdp->file->end = 0;
  } else if(fdp->file->end == UNKNOWN_OFFSET) {
    fdp->file->end = file_end(fdp->file->page);
  }

  fdp->flags |= flags;
  fdp->offset = flags & CFS_APPEND ? fdp->file->end : 0;
  fdp->file->references++;

  return fd;
}