Ejemplo n.º 1
0
int EL_Log::init(const char *dir, const char *name_base, long file_size, long maintain)
{
	if (strlen(dir) <= 0 || strlen(name_base) <= 0)
		return -1;

	if (strlen(dir) >= 256 || strlen(name_base) >= 256)
		return -1;

	strcpy(this->dir, dir);
	strcpy(this->name_base, name_base);
	this->file_size = file_size;
	this->maintain = maintain;

	log_fp = NULL;
	current_num = 0;

	if (find_exist_file() < 0) {
		if (create_new_file() < 0) {
			return -1;
		}
	}

	config(FATAL, STDERR | LOGFILE);
	config(ERROR, STDERR | LOGFILE);
	config(WARN, STDOUT | LOGFILE);
	config(INFO, STDOUT);
	config(DEBUG, STDOUT);

	return 0;
}		// -----  end of function init  -----
Ejemplo n.º 2
0
Archivo: exec.c Proyecto: Vuldo/42sh
int     check_ops2(char *str, t_tree *tree)
{
  if (my_strcmp(str, ">") == 0)
    create_new_file(tree);
  else if (my_strcmp(str, ">>") == 0)
    create_new_file(tree);
  else if (my_strcmp(str, "<") == 0)
    open_new_file(tree);
  else
    return (1);
  if (tree->fd[1] < 0)
    {
      my_printf("Can't open %s\n", tree->right->str);
      tree->fd[1] = -1;
    }
  return (0);
}
Ejemplo n.º 3
0
DocumentView::DocumentView(Window& window):
    window_(window) {

    create_buffer();

    build_widgets();
    connect_signals();

    create_new_file();
}
Ejemplo n.º 4
0
void watch_events(void)
{
	GNode *modified_directory, *removed_file;
	File *new_file;
	gboolean refresh_request = FALSE;
	struct timeval time;
	struct inotify_event *event;
	int n, i;
	fd_set fds;

	CHECK_INOTIFY_ENABLED();
again:	time.tv_sec = 0;
	time.tv_usec = 0;
	FD_ZERO(&fds);
	FD_SET(inotify_descriptor, &fds);
	while ((n = select(inotify_descriptor + 1, &fds, NULL, NULL, &time)) == 1) {
again2:		if ((n = read(inotify_descriptor, events_buffer, BUFFER_LENGTH)) > 0) {
			for (i = 0; i < n; i += sizeof(struct inotify_event) + event->len) {
				event = (struct inotify_event *) (events_buffer + i);
				if (debug_inotify == TRUE)
					dump_event(event);
				modified_directory = (GNode *) g_hash_table_lookup(
						watches_table, &(event->wd));

				if (event->mask & IN_CREATE || event->mask & IN_MOVED_TO) {
					if ((new_file = create_new_file(event->name,
									get_path(modified_directory), FALSE)) != NULL)
						refresh_request |= insert_in_tree(modified_directory, new_file);
				} else if (event->mask & IN_DELETE || event->mask & IN_MOVED_FROM) {
					if ((removed_file = search_node_by_name(modified_directory,
									event->name)) != NULL)
						refresh_request |= remove_from_tree(removed_file, FALSE);
				} else if (event->mask & IN_UNMOUNT) {
					refresh_request |= remove_from_tree(modified_directory, TRUE);
				}
			}
		} else if (n == -1) {
			if (errno == EINTR)
				goto again2;
			else
				PRINT_ERRNO_AND_EXIT();
		}
	}
	if (n == -1) {
		if (errno == EINTR)
			goto again;
		else
			PRINT_ERRNO_AND_EXIT();
	}
	if (refresh_request == TRUE)
		refresh_screen();
}
Ejemplo n.º 5
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");
         }
      }
   }
