Beispiel #1
0
void recursive_add_files(char* dir_path, struct queue_head* file_queue){
  fprintf(stderr,"Reading directory path %s\n",dir_path);
  DIR* dir = opendir(dir_path);
  
  struct dirent * f;
  while((f = readdir(dir))!=NULL){
    if(strcmp(".",f->d_name) == 0 || strcmp("..",f->d_name) == 0 || f->d_name[0] == '.')
      continue;

    char path[MAX_FILE_NAME];
    int len = snprintf(path,sizeof(path)-1,"%s/%s",dir_path,f->d_name);
    path[len]='\0';

    if(f->d_type == DT_DIR) {
      recursive_add_files(path,file_queue);
      continue;
    }

    if(!DT_REG == f->d_type){
      continue;   
    }

    char* file_path = strndup(path,MAX_FILE_NAME);
    struct queue* node = queue_create_node(file_path,strlen(file_path)+1);    
    queue_prepend(file_queue,node);
  }

  closedir(dir);  

}
Beispiel #2
0
int minisocket_receive(minisocket_t *socket, char *msg, int max_len, minisocket_error *error) {
  if (socket == NULL || msg == NULL || max_len < 0) {
    *error = SOCKET_INVALIDPARAMS;
    return -1;
  }
  if (socket->state == CLOSED) {
    *error = SOCKET_RECEIVEERROR;
    return -1;
  }
  if (max_len == 0) {
    return 0;
  }
  
  int received = 0;
  while (!received) {
    semaphore_P(socket->datagrams_ready);

    network_interrupt_arg_t *interrupt_message = NULL;
    interrupt_level_t old_level = set_interrupt_level(DISABLED);
    queue_dequeue(socket->incoming_data, (void **) &interrupt_message);
    set_interrupt_level(old_level); 

    if (interrupt_message != NULL) {
      mini_header_reliable_t* received_header = (mini_header_reliable_t *) interrupt_message->buffer; 
      network_address_t temp_address;
      unpack_address(received_header->source_address, temp_address);
      if (socket->remote_port_number == unpack_unsigned_short(received_header->source_port) &&
        network_compare_network_addresses(socket->remote_address, temp_address) != 0 &&
        received_header->message_type == MSG_ACK && interrupt_message->size > sizeof(mini_header_reliable_t)) {
        //same address, same ports, right message
        received = 1; 
        int data_left = interrupt_message->size - sizeof(mini_header_reliable_t) - socket->next_read;
        interrupt_level_t old_level = set_interrupt_level(DISABLED);
        
        if (data_left <= max_len) {
          memcpy(msg, interrupt_message->buffer + sizeof(mini_header_reliable_t) + socket->next_read, data_left);
          socket->next_read = 0;
          free(interrupt_message);
          set_interrupt_level(old_level); // must protect global data field next_read 
          return data_left;
        }
        else {
          memcpy(msg, interrupt_message->buffer + sizeof(mini_header_reliable_t) + socket->next_read, max_len);
          socket->next_read += max_len;
          queue_prepend(socket->incoming_data, interrupt_message); //ack works as well when there is data 
          semaphore_V(socket->datagrams_ready); //another message in queue, V semaphore
          set_interrupt_level(old_level); 
          return max_len;
        }
      }
      else {
        free(interrupt_message);
      }
    }
  }
  return -1;
}
Beispiel #3
0
struct queue* find_file_paths(void* obj, int id, void* priv, struct queue_head* in_q,struct queue_head* file_queue)
{

  char* read_from = priv;
  // add files to file_queue
  if(strcmp(read_from,"-") == 0) { // read a list of files from stdin
    char* filename = NULL;
    size_t sz = 1024;
    while(getline(&filename,&sz,stdin) > 0 ) {      
      int l = strlen(filename);
      //  printf("file [%s] last char[%c]\n",filename,filename[l-1]);
      //Remove new line
      if(filename[l-1] == '\n') {
        filename[l-1] = '\0';
      }

      char* f = strdup(filename);
      int fl = strlen(f);
      struct queue* node = queue_create_node(f,fl+1);
      queue_prepend(file_queue,node);
    }
  } else{
    struct stat file_info;
    if(stat(read_from,&file_info)!=0) {
      fprintf(stderr, "Couldnt locate file %s\n",read_from);
      perror("fstat");
      return NULL;
    }

    if(S_ISDIR(file_info.st_mode)){
      recursive_add_files(read_from,file_queue);
    } else {    //regular file
      struct queue* node = queue_create_node(read_from,strlen(read_from)+1);
      queue_prepend(file_queue,node);
    }
  }

  // we are done filling this queue
  queue_mark_finish_filling(file_queue);

