Example #1
0
 void level::read() {
   level_context context;
   
   nbt::Parser<level_context> parser(&context);
   
   parser.register_byte_array = register_byte_array;
   parser.register_string = register_string;
   parser.register_int = register_int;
   parser.begin_compound = begin_compound;
   parser.begin_list = begin_list;
   parser.end_list = end_list;
   parser.end_compound = end_compound;
   parser.error_handler = error_handler;
   
   parser.parse_file(path.string().c_str());
   
   if (context.grammar_error) {
     throw invalid_file("not a valid nbt file");
   }
   
   if (!context.islevel) {
     throw invalid_file("not a level data file");
   }
   
   signs = context.signs;
   blocks = context.blocks;
   skylight = context.skylight;
   heightmap = context.heightmap;
   blocklight = context.blocklight;
   
   complete = true;
 }
Example #2
0
inline void file_wrapper::priv_close()
{
   if(m_handle != invalid_file()){
      close_file(m_handle);
      m_handle = invalid_file();
   }
}
Example #3
0
static void process_file(const char *fname)
{
    char *ptr = NULL;
    size_t size;
    cJSON *json, *id;
    lcb_error_t ret;
    int error = 0;
    lcb_store_cmd_t cmd;
    const lcb_store_cmd_t* const cmds[1] = { &cmd };

    if (fname[0] == '.') {
        if (strcmp(fname, ".dump_stats") == 0) {
            fprintf(stdout, "Dumping stats:\n");
            lcb_get_timings(instance, stdout, timings_callback);
            fprintf(stdout, "----\n");
            remove(fname);
        }
        return;
    }

    if (loadit(fname, &ptr, &size) == -1) {
        /* Error message already printed */
        return;
    }

    if ((json = cJSON_Parse(ptr)) == NULL) {
        invalid_file(INVALID_JSON, fname);
        free(ptr);
        return;
    }

    id = cJSON_GetObjectItem(json, "_id");
    if (id == NULL || id->type != cJSON_String) {
        invalid_file(UNKNOWN_JSON, fname);
        free(ptr);
        return;
    }

    memset(&cmd, 0, sizeof(cmd));
    cmd.v.v0.key = id->valuestring;
    cmd.v.v0.nkey = strlen(id->valuestring);
    cmd.v.v0.bytes = ptr;
    cmd.v.v0.nbytes = size;
    cmd.v.v0.operation = LCB_SET;

    ret = lcb_store(instance, &error, 1, cmds);
    if (ret == LCB_SUCCESS) {
        lcb_wait(instance);
    } else {
        error = 1;
    }

    free(ptr);
    if (error) {
        fprintf(stderr, "Failed to store %s: %s\n", fname,
                lcb_strerror(instance, ret));
    } else {
        remove(fname);
    }
}
Example #4
0
   robust_mutex_lock_file()
   {
      permissions p;
      p.set_unrestricted();
      //Remove old lock files of other processes
      remove_old_robust_lock_files();
      //Create path and obtain lock file path for this process
      create_and_get_robust_lock_file_path(fname, get_current_process_id());

      //Now try to open or create the lock file
      fd = create_or_open_file(fname.c_str(), read_write, p);
      //If we can't open or create it, then something unrecoverable has happened
      if(fd == invalid_file()){
         throw interprocess_exception(other_error, "Robust emulation robust_mutex_lock_file constructor failed: could not open or create file");
      }

      //Now we must take in care a race condition with another process
      //calling "remove_old_robust_lock_files()". No other threads from this
      //process will be creating the lock file because intermodule_singleton
      //guarantees this. So let's loop acquiring the lock and checking if we
      //can't exclusively create the file (if the file is erased by another process
      //then this exclusive open would fail). If the file can't be exclusively created
      //then we have correctly open/create and lock the file. If the file can
      //be exclusively created, then close previous locked file and try again.
      while(1){
         bool acquired;
         if(!try_acquire_file_lock(fd, acquired) || !acquired ){
            throw interprocess_exception(other_error, "Robust emulation robust_mutex_lock_file constructor failed: try_acquire_file_lock");
         }
         //Creating exclusively must fail with already_exists_error
         //to make sure we've locked the file and no one has
         //deleted it between creation and locking
         file_handle_t fd2 = create_new_file(fname.c_str(), read_write, p);
         if(fd2 != invalid_file()){
            close_file(fd);
            fd = fd2;
            continue;
         }
         //If exclusive creation fails with expected error go ahead
         else if(error_info(system_error_code()).get_error_code() == already_exists_error){ //must already exist
            //Leak descriptor to mantain the file locked until the process dies
            break;
         }
         //If exclusive creation fails with unexpected error throw an unrecoverable error
         else{
            close_file(fd);
            throw interprocess_exception(other_error, "Robust emulation robust_mutex_lock_file constructor failed: create_file filed with unexpected error");
         }
      }
   }
