Logo Search packages:      
Sourcecode: gaia version File versions  Download package

void FilesystemStorage::Process ( TilePtr  tile  )  [protected, virtual]

Load/save tile

Implements SimpleTileStorage.

Definition at line 28 of file FilesystemStorage.cc.

References RawBuffer::Data(), SimpleTileStorage::m_pSaveStorage, m_StorageRoot, PathFromCoords(), and RawBuffer::Size().

                                            {
      if (!tile->IsLoaded()) { /* loading */
            std::string path = m_StorageRoot + PathFromCoords(tile->GetX(), tile->GetY(), tile->GetLevel(), tile->GetType());
            try {
                  int f;

                  if ((f = open(path.c_str(), O_RDONLY)) == -1)
                        throw ErrnoException("cannot open file in filesystem storage", errno);

                  RawBuffer *buf = new RawBuffer(f);

                  try {
                        tile->Load(buf, m_pSaveStorage != 0);
                  } catch (...) {
                        close(f);
                        delete buf;
                        throw;
                  }

                  close(f);
            } catch(ErrnoException &e) {
                  /* unable to load current tile, i.e. tile is not in a storage
                   * THIS is not an error */
            } catch(std::exception &e) {
                  /* unable to load current tile, i.e. tile is not in a storage
                   * THIS is not an error */
            }
      } else if (tile->IsSaveable()) { /* saving */
            std::string subpath = PathFromCoords(tile->GetX(), tile->GetY(), tile->GetLevel(), tile->GetType());

            /* store file, creating needed directories */
            std::string::size_type pos = 0;
            while (pos != std::string::npos) {
                  if ((pos = subpath.find('/', pos+1)) == std::string::npos) {
                        std::string path = m_StorageRoot + subpath;
                        int f;
                        if ((f = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1) {
                              throw ErrnoException("cannot open file in filesystem storage", errno);
                        }

                        /* for null tile, just create corresponding file */
                        if (!tile->IsNull()) {
                              RawBuffer *buf = tile->ReleaseRawData();
                              if (write(f, buf->Data(), buf->Size()) != buf->Size()) {
                                    unlink(path.c_str());   /* ensure no broken files get into cache */
                                    delete buf;
                                    close(f);
                                    throw ErrnoException("cannot write file into filesystem storage", errno);
                              }

                              delete buf;
                        }

                        close(f);
                        return;
                  } else {
                        std::string path = m_StorageRoot + subpath.substr(0, pos);
                        if (mkdir(path.c_str(), 0777) != 0 && errno != EEXIST)
                              throw ErrnoException("cannot mkdir in filesystem storage", errno);
                  }
            }
            throw Exception("shouldn't get here");
      }
}


Generated by  Doxygen 1.6.0   Back to index