Ejemplo n.º 6
0
void DocumentView::set_file(GioFilePtr file) {
    disconnect_file_monitor();

    if(!file) {
        create_new_file();
    }

    file_ = file;

    if(!is_new_file()) {
        connect_file_monitor();
        reload();
    } else {
        file_etag_ = ""; //Wipe out the etag if this is a new file
    }
}
Ejemplo n.º 7
0
int EL_Log::ifatal(const char *message, int line, const char *file, const char *func, ...)
{
	char buf[BUFSIZ];
	time_t t = 0;
	struct tm tm;

	time(&t);
	localtime_r(&t, &tm);

	sprintf(buf, "[fatal][%04d-%02d-%02d %02d:%02d:%02d][%s:%d (%s)]:%s\n",
			tm.tm_year+1900, tm.tm_mon, tm.tm_mday,
			tm.tm_hour, tm.tm_min, tm.tm_sec,
			file, line, func,
			message);

	va_list argp;
	int ret = -1;

	if (output_style[FATAL] & STDOUT) {
		va_start(argp, func);
		ret = vfprintf(stdout, buf, argp);
	}
	if (output_style[FATAL] & STDERR) {
		va_start(argp, func);
		ret = vfprintf(stderr, buf, argp);
	}
	if (output_style[FATAL] & LOGFILE) {
		locker.lock();

		if (current_size > file_size) {
			if (create_new_file() < 0)
				return -1;
		}

		va_start(argp, func);
		ret = vfprintf(log_fp, buf, argp);
		fflush(log_fp);
		current_size += ret;

		locker.unlock();
	}

	va_end(argp);

	return ret;
}		// -----  end of function fatal  -----
Ejemplo n.º 8
0
si_t
right_click_menu_NEW_FILE
(void * bt,
 void * msg)
{
    switch(message_get_type(msg))
    {
    	case MESSAGE_TYPE_MOUSE_PRESS:
			save_or_rename_flag=1;
            create_new_file();
			pop_window();
			desktop_flush();
			right_click_menu_cancel();
    	    break;

        default:
            button_default_callback(bt, msg);
            break;
    }
    return 0;
}
Ejemplo n.º 9
0
int write_to_file(void)
{
	float param_value;
    int i, j=0;
    char *c_date;
    char c_time[11];
	
	if(new_file == 1) {
		if(create_new_file() < 0) {
			fprintf(stderr, "Failed to create new file.\n");
			return -1;
		} else {
			new_file = 0;
			write_in_progress = 1;
		}
    }
    
    c_date = get_current_time();
    while (j < 8) {
		c_time[j] = c_date[j+11];
		j++;
	}
	c_time[8] = '\0';
    
    fprintf(fp, "%s\t", c_time);
    
    for(i=19; i<=44; i++) {
	   param_value = rp_main_params[i].value;
	   fprintf(fp, "%f\t", param_value);
	}
	for(i=PARAMS_HARM_U_PARAMS; i<PARAMS_NUM; i++) {
	   param_value = rp_main_params[i].value;
	   fprintf(fp, "%f\t", param_value);
	}
	fprintf(fp, "\n");
	
	return 0;
	
}
Ejemplo n.º 10
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;
}
Ejemplo n.º 11
0
void pcap_callback(u_char *user, const struct pcap_pkthdr *h, const u_char *packet) 
{	
	
	char * pbuf = buf_header;	//헤더 정보	
	register int i = 0;	
	
	//usleep(0.1);
	
	// 시간 체크
	time(&t);
	tm=localtime(&t);
	
	if(tm->tm_sec == 0 && tm->tm_min == 0 && start == 1){
		create_new_file();
		start = 0;
	}
	
	if(tm->tm_sec == 1 && tm->tm_min == 0){		
		start = 1;
	}
	
	
	// 트래픽 계산
	traffic.Byte += h->len;	
	cal_traffic();
	
	//s += h->len;
	
	// 이더넷 헤더
	ep = (struct ether_header *)packet;
	
	// IP헤더 가져오기 위해 이더넷 헤더 만큼 offset
	packet += sizeof(struct ether_header);
	
	// 이더넷 헤더 타입
  ether_type = ntohs(ep->ether_type);
  
	
	// @ IP protocol
	iph = (struct ip *)packet;
	uint32_t d = (iph->ip_dst).s_addr;
	uint32_t s = (iph->ip_src).s_addr;
  if (ether_type == ETHERTYPE_IP && d != pre_ip_dst && s != pre_ip_src){
  	
  	pre_ip_dst = (iph->ip_dst).s_addr;
  	pre_ip_src = (iph->ip_src).s_addr;
  	
  	
  	///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // @ TCP protocol
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////  	
    
    tcph = (struct tcphdr *)(packet + iph->ip_hl * 4);
    
    int x = tcph->psh;        
		if (iph->ip_p == IPPROTO_TCP  && (x == 1)){
			
			
    	// @point : http header 
      packet += sizeof(struct tcphdr) + 20; // 20 tcp header
      
    	
    	///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    	// @ HTTP protocol
    	///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    	if(tcph->dest == htons(80)){
    		
    		
    		if((Host = strstr(packet+300, "Host: ")) != NULL){    			
    			
    			int i = 0;
    			Host += 6;
    			while( *Host != '\r' && *Host != 0 )
    				url_host[i++] = *Host++;    				
    			
    			url_host[i] = 0;
    			puts(url_host);
    			
    			Log_write(pbuf, url_host);
    			
    			
    			write(fd, buf_header, strlen(buf_header));
    			
    			
    		}
    		
    	}    	
    	///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    	// @ NOT HTTP protocol
    	///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    	else{
    		
    		Log_write(pbuf, "TCP");
    			
    		write(fd, buf_header, strlen(buf_header));
    		
 //   		printf("%s", buf_header);
   	
    	}   	
    }
    
  
  	///////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// @ UDP protocol
  	///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  	else if(iph->ip_p == IPPROTO_UDP){  		
  		
  		Log_write(pbuf, "UDP");
  		
    	write(fd, buf_header, strlen(buf_header));  	
    }
    
    
    
  }  
  
  
  
  printf("%s", buf_header);
 // printf("%d %d %d %d	%d	%d\n",traffic.GByte,traffic.MByte,traffic.KByte,traffic.Byte, h->len, s);
}
Ejemplo n.º 12
0
int
main(int argc, char **argv)
{
	char	*files[MAX_FILE_NAMES];
	char	*name;
	char	*new_name_end;
	int	new_name_len;
	struct stat stat_buf;
	int	in;
	int	in_size;
	int	ret;
	ProDisk	*disk;
	char	new_name[128];
	byte	in_buf[1024];
	int	disk_size;
	int	i;
	int	done;
	int	pos;
	int	size;
	int	num_files;
	int	file_type;
	int	aux_type;

	disk_size = DEF_DISK_SIZE;
	if(argc < 2) {
		fprintf(stderr, "%s: {-[size in K]} {unix_files}\n",
			argv[0]);
		exit(1);
	}

	num_files = 0;
	for(i = 1; i < argc; i++) {
		if(argv[i][0] == '-') {
			/* I smell a -size_in_K */
			disk_size = strtoul(&(argv[i][1]), 0, 10) * 1024;
			printf("disk_size: %d, 0x%x\n", disk_size, disk_size);
			if(disk_size < 40*1024) {
				printf("Too small!\n");
				exit(1);
			}
		} else {
			files[num_files] = argv[i];
			num_files++;
			if(num_files >= MAX_FILE_NAMES) {
				printf("Too many filenames: %d\n", num_files);
				exit(2);
			}
		}
	}

	disk = allocate_memdisk(out_name, disk_size);
	format_memdisk(disk, out_name);

	for(i = 0; i < num_files; i++) {
		name = files[i];
		in = open(name, O_RDONLY | O_BINARY);
		if(in < 0) {
			fprintf(stderr, "opening %s returned %d, errno: %d\n",
				name, in, errno);
			exit(1);
		}

		ret = fstat(in, &stat_buf);
		if(ret != 0) {
			fprintf(stderr, "fstat returned %d, errno: %d\n",
				ret, errno);
		}

		in_size = stat_buf.st_size;
		printf("in size: %d\n", in_size);

		if(in_size > disk->disk_bytes_left) {
			printf("File bigger than %d, too big!\n", disk_size);
			exit(2);
		}

		make_legal_prodos_name(new_name, name);

		new_name_len = strlen(new_name);
		new_name_end = new_name + new_name_len;

		file_type = g_def_file_type;
		aux_type = g_def_aux_type;
		while(g_def_file_type < 0) {
			/* choose file type */
			if(new_name_len >= 5) {
				if(strcmp(new_name_end - 4, ".SHK") == 0) {
					file_type = 0xe0;
					aux_type = 0x8002;
					break;
				}
				if(strcmp(new_name_end - 4, ".SDK") == 0) {
					file_type = 0xe0;
					aux_type = 0x8002;
					break;
				}
			}
			file_type = 0x04;	/* TXT */
			aux_type = 0;
			break;
		}

		create_new_file(disk, 2, 1, new_name, file_type,
			0, 0, 0, 0xc3, aux_type, 0, in_size);


		done = 0;
		pos = 0;
		while(pos < in_size) {
			size = 512;
			if(pos + size > in_size) {
				size = in_size - pos;
			}
			ret = read(in, in_buf, size);
			if(ret != size || ret <= 0) {
				fprintf(stderr, "read returned %d, errno: %d\n",
					ret, errno);
				exit(2);
			}
			ret = pro_write_file(disk, in_buf, pos, size);
			if(ret != 0) {
				printf("pro_write returned %d!\n", ret);
				exit(3);
			}
			pos += size;
		}

		close_file(disk);

		close(in);
	}

	flush_disk(disk);
	return 0;
}
Ejemplo n.º 13
0
static int callback_rename(const char *from, const char *to)
{
  /*
   * We could simply return EXDEV, which means renaming ist not possible
   * because source and destination are on different file systems.
   * The calling program will then fall back to copy+delete.
   * However, when source and destination are in the same directory, EXDEV
   * makes no sense and not all programs can handle it.
   * 
   */
   
  char *vdir_from;
  char *vdir_to;
  int same_dir;
  
  // Check if source and destination are in the same directory
  vdir_from = helper_extract_dirname(from);
  vdir_to = helper_extract_dirname(to);
  same_dir = (strcmp(vdir_from, vdir_to) == 0);
  free(vdir_from);
  free(vdir_to);
   
  if (same_dir) {
    metadata_t *metadata_from;
    version_t *version_from;
    struct stat st_rfile_from;
    char *metafile_from;
    char *rpath_to;
    int result;
  
    metadata_from = rcs_translate_to_metadata(from, rcs_version_path);
    if (!metadata_from || metadata_from->md_deleted)
      return -ENOENT;
    version_from = rcs_find_version(metadata_from, LATEST, LATEST);

    // Check if source is a regular file or symlink
    // For other types, copying is not implemented yet
    if (lstat(version_from->v_rfile, &st_rfile_from) == -1)
      return -errno;
    if (S_ISREG(st_rfile_from.st_mode) || S_ISLNK(st_rfile_from.st_mode)) {
      if (S_ISREG(st_rfile_from.st_mode)) {
        result = create_new_file(to, st_rfile_from.st_mode, st_rfile_from.st_uid,
          st_rfile_from.st_gid, st_rfile_from.st_rdev);
        if (result == -EEXIST) {
          // renaming to an existing file will overwrite that file
          result = create_new_version_generic(to, SUBVERSION_NO, COPY_NO,
          st_rfile_from.st_mode, st_rfile_from.st_uid, st_rfile_from.st_gid);
        }
        if (result)
          return result;
        rpath_to = rcs_translate_path(to, rcs_version_path); 
        result = create_copy_file(version_from->v_rfile, rpath_to);
        free(rpath_to);
        if (result)
          return result;
      }
      else /* if (S_ISLNK(st_rfile_from.st_mode)) */ {
        char lnk[1024];
        int lnk_size;
        if ((lnk_size = readlink(version_from->v_rfile, lnk, 1023)) == -1)
          return -2;
        lnk[lnk_size] = '\0';
        result = create_new_symlink(lnk, to, st_rfile_from.st_uid, st_rfile_from.st_uid);
        if (result == -EEXIST) {
          // renaming to an existing file will overwrite that file
          result = create_new_version_generic(to, SUBVERSION_NO, COPY_NO,
          st_rfile_from.st_mode, st_rfile_from.st_uid, st_rfile_from.st_gid);
          if (result)
            return result;
          rpath_to = rcs_translate_path(to, rcs_version_path); 
          result = create_copy_file(version_from->v_rfile, rpath_to);
        }
        if (result)
          return result;
      }
      
      // Delete old file
      metadata_from->md_deleted = 1;
      metafile_from = helper_build_meta_name(metadata_from->md_vfile, METADATA_PREFIX);
      if (write_metadata_file(metafile_from, metadata_from) == -1) {
        free(metafile_from);
        return -errno;
      }
      free(metafile_from);
      return 0;
    }
  }
  
  (void)from;
  (void)to;
  return -EXDEV;
}
Ejemplo n.º 14
0
static int callback_mknod(const char *path, mode_t mode, dev_t rdev)
{
  return create_new_file(path, mode, fuse_get_context()->uid,
			 fuse_get_context()->gid, rdev);
}
Ejemplo n.º 15
0
/**
 * Create a new file or dir  named filename
 * If a dir or file of the same name exists, return 0
 * If there is no space on the file system to create file, return 0
 * else create the new file or dir, add entry in current dir table
 * and return index of first block of file or dir
 * @param filename is name of file to create
 * @param file_type is either MFS_BLOCK_TYPE_DIR (for directory) or MFS_BLOCK_TYPE_FILE (for a regular file)
 * @return 1 for success and 0 for failure
 */
