[29122] trunk/base/src/pextlib1.0

source_changes at macosforge.org source_changes at macosforge.org
Sat Sep 15 05:07:48 PDT 2007


Revision: 29122
          http://trac.macosforge.org/projects/macports/changeset/29122
Author:   afb at macports.org
Date:     2007-09-15 05:07:48 -0700 (Sat, 15 Sep 2007)

Log Message:
-----------
add getsize command to tcl curl

Modified Paths:
--------------
    trunk/base/src/pextlib1.0/curl.c
    trunk/base/src/pextlib1.0/curl.h

Modified: trunk/base/src/pextlib1.0/curl.c
===================================================================
--- trunk/base/src/pextlib1.0/curl.c	2007-09-15 11:56:06 UTC (rev 29121)
+++ trunk/base/src/pextlib1.0/curl.c	2007-09-15 12:07:48 UTC (rev 29122)
@@ -577,6 +577,155 @@
 }
 
 /**
+ * curl getsize subcommand entry point.
+ *
+ * @param interp		current interpreter
+ * @param objc			number of parameters
+ * @param objv			parameters
+ */
+int
+CurlGetSizeCmd(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[])
+{
+	int theResult = TCL_OK;
+	CURL* theHandle = NULL;
+	FILE* theFile = NULL;
+
+	do {
+		char theSizeString[32];
+		const char* theURL;
+		CURLcode theCurlCode;
+		double theFileSize;
+				
+		/*	first (second) parameter is the url */
+		if (objc != 3) {
+			Tcl_WrongNumArgs(interp, 1, objv, "getsize url");
+			theResult = TCL_ERROR;
+			break;
+		}
+
+		/* Retrieve the url */
+		theURL = Tcl_GetString(objv[2]);
+
+		/* Open the file (dev/null) */
+		theFile = fopen( "/dev/null", "a" );
+		if (theFile == NULL) {
+			Tcl_SetResult(interp, strerror(errno), TCL_VOLATILE);
+			theResult = TCL_ERROR;
+		}
+
+		/* Create the CURL handle */
+		theHandle = curl_easy_init();
+		
+		/* Setup the handle */
+		theCurlCode = curl_easy_setopt(theHandle, CURLOPT_URL, theURL);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+		
+		/* -L option */
+		theCurlCode = curl_easy_setopt(theHandle, CURLOPT_FOLLOWLOCATION, 1);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+
+		/* -f option */
+		theCurlCode = curl_easy_setopt(theHandle, CURLOPT_FAILONERROR, 1);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+
+		/* set timeout on connections */
+		theCurlCode = curl_easy_setopt(theHandle, CURLOPT_TIMEOUT, _CURL_CONNECTION_TIMEOUT);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+
+		/* set minimum connection speed */
+		theCurlCode = curl_easy_setopt(theHandle, CURLOPT_LOW_SPEED_LIMIT, _CURL_MINIMUM_XFER_SPEED);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+
+		/* set timeout interval for connections < min xfer speed */
+		theCurlCode = curl_easy_setopt(theHandle, CURLOPT_LOW_SPEED_TIME, _CURL_MINIMUM_XFER_TIMEOUT);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+
+		/* write to the file */
+		theCurlCode = curl_easy_setopt(theHandle, CURLOPT_WRITEDATA, theFile);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+
+		/* skip the header data */
+		theCurlCode = curl_easy_setopt(theHandle, CURLOPT_HEADER, 0);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+		
+		/* skip the body data */
+		theCurlCode = curl_easy_setopt(theHandle, CURLOPT_NOBODY, 1);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+
+		/* we do not want any progress */
+		theCurlCode = curl_easy_setopt(theHandle, CURLOPT_NOPROGRESS, 1);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+		
+		/* actually fetch the resource */
+		theCurlCode = curl_easy_perform(theHandle);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+		
+		/* close the file */
+		(void) fclose( theFile );
+		theFile = NULL;
+
+		theFileSize = 0.0;
+
+		/* get the file size */
+		theCurlCode = curl_easy_getinfo(theHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &theFileSize);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+	
+		/* clean up */
+		curl_easy_cleanup( theHandle );
+		theHandle = NULL;
+
+		(void) snprintf(theSizeString, sizeof(theSizeString),
+			"%.0f", theFileSize);
+		Tcl_SetResult(interp, theSizeString, TCL_VOLATILE);
+    } while (0);
+    
+    if (theHandle != NULL) {
+    	curl_easy_cleanup( theHandle );
+    }
+    if (theFile != NULL) {
+    	fclose( theFile );
+    }
+    
+	return theResult;
+}
+
+/**
  * curl command entry point.
  *
  * @param clientData	custom data (ignored)
@@ -593,11 +742,12 @@
 {
     typedef enum {
     	kCurlFetch,
-    	kCurlIsNewer
+    	kCurlIsNewer,
+    	kCurlGetSize
     } EOption;
     
 	static tableEntryString options[] = {
-		"fetch", "isnewer", NULL
+		"fetch", "isnewer", "getsize", NULL
 	};
 	int theResult = TCL_OK;
     EOption theOptionIndex;
@@ -624,6 +774,10 @@
 			case kCurlIsNewer:
 				theResult = CurlIsNewerCmd(interp, objc, objv);
 				break;
+
+			case kCurlGetSize:
+				theResult = CurlGetSizeCmd(interp, objc, objv);
+				break;
 		}
 	}
 	

Modified: trunk/base/src/pextlib1.0/curl.h
===================================================================
--- trunk/base/src/pextlib1.0/curl.h	2007-09-15 11:56:06 UTC (rev 29121)
+++ trunk/base/src/pextlib1.0/curl.h	2007-09-15 12:07:48 UTC (rev 29122)
@@ -54,6 +54,10 @@
  * curl isnewer url date
  *	Determine if some resource is newer than date. Try to not fetch the resource
  *  if possible. The date is the number of seconds since epoch.
+ *
+ * curl getsize url
+ *	Determine the file size of some resource. Try to not fetch the resource
+ *  if possible. The size returned is the the number of bytes.
  */
 int CurlCmd(ClientData clientData, Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[]);
 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macports-changes/attachments/20070915/dd8a5704/attachment.html


More information about the macports-changes mailing list