void
internal_function
__libdwfl_module_free (Dwfl_Module *mod)
{
  if (mod->lazy_cu_root != NULL)
    tdestroy (mod->lazy_cu_root, nofree);

  if (mod->aranges != NULL)
    free (mod->aranges);

  if (mod->cu != NULL)
    {
      for (size_t i = 0; i < mod->ncu; ++i)
	free_cu (mod->cu[i]);
      free (mod->cu);
    }

  if (mod->dw != NULL)
    INTUSE(dwarf_end) (mod->dw);

  if (mod->ebl != NULL)
    ebl_closebackend (mod->ebl);

  if (mod->debug.elf != mod->main.elf)
    free_file (&mod->debug);
  free_file (&mod->main);

  if (mod->build_id_bits != NULL)
    free (mod->build_id_bits);

  free (mod->name);
  free (mod);
}
Пример #2
0
/* Finds and reports all duplicate clusters in each bucket of collected files.
 */
static void process_clusters(void)
{
    size_t i, j, first, second, index = 1;
    FileList duplicates;

    init_file_list(&duplicates);

    for (i = 0;  i < BUCKET_COUNT;  i++)
    {
        File* files = buckets[i].files;

        for (first = 0;  first < buckets[i].allocated;  first++)
        {
            if (files[first].status == INVALID ||
                files[first].status == DUPLICATE)
            {
                continue;
            }

            for (second = first + 1;  second < buckets[i].allocated;  second++)
            {
                if (files[second].status == INVALID ||
                    files[second].status == DUPLICATE)
                {
                    continue;
                }

                if (compare_files(&files[first], &files[second]) == 0)
                {
                    if (duplicates.allocated == 0)
                    {
                        *alloc_file(&duplicates) = files[first];
                        files[first].status = DUPLICATE;
                    }

                    *alloc_file(&duplicates) = files[second];
                    files[second].status = DUPLICATE;
                }
                else
                {
                    if (files[first].status == INVALID)
                        break;
                }
            }

            if (duplicates.allocated > 0)
            {
                report_cluster(&duplicates, index);
                empty_file_list(&duplicates);

                index++;
            }
        }

        for (j = 0;  j < buckets[i].allocated;  j++)
            free_file(&files[j]);
    }

    free_file_list(&duplicates);
}
Пример #3
0
int free_filereader(filereader_t * frd)
{
   int err;
   int err2;

   frd->ioerror    = 0;
   frd->unreadsize = 0;
   frd->nextindex  = 0;
   frd->nrfreebuffer = 0;
   frd->fileoffset = 0;
   frd->filesize   = 0;

   err = free_file(&frd->file);

   err2 = free_vmpage(cast_vmpage(&frd->page[0],));
   if (err2) err = err2;

   err2 = free_vmpage(cast_vmpage(&frd->page[1],));
   if (err2) err = err2;

   if (err) goto ONERR;

   return 0;
ONERR:
   TRACEEXITFREE_ERRLOG(err);
   return err;
}
float* get_font_data(const char *font_file, float *font_height){
    float *data = 0;
    stbtt_bakedchar *baked;
    File_Data file = get_file(font_file);
    if (!file.data) exit(1);

    if (file.data){
        int size = sizeof(*baked)*256;
        baked = (stbtt_bakedchar*)malloc(size);
        memset_4tech(baked, 0, sizeof(*baked)*256);

        stbtt_fontinfo font;
        if (stbtt_InitFont(&font, (unsigned char*)file.data, 0)){
            float scale;
            int a,d,g;
            
            scale = stbtt_ScaleForPixelHeight(&font, 17.f);
            stbtt_GetFontVMetrics(&font, &a, &d, &g);
            *font_height = scale*(a - d + g);
            
            int w, h;
            w = 10*256;
            h = 25;
            unsigned char *pixels = (unsigned char*)malloc(w * h);
            stbtt_BakeFontBitmap((unsigned char*)file.data, 0, 17.f, pixels, w, h, 0, 128, baked);
            free(pixels);
            free_file(file);

            data = (float*)malloc(sizeof(float)*256);
            memset_4tech(data, 0, sizeof(float)*256);

            stbtt_bakedchar *baked_ptr = baked;
            for (int i = 0; i < 128; ++i, ++baked_ptr){
                data[i] = baked_ptr->xadvance;
            }
        }
        free(baked);
    }
    else{
        printf("error: cannot continue without font\n");
    }
    
    return data;
}
Пример #5
0
uint8_t run_program(const char *__n){
    uint8_t rc;
    if((rc = load_file(__n, &file_handler)))
        return rc;
        
    if( file_handler.entry.file_size > MAX_BIN_SIZE )
        return EXEC_SIZE_TOO_BIG;
    
    puts_attrib("=================EXECUTOR=================", color_entry(COLOR_GREEN, COLOR_BLACK)); eol();
    load_bin(0, file_handler.entry.file_size);
    return_code = (int)(*raw_bin)();
    eol();
    puts_attrib("=================EXECUTOR=================", color_entry(COLOR_RED, COLOR_BLACK));
    eol(); 
    puts_attrib("RC:", color_entry(COLOR_MAGENTA, COLOR_BLACK)); print_int(return_code, 10, 0);eol();
    puts_attrib("=================EXECUTOR=================", color_entry(COLOR_GREEN, COLOR_BLACK));
    free_file();
    return EXEC_OP_OK;
}
Пример #6
0
void put_file(struct file *file)
{
	if (file->ref <= 0) {
		fs_error("File has no reference\n");
		panic("Unrecoverable FS error\n");
	}
	file->ref -= 1;
	if (file->ref == 0) {
		if (file->f_ops && file->f_ops->close != NULL)
			file->f_ops->close(file); /* don't operate on inode here */

		if (file->inode) {
			put_inode(file->inode);
			file->inode = NULL;
		}

		free_file(file);
	}
}
Пример #7
0
int remove_sub_file( pfile parent, char *name )
{
    pfile *list = NULL;
    int index = 0;
    pfile t = NULL;
    
    if ( parent == NULL || name == NULL ) {
        return 0;
    }
    
    if ( parent->data == NULL ) {
        return 0;
    }
    
    list = (pfile*)parent->data;
    
    while ( index < parent->length ) {
        t = list[index];
        
        if ( t == NULL ) {
            index++;
            continue;
        }
        
        if ( strcmp( t->name, name ) == 0 ) {
            
            if ( t->type == DIR ) {
                printf("[ERROR] Cannot delete a directory\n");
                return 0;
            }
            
            free_file( t );
            list[index] = NULL;
            return 1;
        }
        
        index ++;
    }
    
    return 0;
}
Пример #8
0
int main(int argc,char** argv) {

    if (argc != 3) {
        fprintf(stderr,"usage: RE216_CLIENT hostname port\n");
        return 1;
    }
    
	// -----------------------------------------------------------------
	// ------------------------ Variables ------------------------------
	
	// Buffer
	char *input = NULL;		// Taille d'entrée dynamique
	char output[TAILLE_MSG];// Taille de réception fixée
    
	// Liste chaînée pour l'envoi de fichiers
	struct file fichiers;
		memset(&fichiers, 0, sizeof(struct file));

	// Récupération de la structure sockaddr_in6 pour l'adresse du serveur
	struct sockaddr_in6* server_add = get_addr_info(atoi(argv[2]), argv[1]);

	// Création de la socket
	int sckt = do_socket();  

	// Connexion de la socket à l'adresse server_add
	int conn = do_connect(sckt, *server_add);

	// Initialisation des tableaux pour utliser select ------------------
    
    fd_set fd_set_read; // Ici seront stockés les descripteurs de fichier
    
    int i, select_ret_val;
    
    // Socket du serveur quand elle existe
    int socket_fichier = -1;
    
    // Eventuellement : timeout du select
	//~ struct timeval tv;
	//~ tv.tv_sec = 5;
	//~ tv.tv_usec = 0;
	
	init_reg();
	// -----------------------------------------------------------------
	// -----------------------------------------------------------------
	
	start_line();

	// Boucle jusqu'à recevoir le "/quit" final
	do {
		
		// ------------------------ R.A.Z ------------------------------
		// clean the set before adding file descriptors
		FD_ZERO(&fd_set_read);
		
		// add the fd for server connection
		FD_SET(sckt, &fd_set_read);

		// add the fd for user-input
		FD_SET(fileno(stdin), &fd_set_read);
		// -------------------------------------------------------------
		 
		// we now wait for any file descriptor to be available for reading
        select_ret_val = select(sckt + 1, &fd_set_read, NULL, NULL, NULL);//&tv);
        
        if (select_ret_val == -1) {
			error("Erreur concernant le select ");
		}
        
        //printf("Le retour de la fonction select est : %i", select_ret_val);
		
		for (i = 0; i < (sckt+1) && select_ret_val > 0; i++) {
	
            // Le buffer est nettoyé avec memset directement dans les fonctions
            
            //printf("Bonjour je suis le i n°%i. Retour => %i\n", i, select_ret_val);

            // Si le file descripteur i est dans le set mis en écoute, c'est qu'il y a une activité
            if (FD_ISSET(i, &fd_set_read)) {
				
				// printf("Descripteur trouvé : %i\n", i);

                if (i == fileno(stdin)) // C'est une entrée utilisateur
					client_write(&input, sckt, &fichiers);
				
				else // Les données viennent du serveur
					if (!client_read(sckt, output, &fichiers, &socket_fichier))
						break;

                // Select_ret_val est le nombre de descripteurs où il y 
                // a eu une activité, on diminue donc sa valeur au fur
                // et à mesure.
                select_ret_val--;

            }
        }

	} while(notquit(input) && notquit(output));

	//printf("Extinction.\n");

	free(input);
	
	free_file(&fichiers);

	free_reg();

	// Fermeture de la socket
    close_socket(sckt);
    
	printf("Fin du tchat\n");
	
    return 0;
}
Пример #9
0
static int test_rwpartial(directory_t* tmpdir)
{
   iothread_t  iothr = iothread_FREE;
   file_t      file = file_FREE;
   memblock_t  writebuf = memblock_FREE;
   memblock_t  readbuf  = memblock_FREE;
   iotask_t    iotask_buffer;
   iotask_t*   iotask = &iotask_buffer;
   eventcount_t counter = eventcount_INIT;

   // prepare0
   TEST(0 == init_iothread(&iothr));
   // alloc buffer
   TEST(0 == ALLOC_PAGECACHE(pagesize_1MB, &readbuf));
   TEST(0 == ALLOC_PAGECACHE(pagesize_1MB, &writebuf));
   for (size_t val = 0; val < writebuf.size/sizeof(uint32_t); ++val) {
      ((uint32_t*)writebuf.addr)[val] = (uint32_t) val;
   }
   memset(readbuf.addr, 0, readbuf.size);

   for (int ispos = 0; ispos <= 1; ++ispos) {

      // TEST insertiotask_iothread: (writep,write) && syscall writes less than writebuf.size
      TEST(0 == initcreate_file(&file, "testpartial", tmpdir))
      if (ispos) {
         initwritep_iotask(iotask, io_file(file), writebuf.size, writebuf.addr, 0, &counter);
      } else {
         initwrite_iotask(iotask, io_file(file), writebuf.size, writebuf.addr, &counter);
      }
      // test
      s_iothread_errtimer_count = 0;
      init_testerrortimer(&s_iothread_errtimer, 2/*trigger partial io*/, 1);
      insertiotask_iothread(&iothr, 1, &iotask);
      // wait for writer ready
      wait_eventcount(&counter,0);
      // check iotask
      TEST(iotask->iolist_next == 0);
      TEST(iotask->bytesrw    == writebuf.size);
      TEST(iotask->state      == iostate_OK);
      TEST(iotask->op         == ioop_WRITE);
      TEST(iotask->ioc        == io_file(file));
      TEST(iotask->offset     == (ispos ? 0 : -1));
      TEST(iotask->bufaddr    == writebuf.addr);
      TEST(iotask->bufsize    == writebuf.size);
      TEST(iotask->readycount == &counter);
      // check 32 partial writes (1+32)
      TEST(32 == s_iothread_errtimer_count);
      // reset
      TEST(0 == free_file(&file));
      free_testerrortimer(&s_iothread_errtimer);

      // TEST insertiotask_iothread: (readp, read) && syscall reads less than writebuf.size
      TEST(0 == init_file(&file, "testpartial", accessmode_READ, tmpdir));
      if (ispos) {
         initreadp_iotask(iotask, io_file(file), readbuf.size, readbuf.addr, 0, &counter);
      } else {
         initread_iotask(iotask, io_file(file), readbuf.size, readbuf.addr, &counter);
      }
      // test
      s_iothread_errtimer_count = 0;
      init_testerrortimer(&s_iothread_errtimer, 2/*trigger partial io*/, 1);
      insertiotask_iothread(&iothr, 1, &iotask);
      // wait for reader ready
      wait_eventcount(&counter,0);
      // check iotask.ioop[0]
      TEST(iotask->iolist_next == 0);
      TEST(iotask->bytesrw    == writebuf.size);
      TEST(iotask->state      == iostate_OK);
      TEST(iotask->op         == ioop_READ);
      TEST(iotask->ioc        == io_file(file));
      TEST(iotask->offset     == (ispos ? 0 : -1));
      TEST(iotask->bufaddr    == readbuf.addr);
      TEST(iotask->bufsize    == readbuf.size);
      TEST(iotask->readycount == &counter);
      // check content of readbuf
      for (size_t val = 0; val < readbuf.size/sizeof(uint32_t); ++val) {
         TEST(val == ((uint32_t*)readbuf.addr)[val]);
      }
      // check 32 partial writes (1+32)
      TEST(32 == s_iothread_errtimer_count);
      // reset
      memset(readbuf.addr, 0, readbuf.size);
      TEST(0 == free_file(&file));
      TEST(0 == removefile_directory(tmpdir, "testpartial"));
      free_testerrortimer(&s_iothread_errtimer);
   }

   // reset
   TEST(0 == free_iothread(&iothr));
   TEST(0 == free_eventcount(&counter));
   TEST(0 == free_iochannel(&file));
   TEST(0 == RELEASE_PAGECACHE(&writebuf));
   TEST(0 == RELEASE_PAGECACHE(&readbuf));

   return 0;
ONERR:
   free_iothread(&iothr);
   free_eventcount(&counter);
   free_iochannel(&file);
   RELEASE_PAGECACHE(&writebuf);
   RELEASE_PAGECACHE(&readbuf);
   removefile_directory(tmpdir, "testpartial");
   return EINVAL;
}
Пример #10
0
__export void _close_file(struct file *file)
{
    if (file->fs)
	file->fs->fs_ops->close_file(file);
    free_file(file);
}
Пример #11
0
/* if we have a backup_dir, then we get here from make_backup().
   We will move the file to be deleted into a parallel directory tree */