static int create_file(const char *filename, int file_type) {
  int new_dir_block;
  int new_dir_index;
  int new_entry_index;
  int new_block;
  int first_dir_block;
  int reuse_block = -1;
  int reuse_index = -1;
  int reusing = 0;

  if (get_dir_ent(filename, &new_dir_block, &new_dir_index, &reuse_block, &reuse_index)) {
    //xil_printf("Case 10\r\n");
    /* file already exists */
    return 0 ; /* cannot create file if it already exists */
  }
  else if (new_dir_block == -1 || new_dir_index == -1) {
    /* file does not exist but path prefix does not exist either */
    //xil_printf("Case 11\r\n");
    return 0; /* cannot create file because its parent dir does not exist */
  }
  else { /* create the file */
    /* first check if there is a reusable entry */
	if ((reuse_block != -1) && (reuse_index != -1)) {
	  /* found an entry to reuse */
	  new_dir_index = reuse_index;
	  new_dir_block = reuse_block;
	  reusing = 1;
	}
    else {
	  /* check if the current dir block is full and
         allocate a new dir block if needed */

      if (new_dir_index == MFS_MAX_LOCAL_ENT) {
        /* create a new dir block linked from this one */
        if (get_next_free_block(&new_block)) { /* found a free block */
	      mfs_file_system[new_block].prev_block = new_dir_block;
	      mfs_file_system[new_block].next_block = 0;
	      mfs_file_system[new_block].block_type = MFS_BLOCK_TYPE_DIR;
	      mfs_file_system[new_block].u.dir_data.num_entries = 0;
	      mfs_file_system[new_block].u.dir_data.num_deleted = 0;
	      mfs_file_system[new_dir_block].next_block = new_block;
	      new_dir_block = new_block;
	      new_dir_index = 0;
        }
        else { /* no space for new block  - return failure */
	      //xil_printf("Case 12\r\n");
	      return 0;
        }
      }
    }
    /* at this point new_dir_index and new_dir_block both point to
       the first free entry */
    first_dir_block = get_first_dir_block(new_dir_block);
    if (!create_new_file(file_type, &new_entry_index, first_dir_block)) { /* cannot create new file */
      //xil_printf("Case 13\r\n");
      return 0; /* failure */
    };

	if (reusing != 1) {
      /* update number of entries in current block */
      mfs_file_system[new_dir_block].u.dir_data.num_entries += 1;
      /* update number of entries in directory if it is different than current block */

      if (new_dir_block != first_dir_block)
        mfs_file_system[first_dir_block].u.dir_data.num_entries += 1;
	}
    mfs_file_system[new_dir_block].u.dir_data.dir_ent[new_dir_index].index = new_entry_index;
    set_filename(mfs_file_system[new_dir_block].u.dir_data.dir_ent[new_dir_index].name, get_basename(filename));
    mfs_file_system[new_dir_block].u.dir_data.dir_ent[new_dir_index].deleted = 'n';
    //xil_printf("Case 14\r\n");
    return new_entry_index;
  }
}