Example #5
0
static bfelf64_sword
private_get_string(struct bfelf_file_t *ef,
                   struct bfelf_shdr *strtab,
                   bfelf64_word offset,
                   struct e_string_t *str)
{
    bfelf64_sword max = strtab->sh_size - offset;

    str->buf = ef->file + strtab->sh_offset + offset;
    str->len = 0;

    for (str->len = 0; str->len < max; str->len++)
    {
        if (str->buf[str->len] == 0)
            break;
    }

    if (str->len >= max)
        goto failure;

    return BFELF_SUCCESS;

failure:

    str->buf = 0;
    str->len = 0;

    return invalid_file("the dynamic string table is corrupt");
}
Example #6
0
inline void remove_if_can_lock_file(const char *file_path)
{
   file_handle_t fhnd = open_existing_file(file_path, read_write);

   if(fhnd != invalid_file()){
      bool acquired;
      if(try_acquire_file_lock(fhnd, acquired) && acquired){
         delete_file(file_path);
      }
      close_file(fhnd);
   }
}
Example #7
0
static bfelf64_sword
private_validate_bounds(struct bfelf_file_t *ef)
{
    bfelf64_xword phtab_size = ef->ehdr->e_phoff +
                               (ef->ehdr->e_phentsize * ef->ehdr->e_phnum);
    bfelf64_xword shtab_size = ef->ehdr->e_shoff +
                               (ef->ehdr->e_shentsize * ef->ehdr->e_shnum);

    if (ef->ehdr->e_ehsize != sizeof(struct bfelf64_ehdr))
        return invalid_file("unexpected header size");

    if (ef->ehdr->e_phentsize != sizeof(struct bfelf_phdr))
        return invalid_file("unexpected program header size");

    if (ef->ehdr->e_shentsize != sizeof(struct bfelf_shdr))
        return invalid_file("unexpected section header size");

    if (phtab_size > ef->fsize)
        return invalid_file("corrupt program header table");

    if (shtab_size > ef->fsize)
        return invalid_file("corrupt section header table");

    if (ef->ehdr->e_shstrndx >= ef->ehdr->e_shnum)
        return invalid_file("section header string table index out of bounds");

    return BFELF_SUCCESS;
}
    TEST_FIXTURE(page_blob_test_base, page_blob_file_upload)
    {
        azure::storage::blob_request_options options;
        options.set_store_blob_content_md5(true);

        utility::string_t md5_header;
        m_context.set_sending_request([&md5_header] (web::http::http_request& request, azure::storage::operation_context)
        {
            if (!request.headers().match(U("x-ms-blob-content-md5"), md5_header))
            {
                md5_header.clear();
            }
        });

        temp_file invalid_file(1000);
        CHECK_THROW(m_blob.upload_from_file(invalid_file.path(), 0, azure::storage::access_condition(), options, m_context), azure::storage::storage_exception);

        temp_file file(1024);
        m_blob.upload_from_file(file.path(), 0, azure::storage::access_condition(), options, m_context);
        CHECK_UTF8_EQUAL(file.content_md5(), md5_header);

        m_context.set_sending_request(std::function<void(web::http::http_request &, azure::storage::operation_context)>());

        temp_file file2(0);
        m_blob.download_to_file(file2.path(), azure::storage::access_condition(), options, m_context);

        concurrency::streams::container_buffer<std::vector<uint8_t>> original_file_buffer;
        auto original_file = concurrency::streams::file_stream<uint8_t>::open_istream(file.path()).get();
        original_file.read_to_end(original_file_buffer).wait();
        original_file.close().wait();

        concurrency::streams::container_buffer<std::vector<uint8_t>> downloaded_file_buffer;
        auto downloaded_file = concurrency::streams::file_stream<uint8_t>::open_istream(file2.path()).get();
        downloaded_file.read_to_end(downloaded_file_buffer).wait();
        downloaded_file.close().wait();

        CHECK_EQUAL(original_file_buffer.collection().size(), downloaded_file_buffer.collection().size());
        CHECK_ARRAY_EQUAL(original_file_buffer.collection(), downloaded_file_buffer.collection(), (int)downloaded_file_buffer.collection().size());

        m_blob.properties().set_content_md5(dummy_md5);
        m_blob.upload_properties();
        options.set_retry_policy(azure::storage::no_retry_policy());
        CHECK_THROW(m_blob.download_to_file(file2.path(), azure::storage::access_condition(), options, m_context), azure::storage::storage_exception);
    }