  return NULL;
}
/*
 * Return the first item from the highest level that is not higher
 * than the specified level - just like dequeue, with the exception
 * that the item remains in the queue.
 * Return 0 (success) and the dequeued item if the multilevel queue is
 * valid and nonempty.
 * Return -1 (failure) and NULL if the multilevel queue is invalid or
 * empty.
 */
int multilevel_queue_peak(multilevel_queue_t obj, int level, any_t* item_p) {
	any_t item = NULL;
	int ret = queue_dequeue(obj->levels[level], &item);
	while ( (level < (obj->num - 1)) && -1 == ret ) {
		level++;
		ret = queue_dequeue(obj->levels[level], &item);
	}	
	if ( ret != -1 ) {
		queue_prepend(obj->levels[level], item);
		*item_p = item;
	}
	return ret;
}
Beispiel #5
0
/****************************************************************
  Key press for source
*****************************************************************/
static gboolean src_key_press_callback(GtkWidget *w, GdkEventKey *ev,
				       gpointer data)
{
  struct worklist_data *ptr;
    
  ptr = data;

  if (!gtk_widget_is_sensitive(ptr->dst_view)) {
    return FALSE;
  }
  
  if ((ev->state & GDK_SHIFT_MASK) && ev->keyval == GDK_KEY_Insert) {
    queue_prepend(ptr);
    return TRUE;
  } else if (ev->keyval == GDK_KEY_Insert) {
    queue_append(ptr);
    return TRUE;
  } else {
    return FALSE;
  }
}
Beispiel #6
0
void interrupt_handler(void* arg)
{
	// disable interrupts
	interrupt_level_t prev_int_level = set_interrupt_level(DISABLED);
	int context_switch_ret;
	alarm_t next_alarm;

	ticks++;

	// Trigger any alarms that need to be triggered
	if (queue_length(alarm_queue) > 0)
	{
		do
		{
			next_alarm = NULL; // not necessary, but included for clarity
			queue_dequeue(alarm_queue, (void**) &next_alarm);
			if (next_alarm != NULL)
			{
				if (next_alarm->tick_time <= ticks)
				{
					//minithread_fork((proc_t) next_alarm->func, next_alarm->arg);
					(next_alarm->func)(next_alarm->arg);

					free(next_alarm);
				}
				 else
				{
					queue_prepend(alarm_queue, next_alarm);
				}
			}
		} while (next_alarm != NULL && next_alarm->tick_time <= ticks);
	}


	// If our time at this level of the multilevel queue is up
	if (--ticks_until_next_level == 0)
	{
		// Recalculate the current level
		current_level = (current_level + 1 ) % 4;

		// Set the length of time we have to spend at the next level
		switch (current_level)
		{
			case 0:
				ticks_until_next_level = 80;
				break;

			case 1:
				ticks_until_next_level = 40;
				break;

			case 2:
				ticks_until_next_level = 24;
				break;

			case 3:
				ticks_until_next_level = 16;
				break;
		}
	}

	// If the running thread has exceeded its allocated quanta
	if (--ticks_until_switch == 0)
	{
		// Decrement (or really increment) its priority, if its not lowest yet
		if (running->priority < 3)
		{
			running->priority++;
		}

		// Attempt a context switch - add to ready queue, can switch to idle
		context_switch_ret = minithread_context_switch(1, 1);
		if (context_switch_ret == -1) // Check if the next thread is "running"
		{
			// If so, no need to context switch
			ticks_until_switch = minithread_get_priority_quanta(running->priority);
		}
	}

	// set interrupts back to previous value
	set_interrupt_level(prev_int_level);
}
Beispiel #7
0
queue_t
*read_rbfsum2 (char *buffer)
{
  queue_t *list = NULL;
  char *tmp = buffer;
  char *name;
  char *start, *end;
  char *file_line;
  char *line;
  rbfsum_t rb;
  unsigned int id;
  station_data_t *data;

  while (*tmp)
    {
      file_line = get_line (tmp);
      if (!file_line)
        {
          tmp = next_line (tmp);
          continue;
        }

      if (*file_line == '/')
        {
          start = strrchr (file_line, '/');
          start++;
          end = strrchr (start, '.');
          if (!end)
            {
              tmp = next_line (tmp);
              continue;
            }

          name = xstrndup (start + 1, end - start - 1);
          free (file_line);

          tmp = next_line (tmp);
          if (*tmp == '\0')
            {
              free (name);
              break;
            }

          tmp = next_line (tmp);
          if (*tmp == '\0')
            {
              free (name);
              break;
            }

          data = malloc (sizeof(station_data_t));
          data->name = name;

          id = 0;
          while ((line = get_line (tmp)))
            {
              if (parse_data (line, &rb))
                {
                  memcpy (&data->rs[id], &rb, sizeof(rb));
                  id++;
                }
              free (line);
              tmp = next_line (tmp);
            }
          data->ndata = id;
          list = queue_prepend (list, data);
        }
      tmp = next_line (tmp);
    }

  list = queue_sort (list, comp_func);
  return list;
}
Beispiel #8
0
int main(){
  int counter;
  int arraya[5] = {1,2,3,4,5};
  int arrayb[] = {3,4,5};
  int arrayc[] = {2,4,5};
  int* inty = malloc(sizeof(int *));
  int** item1 = malloc(sizeof(int **));
  int** item2 = malloc(sizeof(int **));
  int iny = 3;

  queue_t queue = queue_new();
  printf("prepend prior\n");
  queue_prepend(queue, &iny);
  printf("prepend post\n");
  int inty1 = 2;
  queue_prepend(queue, &inty1);
  printf("prepend post\n");
  int inty2 = 1;
  queue_prepend(queue, &inty2);
  int inty3 = 4;
  queue_append(queue, &inty3);
  printf("prepend post\n");
  int inty4 = inty3+1;
  queue_append(queue, &inty4);
  if (test(queue,arraya,5) == 0){
    printf("test 1 PASSED\n");
    }
  else {
    printf("test 1 FAILED\n");
    }
  queue_dequeue(queue,(void **)item1);  
  queue_dequeue(queue,(void **)item2);
  if (test(queue,arrayb,3) == 0 && **item1 == 1 && **item2 == 2){
    printf("test 2 PASSED\n");
    }
  else {
    printf("test 2 FAILED\n");
    }
  queue_prepend(queue, &inty1);
  *inty = 0;
  queue_iterate(queue,iter_tester,(void *)inty);
  if (*inty == 14){
    printf("test 3 PASSED\n");
    }
  else{
    printf("test 3 FAILED, total = %i\n",*inty);
    }
  queue_delete(queue, &iny);
  if (test(queue,arrayc,3) == 0){
    printf("test 4 PASSED\n");
    }
  else {
    printf("test 4 FAILED\n");
    }
  queue_t q = queue_new();
  for(counter=0;counter<100;counter++){
    printf("Index %i\n",counter);
    queue_append(q,&counter);
    }
  if (q->length = 100){
    printf("test 5 PASSED\n");
    }
  else {
    printf("test 5 FAILED\n");
    }
  queue_free(q);
  queue_free(queue);
  free(inty);
  free(item1);
  free(item2);
  }
