download tmpnam patch

"Navin P" <[email protected]>
Newsgroups gmane.comp.web.links
Message-ID <[email protected]>
Only in elinks: patchfile
Only in elinks/src: linux-2.6.11.8.tar.bz2
diff -pr elinks_old/src/session/download.c elinks/src/session/download.c
*** elinks_old/src/session/download.c	2006-12-23 18:10:38.000000000 -0800
--- elinks/src/session/download.c	2006-12-23 22:36:56.000000000 -0800
*************** create_download_file_do(struct terminal
*** 664,669 ****
--- 664,675 ----
  	/* O_APPEND means repositioning at the end of file before each write(),
  	 * thus ignoring seek()s and that can hide mysterious bugs. IMHO.
  	 * --pasky */
+
+ 	/* Can we unlink(file) here if case of if(!resume) unlink(file);
+ 	 * Btw where is the seek or O_APPEND being done. In that case
+ 	 * can't be remove unlink even in the resume case.
+ 	 * --navin */
+
  	h = open(file, O_CREAT | O_WRONLY | (resume ? 0 : O_TRUNC)
  			| (sf && !resume ? O_EXCL : 0),
  		 sf ? 0600 : 0666);
*************** get_temp_name(struct uri *uri)
*** 748,768 ****
  {
  	struct string name;
  	unsigned char *extension;
! 	/* FIXME
! 	 * We use tempnam() here, which is unsafe (race condition), for now.
! 	 * This should be changed at some time, but it needs an in-depth work
! 	 * of whole download code. --Zas */
! 	unsigned char *nm = tempnam(NULL, ELINKS_TEMPNAME_PREFIX);

! 	if (!nm) return NULL;

  	if (!init_string(&name)) {
! 		free(nm);
  		return NULL;
  	}

  	add_to_string(&name, nm);
! 	free(nm);

  	extension = get_extension_from_uri(uri);
  	if (extension) {
--- 754,783 ----
  {
  	struct string name;
  	unsigned char *extension;
! 	unsigned char *nm,*tm;
! 	int fd=-1;

! 	nm=get_tempdir_filename(ELINKS_TEMPNAME_PREFIX"XXXXXX");
!
! 	if(nm==NULL) {
! 			return  NULL;
! 	}
! 	
! 	if((fd=mkstemp(nm)) < 0) {
! 		mem_free(nm);
! 		return NULL;
! 	}

  	if (!init_string(&name)) {
! 		mem_free(nm);
! 		close(fd);
  		return NULL;
  	}

  	add_to_string(&name, nm);
!
! 	unlink(nm);
! 	mem_free(nm);

  	extension = get_extension_from_uri(uri);
  	if (extension) {
*************** get_temp_name(struct uri *uri)
*** 770,775 ****
--- 785,792 ----
  		mem_free(extension);
  	}

+ 	close(fd);
+
  	return name.source;
  }

*************** continue_download(void *data, unsigned c
*** 925,931 ****
  	}

  	if (type_query->external_handler) {
! 		/* FIXME: get_temp_name() calls tempnam(). --Zas */
  		file = get_temp_name(type_query->uri);
  		if (!file) {
  			mem_free(codw_hop);
--- 942,950 ----
  	}

  	if (type_query->external_handler) {
! 		/* FIXME: Well this doesn't call tmpnam but calls mkstemp .
! 		 * We cannot be sure that a user hasn't created symlink
! 		 * at this time. So we have to be careful. --navin */
  		file = get_temp_name(type_query->uri);
  		if (!file) {
  			mem_free(codw_hop);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.