Example #1
0
int main(int argc, char *argv[])
{
	dbhead_t 	*dbh;
	int	rec_num;

	if (argc < 2) {
		printf(USAGE);
		exit(1);
	}
	argv++;	argc--;
	if ((dbh = dbf_open(*argv, O_RDWR)) == NULL) {
		fprintf(stderr, "unable to open database file %s\n", *argv);
		exit(1);
	}
	argv++;	argc--;

	while (argc-- > 0) {
		rec_num = strtol(*argv++, NULL, 0);
		if (del_dbf_record(dbh, rec_num) < 0) {
			if (rec_num > dbh->db_records) {
				fprintf(stderr, "record %d out of bounds\n",
						 rec_num);
				continue;
			} else {
				fprintf(stderr, "unable to delete record %d\n",
						 rec_num);
			}
		}
	}
	put_dbf_info(dbh);
	exit(0);
}
Example #2
0
static bool dbase_add_or_replace_record(int dbase_identifier, const Variant& record, boost::optional<int64_t> recnum) {
  if (!record.isArray()) {
    raise_warning("Argument two must be of type 'Array'");
    return false;
  }

  open_db_map::iterator it = dbase_find_connection(dbase_identifier);
  if (it != open_dbases->end()) {
    dbhead_t* dbh = it->second->dbh;
    assert(dbh != nullptr);
    Array arr_record = record.toArray();

    ssize_t num_fields = arr_record.size();

    if (num_fields != dbh->db_nfields) {
      raise_warning("Wrong number of fields specified");
      return false;
    }

    char* cp = (char*)malloc(dbh->db_rlen + 1);
    char* t_cp = cp;
    *t_cp++ = VALID_RECORD;

    dbfield_t* dbf = dbh->db_fields;
    dbfield_t* cur_f = dbf;
    for (ArrayIter arr_it(arr_record); arr_it; ++arr_it, cur_f++) {
      assert(cur_f < &dbf[num_fields]);
      //snprintf(t_cp, cur_f->db_flen+1, cur_f->db_format, Z_STRVAL(tmp));
      snprintf(t_cp, cur_f->db_flen+1, cur_f->db_format, arr_it.second().toString().c_str());
      t_cp += cur_f->db_flen;
    }

    int put_recnum;
    if (recnum) {
      put_recnum = recnum.get();
    } else {
      put_recnum = dbh->db_records++;
    }

    if (put_dbf_record(dbh, put_recnum, cp) < 0) {
      raise_warning("unable to put record at %d", put_recnum);
      free(cp);
      return false;
    }

    put_dbf_info(dbh);
    free(cp);

    return true;
  } else {
    return false;
  }
}
Example #3
0
static bool HHVM_FUNCTION(dbase_pack, int64_t dbase_identifier) {
  open_db_map::iterator it = dbase_find_connection(dbase_identifier);
  if (it != open_dbases->end()) {
    dbhead_t* dbh = it->second->dbh;
    assert(dbh != nullptr);
    pack_dbf(dbh);
    put_dbf_info(dbh);
    return true;
  } else {
    return false;
  }
}
Example #4
0
static bool HHVM_FUNCTION(dbase_delete_record, int64_t dbase_identifier, int64_t record_number) {
  open_db_map::iterator it = dbase_find_connection(dbase_identifier);
  if (it != open_dbases->end()) {
    dbhead_t* dbh = it->second->dbh;
    assert(dbh != nullptr);
    if (record_number > dbh->db_records) {
      raise_warning("record %" PRId64 " out of bounds", record_number);
      return false;
    }
    if (del_dbf_record(dbh, record_number) < 0) {
      raise_warning("unable to delete record %" PRId64, record_number);
      return false;
    }
    put_dbf_info(dbh);
    return true;
  } else {
    return false;
  }
}
Example #5
0
static Variant HHVM_FUNCTION(dbase_create, const String& filename, const Variant& fields) {
  if (!fields.isArray()) {
    raise_warning("Expected array as second parameter");
    return false;
  }

  //if (php_check_open_basedir(Z_STRVAL_PP(filename) TSRMLS_CC)) {
  //  RETURN_FALSE;
  //}

  int fd;
  if ((fd = open(filename.c_str(), O_BINARY|O_RDWR|O_CREAT, 0644)) < 0) {
    raise_warning("Unable to create database (%d): %s", errno, strerror(errno));
    return false;
  }

  Array arr_fields = fields.toArray();
  ssize_t num_fields = arr_fields.size();
  if (num_fields <= 0) {
    raise_warning("Unable to create database without fields");
    close(fd);
    return false;
  }

  // have to use regular malloc() because this gets free()'d by
  // code in the dbase library.
  dbhead_t* dbh = (dbhead_t*)malloc(sizeof(dbhead_t));
  dbfield_t* dbf = (dbfield_t*)malloc(sizeof(dbfield_t) * num_fields);
  if ((dbh == nullptr) || (dbf == nullptr)) {
    raise_warning("Unable to allocate memory for header info");
    if (dbh != nullptr) {
      free(dbh);
    }
    if (dbf != nullptr) {
      free(dbf);
    }
    close(fd);
    return false;
  }

  // This will ensure close(fd) and free_dbf_head(dbh) on "return false".
  DBaseConnection dbc(dbh);

  // initialize the header structure
  dbh->db_fields = dbf;
  dbh->db_fd = fd;
  dbh->db_dbt = DBH_TYPE_NORMAL;
  strcpy(dbh->db_date, "19930818");
  dbh->db_records = 0;
  dbh->db_nfields = num_fields;
  dbh->db_hlen = sizeof(struct dbf_dhead) + 1 + num_fields * sizeof(struct dbf_dfield);

  int rlen = 1;
  // make sure that the db_format entries for all fields are set to NULL to ensure we
  // don't seg fault if there's and error and we need to call free_dbf_head() before all
  // fields have been defined.
  dbfield_t* cur_f = dbf;
  for (size_t i = 0; i < num_fields; i++, cur_f++) {
    cur_f->db_format = nullptr;
  }

  cur_f = dbf;
  int i = 0;
  for (ArrayIter arr_it(arr_fields); arr_it; ++arr_it, cur_f++, i++) {
    Array& field = arr_it.second().toArrRef();
    ArrayIter field_it(field);

    // field name
    if (!field_it) {
      raise_warning("expected field name as first element of list in field %d", i);
      return false;
    }
    const String& field_name = field_it.second().toCStrRef();
    if ((field_name.size() > 10) || (field_name.size() == 0)) {
      raise_warning("invalid field name '%s' (must be non-empty and less than or equal to 10 characters)", field_name.c_str());
      return false;
    }
    strncpy(cur_f->db_fname, field_name.c_str(), field_name.size()+1);

    // field type
    ++field_it;
    if (!field_it) {
      raise_warning("expected field type as second element of list in field %d", i);
      return false;
    }
    cur_f->db_type = toupper(field_it.second().toCStrRef().c_str()[0]);

    cur_f->db_fdc = 0;

    // verify the field length
    switch (cur_f->db_type) {
    case 'L':
      cur_f->db_flen = 1;
      break;
    case 'M':
      cur_f->db_flen = 10;
      dbh->db_dbt = DBH_TYPE_MEMO;
      // should create the memo file here, probably
      break;
    case 'D':
      cur_f->db_flen = 8;
      break;
    case 'F':
      cur_f->db_flen = 20;
      break;
    case 'N':
    case 'C':
      // field length
      ++field_it;
      if (!field_it) {
        raise_warning("expected field length as third element of list in field %d", i);
        return false;
      }
      cur_f->db_flen = field_it.second().toInt32();

      if (cur_f->db_type == 'N') {
        ++field_it;
        if (!field_it) {
          raise_warning("expected field precision as fourth element of list in field %d", i);
          return false;
        }
      }
      break;
    default:
      raise_warning("unknown field type '%c'", cur_f->db_type);
      return false;
    }
    cur_f->db_foffset = rlen;
    rlen += cur_f->db_flen;

    cur_f->db_format = get_dbf_f_fmt(cur_f);
  }

  dbh->db_rlen = rlen;
  put_dbf_info(dbh);

  // We need a copy of dbc here, because return will destroy original.
  open_dbases->insert(std::make_pair(dbh->db_fd, std::shared_ptr<DBaseConnection>(new DBaseConnection(dbc))));
  return Variant(dbh->db_fd);
}