Ticket #13579: 13579_secure_tmp.patch

File 13579_secure_tmp.patch, 3.6 KB (added by jdemeyer, 9 years ago)
  • sage/misc/interpreter.py

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1349687927 -7200
    # Node ID a9eaf717e609ed39dc52f30c7a3e9b58bfcc3c0e
    # Parent  91d3aa6f936e5689fe9f33127f3b935971012cdf
    Use tempfile for tmp_dir() and tmp_filename()
    
    diff --git a/sage/misc/interpreter.py b/sage/misc/interpreter.py
    a b  
    380380    """
    381381    import sage.misc.misc
    382382    name = os.path.abspath(name)
    383     tmpfilename = os.path.abspath(sage.misc.misc.tmp_filename(name) + ".py")
     383    tmpfilename = sage.misc.misc.tmp_filename('preparse', ext='.py')
    384384    out = open(tmpfilename,'w')
    385385    preparse_file_named_to_stream(name, out)
    386386    out.close()
  • sage/misc/misc.py

    diff --git a/sage/misc/misc.py b/sage/misc/misc.py
    a b  
    19691969# temporary directory
    19701970#################################################################
    19711971
    1972 def tmp_dir(name='dir'):
     1972import tempfile
     1973
     1974def tmp_dir(name="dir", ext=""):
    19731975    r"""
    19741976    Create and return a temporary directory in
    19751977    ``$HOME/.sage/temp/hostname/pid/``
     1978
     1979    INPUT:
     1980
     1981    - ``name`` -- (default: "dir") A prefix for the directory name.
     1982
     1983    - ``ext`` -- (default: "") A suffix for the directory name.
     1984
     1985    OUTPUT: The absolute path of the temporary directory created.
     1986
     1987    EXAMPLES::
     1988       
     1989        sage: d = tmp_dir('dir_testing_', '.extension')
     1990        sage: d  # random
     1991        '/home/username/sage/temp/hostname/7961/dir_testing_XgRu4p.extension'
     1992        sage: os.chdir(d)
     1993        sage: _ = open('file_inside_d', 'w')
     1994        sage: os.chdir(DOT_SAGE)
     1995        sage: import shutil
     1996        sage: shutil.rmtree(d)
    19761997    """
    1977     name = str(name)
    1978     n = 0
    1979     while True:
    1980         tmp = "%s/%s_%s"%(SAGE_TMP, name, n)
    1981         if not os.path.exists(tmp):
    1982             break
    1983         n += 1
    1984     try:
    1985         os.makedirs(tmp)
    1986     except IOError:
    1987         # Put in local directory instead, e.g., because user doesn't
    1988         # have privileges to write in Sage's tmp directory.  That's OK.
    1989         n = 0
    1990         while True:
    1991             tmp = "/temp/tmp_%s_%s"%(name, n)
    1992             if not os.path.exists(tmp):
    1993                 break
    1994             n += 1
    1995         os.makedirs(tmp)
     1998    tmp = tempfile.mkdtemp(prefix=name, suffix=ext, dir=SAGE_TMP)
    19961999    return os.path.abspath(tmp)
    19972000   
    19982001
     
    20002003# temporary filename
    20012004#################################################################
    20022005
    2003 __tmp_n = 0
     2006def tmp_filename(name="tmp", ext=""):
     2007    r"""
     2008    Create and return a temporary file in
     2009    ``$HOME/.sage/temp/hostname/pid/``
    20042010
    2005 def tmp_filename(name='tmp'):
    2006     name = list(str(name))
    2007     for i in range(len(name)):
    2008         # protect against paths with slashes, colons, etc
    2009         if not (name[i].isalpha() or name[i].isdigit()):
    2010             name[i] = '_'
    2011     name = ''.join(name)
     2011    INPUT:
    20122012
    2013     global __tmp_n
    2014     while True:
    2015         tmp = "%s/%s_%s"%(SAGE_TMP, name, __tmp_n)
    2016         __tmp_n += 1
    2017         if not os.path.exists(tmp):
    2018             break
    2019     return tmp
     2013    - ``name`` -- (default: "tmp") A prefix for the file name.
     2014
     2015    - ``ext`` -- (default: "") A suffix for the file name.
     2016
     2017    OUTPUT: The absolute path of the temporary file created.
     2018
     2019    EXAMPLES::
     2020
     2021        sage: fn = tmp_filename('just_for_testing_', '.extension')
     2022        sage: fn  # random
     2023        '/home/username/.sage/temp/hostname/8044/just_for_testing_tVVHsn.extension'
     2024        sage: _ = open(fn, 'w')
     2025        sage: os.unlink(fn)
     2026    """
     2027    handle, tmp = tempfile.mkstemp(prefix=name, suffix=ext, dir=SAGE_TMP)
     2028    os.close(handle)
     2029    return os.path.abspath(tmp)
    20202030
    20212031def graphics_filename(ext='png'):
    20222032    """