Example #9
0
inline bool file_wrapper::priv_open_or_create
   (detail::create_enum_t type, 
    const char *filename,
    mode_t mode)
{
   m_filename = filename;

   if(mode != read_only && mode != read_write){
      error_info err(mode_error);
      throw interprocess_exception(err);
   }

   //Open file existing native API to obtain the handle
   switch(type){
      case detail::DoOpen:
         m_handle = open_existing_file(filename, mode);
      break;
      case detail::DoCreate:
         m_handle = create_new_file(filename, mode);
      break;
      case detail::DoOpenOrCreate:
         m_handle = create_or_open_file(filename, mode);
      break;
      default:
         {
            error_info err = other_error;
            throw interprocess_exception(err);
         }
   }

   //Check for error
   if(m_handle == invalid_file()){
      throw interprocess_exception(error_info(system_error_code()));
   }

   m_mode = mode;
   return true;
}
Example #10
0
static bfelf64_sword
private_check_entry(struct bfelf_file_t *ef)
{
    bfelf64_sword i = 0;
    bfelf64_sword valid_addr = 0;

    for (i = 0; i < ef->ehdr->e_phnum; i++)
    {
        struct bfelf_phdr *phdr = private_get_segment(ef, i);

        if (ef->ehdr->e_entry >= phdr->p_vaddr &&
            ef->ehdr->e_entry < phdr->p_vaddr + phdr->p_memsz)
        {
            valid_addr = 1;
            break;
        }
    }

    if (ef->ehdr->e_entry != 0 && valid_addr == 0)
        return invalid_file("ELF entry corrupt");

    return BFELF_SUCCESS;
}
Example #11
0
static bfelf64_sword
private_symbol_table_sections(struct bfelf_file_t *ef)
{
    bfelf64_sword i = 0;
    bfelf64_sword ret = 0;

    for (i = 0; i < ef->ehdr->e_shnum; i++)
    {
        struct bfelf_shdr *shdr = private_get_section(ef, i);

        if (shdr->sh_type == bfsht_dynsym)
        {
            ret = private_check_section(shdr, bfsht_dynsym, bfshf_a, 8,
                                        sizeof(struct bfelf_sym));
            if (ret != BFELF_SUCCESS)
                return ret;

            ef->dynsym = shdr;
            continue;
        }

        if (shdr->sh_type == bfsht_hash)
        {
            ret = private_check_section(shdr, bfsht_hash, bfshf_a, 8, 0x04);
            if (ret != BFELF_SUCCESS)
                return ret;

            ef->hashtab = shdr;
            continue;
        }
    }

    if (!ef->dynsym)
        return invalid_file("unable to locate dynammic symbol table");

    return BFELF_SUCCESS;
}
Example #12
0
inline bool robust_spin_mutex<Mutex>::is_owner_dead(boost::uint32_t own)
{
   //If owner is an invalid id, then it's clear it's dead
   if(own == (boost::uint32_t)get_invalid_process_id()){
      return true;
   }

   //Obtain the lock filename of the owner field
   std::string file;
   this->owner_to_filename(own, file);

   //Now the logic is to open and lock it
   file_handle_t fhnd = open_existing_file(file.c_str(), read_write);

   if(fhnd != invalid_file()){
      //If we can open the file, lock it.
      bool acquired;
      if(try_acquire_file_lock(fhnd, acquired) && acquired){
         //If locked, just delete the file
         delete_file(file.c_str());
         close_file(fhnd);
         return true;
      }
      //If not locked, the owner is suppossed to be still alive
      close_file(fhnd);
   }
   else{
      //If the lock file does not exist then the owner is dead (a previous cleanup)
      //function has deleted the file. If there is another reason, then this is
      //an unrecoverable error
      if(error_info(system_error_code()).get_error_code() == not_found_error){
         return true;
      }
   }
   return false;
}