static int keep_backup(char *fname)
{

	static int initialised;

	char keep_name [MAXPATHLEN];
	STRUCT_STAT st;
	struct file_struct *file;

	int kept=0;
	int ret_code;

	if (!initialised) {
		if (backup_dir[strlen(backup_dir) - 1] == '/')
			backup_dir[strlen(backup_dir) - 1] = 0;
		if (verbose > 0)
			rprintf (FINFO, "backup_dir is %s\n", backup_dir);
		initialised = 1;
	}

	/* return if no file to keep */
#if SUPPORT_LINKS
	if (do_lstat (fname, &st)) return 1;
#else
	if (do_stat (fname, &st)) return 1;
#endif

	file = make_file(-1, fname, NULL, 1);

	/* the file could have disappeared */
	if (!file) return 1;

        /* make a complete pathname for backup file */
        if (strlen(backup_dir) + strlen(fname) + 
		(suffix_specified ? strlen(backup_suffix) : 0) > (MAXPATHLEN - 1)) {
                rprintf (FERROR, "keep_backup filename too long\n");
                return 0;
        }

	if (suffix_specified) {
        	snprintf(keep_name, sizeof (keep_name), "%s/%s%s", backup_dir, fname, backup_suffix);
		} else {
        	snprintf(keep_name, sizeof (keep_name), "%s/%s", backup_dir, fname);
		}


#ifdef HAVE_MKNOD
	/* Check to see if this is a device file, or link */
        if(IS_DEVICE(file->mode)) {
                if(am_root && preserve_devices) {
                        make_bak_dir(fname,backup_dir);
                        if(do_mknod(keep_name,file->mode,file->rdev)!=0) {
                                rprintf(FERROR,"mknod %s : %s\n",keep_name,strerror(errno));
                        } else {
                                if(verbose>2)
                                        rprintf(FINFO,"make_backup : DEVICE %s successful.\n",fname);
                        };
                };
		kept=1;
                do_unlink(fname);
        };
#endif

	if(!kept && S_ISDIR(file->mode)) {
		/* make an empty directory */
                make_bak_dir(fname,backup_dir);
                do_mkdir(keep_name,file->mode);
                ret_code=do_rmdir(fname);
                if(verbose>2)
                        rprintf(FINFO,"make_backup : RMDIR %s returns %i\n",fname,ret_code);
		kept=1;
        };

#if SUPPORT_LINKS
        if(!kept && preserve_links && S_ISLNK(file->mode)) {
                extern int safe_symlinks;
                if (safe_symlinks && unsafe_symlink(file->link, keep_name)) {
                        if (verbose) {
                                rprintf(FINFO,"ignoring unsafe symlink %s -> %s\n",
                                        keep_name,file->link);
                        }
			kept=1;
                }
                make_bak_dir(fname,backup_dir);
                if(do_symlink(file->link,keep_name) != 0) {
                        rprintf(FERROR,"link %s -> %s : %s\n",keep_name,file->link,strerror(errno));
                };
                do_unlink(fname);
		kept=1;
        };
#endif
        if(!kept && preserve_hard_links && check_hard_link(file)) {
                if(verbose > 1) rprintf(FINFO,"%s is a hard link\n",f_name(file));
        };

        if(!kept && !S_ISREG(file->mode)) {
                rprintf(FINFO,"make_bak: skipping non-regular file %s\n",fname);
        }

	/* move to keep tree if a file */
	if(!kept) {
		if (!robust_move (fname, keep_name))
			rprintf(FERROR, "keep_backup failed %s -> %s : %s\n",
				fname, keep_name, strerror(errno));
	};
	set_perms (keep_name, file, NULL, 0);
	free_file (file);
	free (file);

	if (verbose > 1)
		rprintf (FINFO, "keep_backup %s -> %s\n", fname, keep_name);
	return 1;
} /* keep_backup */
Пример #12
0
int main(int argc,char **argv) {  
	int z;  
	struct sockaddr_in adr_inet;
	struct sockaddr_in adr_clnt; 
	int len_inet; 
	int s;  
	char dgram[512];             // Recv buffer  
	char dtfmt[512];			  // Date/Time Result  
	time_t td;                   // Current Time and Date  
	struct tm tm;                // Date time values  

	char* response;	  									// Response line to print out: timestamp; client_address:port responseLine; filepath
	char* requestLine;	  									// Request line formatted from datagram packet
	char* port_asStr = (char*)malloc(sizeof(char*)*256); 	// Port number as a string

	int port;					  // User specified port number
	char* root;				  // User specified root directory
	 
 /* 
  * Set the port and root directory fromm the command line
  */  
     if ( argc == 3 ) {
		port = atoi(argv[1]);
		root = argv[2];
		mount_onroot(root);	// mount server on root dir
     }  
     else {  
        printf("Please specify a port number followed by a root directory\n"); 
		exit(1);
     }  
  
 /* 
  * Create a UDP socket to use: 
  */  
     s = socket(AF_INET,SOCK_DGRAM,0);  
     if ( s == -1 ) {  
        displayError("socket()");  
     }  
 /* 
  * Create a socket address, for use 
  * with bind(2): 
  */  
     memset(&adr_inet,0,sizeof adr_inet);  
     adr_inet.sin_family = AF_INET;  
     adr_inet.sin_port = htons(port);  
     adr_inet.sin_addr.s_addr = htonl(INADDR_ANY);
	
     if ( adr_inet.sin_addr.s_addr == INADDR_NONE ) {  
        displayError("bad address.");  
     }  
     len_inet = sizeof adr_inet;  
  
 /* 
  * Bind an address to our socket, so that 
  * client programs can contact this 
  * server: 
  */  
	int optval = 1;
	setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
  
     z = bind(s, (struct sockaddr *)&adr_inet, len_inet);  
     if ( z == -1 ) {  
        displayError("bind()");  
     }  
	 printf("sws is running on UDP port %d and serving %s\n",port,root);
	 
 /* 
  * Now wait for requests: 
  */  
	for (;;) {  
		/*
		* Check if user wants to quit the server
		*/
		int retval = waitForUserQuit(s);
		printf("retval: %d\n",retval);
		if(retval == 1)
		{
			printf("quitting...\n");
			exit(1);
		}
		/* 
		* Block until the program receives a 
		* datagram at the address and port: 
		*/  
		len_inet = sizeof adr_clnt;  
		z = recvfrom(s,            // Socket  
					 dgram,        // Receiving buffer  
					 sizeof dgram, // Max recv buf size  
					 0,            // Flags: no options  
					 (struct sockaddr *)&adr_clnt, // Addr  
					 &len_inet);  // Addr len, in & out  
		if ( z < 0 ) {  
		   displayError("recvfrom(2)");  
		}  
		dgram[z] = '\0'; 

		
		/* 
		* Get the current date and time: 
		*/  
		time(&td);           
		tm = *localtime(&td); 
		/* 
		* Format the timestamp 
		*/  
		char* fmt = "%m %e %H:%M:%S";
        strftime(dtfmt,       
                 sizeof dtfmt,
                 fmt,  
                 &tm); 
		char* timestamp = format_timestamp(dtfmt);
		/*
		* Copy the packet's data for later use
		*/
		requestLine = (char*)malloc(sizeof(char*)*256);
		int i=0;
		while(dgram[i] != '\0')
		{
			if(ispunct(dgram[i]) || isalnum(dgram[i]) || dgram[i] == ' ')
				requestLine = charAppend(requestLine, dgram[i]);
			i++;
		}
		/* 
		* Parse then execute the request and store output
		*/
		char* responseLine;
		if(isBadRequest(requestLine))
			responseLine = responseSet._400;
		else
			responseLine = get_responseline(dgram);
		char* filePath = get_filePath();
		char* fileContents = get_fileContents();
		free_file();
		//printf("responseLine: %s\nfilePath: %s\nfileContents: %s\n",responseLine, filePath, fileContents);
		/*
		* Create response string
		*/
		response = (char*)malloc(sizeof(response)*1000);
		strcpy(response, timestamp);
		response = charAppend(response, ' ');
		//printf("response: %s\n",response);
		
		strcat(response, inet_ntoa(*(struct in_addr *)&adr_clnt.sin_addr.s_addr));
		response = charAppend(response, ':');
		//printf("response: %s\n",response);
		
		sprintf(port_asStr, "%d", ntohs(adr_clnt.sin_port));
		strcat(response, port_asStr);
		response = charAppend(response, ' ');
		//printf("response: %s\n",response);
		
		strcat(response, requestLine);
		strcat(response, "; ");
		//printf("response: %s\n",response);
		
		strcat(response, responseLine);
		strcat(response, "; ");
		//printf("response: %s\n",response);
		
		if(strcmp(responseLine, responseSet._200)==0)
			strcat(response, filePath);
		printf("%s\n",response);
		
		char* to_client = fileContents;
		if(strcmp(responseLine, responseSet._200) != 0)
			to_client = responseLine;
	
		/* 
		* Send the response or file back to the 
		* client program: 
		*/  
        z = sendto(s,
                   to_client, 
                   strlen(to_client), 
                   0, 
                   (struct sockaddr *)&adr_clnt,// addr  
                   len_inet);
		if ( z < 0 ) {  
		  displayError("sendto(2)");  
		}  
    }  
	/*
	*	Free memory
	*/
	free(response);
	free(requestLine);
	free(port_asStr);

	/* 
	* Close the socket and exit: 
	*/  
	 close(s);  
	 return 0;  
}