Beispiel #9
0
int minisocket_receive(minisocket_t *socket, char *msg, int max_len, minisocket_error *error)
{  
  if (socket->socket_state == CLOSED || socket->socket_state == CLOSING) {
    *error=SOCKET_RECEIVEERROR;
    return 0;
  }
  if (socket->socket_state != OPEN || !msg) {
    *error = SOCKET_INVALIDPARAMS;
    return -1;
  } 

  if(max_len == 0)
  {
    *error = SOCKET_NOERROR;
    return 0;
  }
  semaphore_P(socket->send_receive_mutex);
  semaphore_P(socket->data_ready);
  if (socket->socket_state == CLOSED || socket->socket_state == CLOSING) {
    *error=SOCKET_RECEIVEERROR;
    return 0;
  }

  network_interrupt_arg_t *arg;
  queue_dequeue(socket->data, (void **) &arg);
  int msg_len = arg->size - sizeof(mini_header_reliable_t);
  assert(msg_len > 0);

  int copy_len = ( msg_len > max_len) ? max_len : msg_len;

  for(int i=0;i<copy_len;i++)
  {
    msg[i] = arg->buffer[sizeof(mini_header_reliable_t) + i];
  }

  //If the packet contained more data than the buffer could take, then enqueue the packet with the remaining message again
  if(copy_len > max_len)
  {
    mini_header_reliable_t *h = (mini_header_reliable_t *) malloc(sizeof(mini_header_reliable_t));

    if(!h)
    {
      *error = SOCKET_OUTOFMEMORY;
      semaphore_V(socket->send_receive_mutex);
      return -1;
    }

    
    mini_header_reliable_t *header = (mini_header_reliable_t *) arg->buffer;
    //Update the sequence number in the header
    unsigned int seq_no = unpack_unsigned_int(header->seq_number);
    pack_unsigned_int(header->seq_number, seq_no + max_len);
    //Copy the remaining message to the start of the message space in the buffer
    memcpy(arg->buffer + sizeof(mini_header_reliable_t), arg->buffer + sizeof(mini_header_reliable_t) + max_len, msg_len - max_len);
    //Set the packet size to its correct length
    arg->size = msg_len + sizeof(mini_header_reliable_t);
    //Prepend the packet in the data queue
    queue_prepend(socket->data, &arg);
  }
  semaphore_V(socket->send_receive_mutex);
  return copy_len;
}