¡¡¡¡1¡¢±£Ö¤ÁÙʱÎļþ¼äµÄÎļþÃû²»»¥Öú³åÍ»¡£
¡¡¡¡2¡¢±£Ö¤ÁÙʱÎļþÖÐÄÚÈݲ»±»ÆäËûÓû§»òÕߺڿÍ͵¿´¡¢É¾³ýºÍÐ޸ġ£
¡¡¡¡ËùÒÔÔÚlinuxÏÂÓÐרÃÅ´¦ÀíÁÙʱÎļþµÄº¯Êý
¡¡¡¡mkstempº¯Êý
¡¡¡¡mkstempº¯Êý½«ÔÚϵͳÖÐÒÔ¶ÀÒ»ÎÞ¶þµÄÎļþÃû´´½¨Ò»¸öÎļþ²¢´ò¿ª£¬¶øÇÒÖ»Óе±Ç°Óû§²ÅÓзÃÎÊÕâ¸öÁÙʱÎļþµÄȨÏÞ£¬µ±Ç°Óû§¶ÔÕâ¸öÁÙʱÎļþ¿ÉÒÔ´ò¿ª²¢½øÐжÁ¡¢Ð´²Ù×÷¡£mkstempº¯ÊýÖ»ÓÐÒ»¸ö²ÎÊý£¬Õâ¸ö²ÎÊýÊǸöÒÔ¡°XXXXXX¡±½áβµÄ·Ç¿Õ×Ö·û´®¡£mkstempº¯Êý»áÓÃËæ»ú²úÉúµÄ×Ö·û´®Ìæ»»¡°XXXXXX¡±£¬±£Ö¤ÁËÎļþÃûµÄΨһÐÔ¡£º¯Êý·µ»ØÒ»¸öÎļþÃèÊö·û£¬Èç¹ûÖ´ÐÐʧ°Ü·µ»Ø-1¡£ÔÚglibc 2.0.6 ÒÔ¼°¸üÔçµÄglibc¿âÖÐÕâ¸öÎļþµÄ·ÃÎÊȨÏÞÊÇ0666 ,glibc 2.0.7 ÒÔºóµÄ¿âÕâ¸öÎļþµÄ·ÃÎÊȨÏÞÊÇ0600¡£
¡¡¡¡µ±ÁÙʱÎļþÍê³ÉËýµÄʹÃüÈç¹û²»°ÑËüÇå³ý¸É¾»°Ñ»òÕß³ÌÐòÓÉÓÚÒâÍâÔÚÁÙʱÎļþ±»Çå³ýÇ°¾ÍÒѾÍ˳ö£¬ÁÙʱÎļþËùÔÚµÄĿ¼»áÈûÂúÀ¬»ø¡£ÓÉÓÚmkstempº¯Êý´´½¨µÄÁÙʱÎļþ²»ÄÜ×Ô¶¯É¾³ý£¨Çë²Î¿¼ÏÂÎÄÖеÄtmpfileº¯Êý£©¡£Ö´ÐÐÍêmkstempº¯ÊýºóÒªµ÷ÓÃunlinkº¯Êý£¬unlinkº¯Êýɾ³ýÎļþµÄĿ¼Èë¿Ú£¬ËùÒÔÁÙʱÎļþ»¹¿ÉÒÔͨ¹ýÎļþÃèÊö·û½øÐзÃÎÊ£¬Ö±µ½×îºóÒ»¸ö´ò¿ªµÄ½ø³Ì¹Ø±ÕÎļþ²Ù×÷·û£¬»òÕß³ÌÐòÍ˳öºóÁÙʱÎļþ±»×Ô¶¯³¹µ×µØɾ³ý¡£
¡¡¡¡Àý³Ì£º
¡¡¡¡Ö±½ÓʹÓÃadvanced linux programmingµÄÀý³Ì£¬Ö»°Ñ×¢ÊÍ·ÒëÒ»ÏÂ
ÒýÓÃ:#include
#include
/* A handle for a temporary file created with write_temp_file. In
this implementation, it¡¯s just a file descriptor. */
/*write_temp_fileÊǸö²Ù×÷ÁÙʱÎļþµÄ¾ä±ú£¬±¾ÀýÖÐÖ»ÊǸöÎļþÃèÊö·û*/
typedef int temp_file_handle;
/* Writes LENGTH bytes from BUFFER into a temporary file. The
temporary file is immediately unlinked. Returns a handle to the
temporary file. */
/*ÔÚÕ⺯Êý´ÓBUFFERÖÐÏòÁÙʱÎļþдÈëLENGTH×Ö½ÚÊý¾Ý¡£ÁÙʱÎļþÔÚ¸ÕÒ»´´½¨¾Í±»É¾³ýµô¡£º¯Êý»á·µ»ØÁÙʱÎļþµÄ¾ä±ú¡£*/
temp_file_handle write_temp_file (char* buffer, size_t length)
{
/* Create the filename and file. The XXXXXX will be replaced with
characters that make the filename unique. */
/*н¨ÎļþÃûºÍÎļþ£¬ÎļþÃûÖеÄXXXXXX½«±»Ëæ»ú×Ö·û´®´úÌ棬ÒÔ±£Ö¤ÎļþÃûÔÚϵͳÖеÄΨһÐÔ*/
char temp_filename[] = ¡°/tmp/temp_file.XXXXXX¡±;
int fd = mkstemp (temp_filename);
/* Unlink the file immediately, so that it will be removed when the
file descriptor is closed. */
/*ÎļþÁ¢¿Ì±»unlink£¬ÕâÑùÖ»ÒªÎļþÃèÊö·ûÒ»¹Ø±ÕÎļþ¾Í»á±»×Ô¶¯É¾³ý*/
unlink (temp_filename);
/* Write the number of bytes to the file first. */
/*Ê×ÏÈдÈë¼´½«Ð´ÈëÊý¾ÝµÄ³¤¶È*/
write (fd, &length, sizeof (length));
/* Now write the data itself. */
/*дÈëÊý¾Ý±¾Éí*/
write (fd, buffer, length);
/* Use the file descriptor as the handle for the temporary file. */
/*º¯Êý·µ»ØÎļþÃèÊö·û£¬×÷ΪÁÙʱÎļþµÄ¾ä±ú*/
return fd;
}
/* Reads the contents of a temporary file TEMP_FILE created with
write_temp_file. The return value is a newly allocated buffer of
those contents, which the caller must deallocate with free.
*LENGTH is set to the size of the contents, in bytes. The
temporary file is removed. */
/*´Ó±»write_temp_file´´½¨µÄÁÙʱÎļþÖжÁÈ¡Êý¾Ý¡£·µ»ØÖµÊǺ¬ÓÐÎļþÄÚÈݵÄÐÂÉêÇëµ½µÄÄÚ´æ¿é£¬Õâ¿éÄÚ´æÓ¦¸ÃÓÖµ÷ÓÃread_temp_fileÕßÊÍ·Å¡£
*lengthÊÇÁÙʱÎļþÕýÎÄÄÚÈݵij¤¶È¡£Ö´ÐÐÍêread_temp_fileº¯ÊýºóÁÙʱÎļþ±»³¹µ×ɾ³ý*/
char* read_temp_file (temp_file_handle temp_file, size_t* length)
{
char* buffer;
/* The TEMP_FILE handle is a file descriptor to the temporary file. */
/*fdÊÇ·ÃÎÊÁÙʱÎļþµÄÎļþÃèÊö·û*/
int fd = temp_file;
/* Rewind to the beginning of the file. */
/*°ÑÎļþÖ¸ÕëÖ¸ÏòÎļþ¿ªÍ·*/
lseek (fd, 0, SEEK_SET);
/* Read the size of the data in the temporary file. */
/*»ñµÃÁÙʱÎļþÕýÎij¤¶È*/
read (fd, length, sizeof (*length));
/* Allocate a buffer and read the data. */
/*·ÖÅäÄÚ´æ¿é£¬¶ÁÈ¡Êý¾Ý*/
buffer = (char*) malloc (*length);
read (fd, buffer, *length);
/* Close the file descriptor, which will cause the temporary file to
go away. */
/*¹Ø±ÕÎļþÃèÊö·û£¬ÁÙʱÎļþ±»³¹µ×ɾ³ý*/
close (fd);
return buffer;
}
¡¡¡¡tmpfileº¯Êý
¡¡¡¡Èç¹ûÄúʹÓÃC library I/Oº¯Êý£¬²¢ÇÒ²¢Ã»ÓÐÁíÒ»¸ö³ÌÐòʹÓÃÕâ¸öÁÙʱÎļþ£¨±ÊÕß×¢£º°´ÎÒµÄÀí½âÊÇÔÚͬһ½ø³Ì»ò¾ßÓи¸×Ó¹ØϵµÄ½ø³Ì×éÖУ©£¬Óиö¸ü¼ò½àµÄº¯Êý----tmpfile¡£tmpfileº¯Êý´´½¨²¢´ò¿ªÒ»¸öÁÙʱÎļþ£¬²¢ÇÒ×Ô¶¯Ö´ÐÐÁËunlinkÁËÕâ¸öÁÙʱÎļþ¡£tmpfileº¯Êý·µ»ØÒ»¸öÎļþÃèÊö·û£¬Èç¹ûÖ´ÐÐʧ°Ü·µ»ØNULL¡£µ±³ÌÐòÖ´ÐÐÁËfclose»òÕßÍ˳öʱ£¬×ÊÔ´±»ÊÍ·Å¡£
¡¡¡¡linuxϵͳÖл¹Ìṩmktemp¡¢ tmpnam¡¢ ºÍtempnamµÈº¯Êý£¬µ«ÊÇÓÉÓÚ½¡×³ÐԺͰ²È«·½ÃæÀíÓɲ»½¨ÒéʹÓÃËûÃÇ¡£