On Unix and Mac OS X, this directory is determined by expanding the path "~", which is expanded by first checking for a HOME environment variable. If none is defined, the USER and LOGNAME environment variables are consulted (in that order) to find a user name, and then system files are consulted to locate the user’s home directory.
On Windows, the user’s home directory is the user-specific profile directory as determined by the Windows registry. If the registry cannot provide a directory for some reason, the value of the USERPROFILE environment variable is used instead, as long as it refers to a directory that exists. If USERPROFILE also fails, the directory is the one specified by the HOMEDRIVE and HOMEPATH environment variables. If those environment variables are not defined, or if the indicated directory still does not exist, the directory containing the current executable is used as the home directory.
the standard directory for storing the current user’s preferences. On Unix, the directory is ".racket" in the user’s home directory. On Windows, it is "Racket" in the user’s application-data folder as specified by the Windows registry; the application-data folder is usually "Application Data" in the user’s profile directory. On Mac OS X, it is "Library/Preferences" in the user’s home directory. This directory might not exist.
a file that contains a symbol-keyed association list of preference values. The file’s directory path always matches the result returned for 'pref-dir. The file name is "racket-prefs.rktd" on Unix and Windows, and it is "org.racket-lang.prefs.rktd" on Mac OS X. The file’s directory might not exist. See also get-preference.
the standard directory for storing temporary files. On Unix and Mac OS X, this is the directory specified by the TMPDIR environment variable, if it is defined, otherwise it is the first path that exists among "/var/tmp", "/usr/tmp", and "/tmp". On Windows, the result is the directory specified by the TMP or TEMP environment variable, if it is defined, otherwise it is the current directory.
the user-specific collection links file for specifying the location of library collections. This file is specified by the PLTLINKSFILE environment variable, and it can be overridden by the --links or -C command-line flag. If no environment variable or flag is specified, or if the value is not a legal path name, then this file defaults to "links.rktd" in the directory reported by (find-system-path 'addon-dir).
a directory for installing user-specific Racket extensions. This directory is specified by the PLTADDONDIR environment variable, and it can be overridden by the --addon or -A command-line flag. If no environment variable or flag is specified, or if the value is not a legal path name, then this directory defaults to "Library/Racket" in the user’s home directory on Mac OS X and 'pref-dir otherwise. This directory might not exist.
the standard directory for storing the current user’s documents. On Unix, it’s the same as 'home-dir. On Mac OS X, it’s the "Documents" directory in the user’s home directory. On Windows, it is the user’s documents folder as specified by the Windows registry; the documents folder is usually "My Documents" in the user’s home directory.
the directory for the current user’s desktop. On Unix, it’s the same as 'home-dir. On Windows, it is the user’s desktop folder as specified by the Windows registry; the documents folder is usually "Desktop" in the user’s home directory. On Mac OS X, it is the desktop directory, which is specifically "~/Desktop" on Mac OS X.
For GRacket, the executable path is the name of a GRacket executable.
the path of the current executable; this may be different from result for 'exec-file because an alternate path was provided through a --name or -N command-line flag to the Racket (or GRacket) executable, or because an embedding executable installed an alternate path. In particular a “launcher” script created by make-racket-launcher sets this path to the script’s path.
a path to the main collection of libraries (see Libraries and Collections). If this path is relative, then it is relative to the executable as reported by (find-system-path 'exec-file)— though the latter could be a soft-link or relative to the user’s executable search path, so that the two results should be combined with find-executable-path. The 'collects-dir path is normally embedded in the Racket executable, but it can be overridden by the --collects or -X command-line flag.
(find-executable-path program-sub [ related-sub deepest?]) → (or/c path? #f) program-sub : path-string? related-sub : (or/c path-string? #f) = #f deepest? : any/c = #f
If related-sub is not #f, then it must be a relative path string, and the path found for program-sub must be such that the file or directory related-sub exists in the same directory as the executable. The result is then the full path for the found related-sub, instead of the path for the executable.
This procedure is used by the Racket executable to find the standard library collection directory (see Libraries and Collections). In this case, program is the name used to start Racket and related is "collects". The related-sub argument is used because, on Unix and Mac OS X, program-sub may involve to a sequence of soft links; in this case, related-sub determines which link in the chain is relevant.
If related-sub is not #f, then when find-executable-path does not finds a program-sub that is a link to another file path, the search can continue with the destination of the link. Further links are inspected until related-sub is found or the end of the chain of links is reached. If deepest? is #f (the default), then the result corresponds to the first path in a chain of links for which related-sub is found (and further links are not actually explored); otherwise, the result corresponds to the last link in the chain for which related-sub is found.
If program-sub is a pathless name, find-executable-path gets the value of the PATH environment variable; if this environment variable is defined, find-executable-path tries each path in PATH as a prefix for program-sub using the search algorithm described above for path-containing program-subs. If the PATH environment variable is not defined, program-sub is prefixed with the current directory and used in the search algorithm above. (On Windows, the current directory is always implicitly the first item in PATH, so find-executable-path checks the current directory first on Windows.)
On Windows, file-exists? reports #t for all variations of the special filenames (e.g., "LPT1", "x:/baddir/LPT1").
The predicates file-exists? or directory-exists? work on the final destination of a link or series of links, while link-exists? only follows links to resolve the base part of path (i.e., everything except the last name in the path).
This procedure never raises the exn:fail:filesystem exception.
(rename-file-or-directory old new [ exists-ok?]) → void? old : path-string? new : path-string? exists-ok? : any/c = #f
This procedure can be used to move a file/directory to a different directory (on the same disk) as well as rename a file/directory within a directory. Unless exists-ok? is provided as a true value, new cannot refer to an existing file or directory. Even if exists-ok? is true, new cannot refer to an existing file when old is a directory, and vice versa.
If new exists and is replaced, the replacement is atomic on Unix and Mac OS X, but it is not guaranteed to be atomic on Windows. Furthermore, if new exists and is opened by any process for reading or writing, then attempting to replace it will typically fail on Windows.
If old is a link, the link is renamed rather than the destination of the link, and it counts as a file for replacing any existing new.
(file-or-directory-modify-seconds path [ secs-n fail-thunk]) → any path : path-string? secs-n : (or/c exact-integer? #f) = #f
fail-thunk : (-> any) = (lambda () (raise (make-exn:fail:filesystem ....)))
If secs-n is provided and not #f, the access and modification times of path are set to the given time.
On error (e.g., if no such file exists), fail-thunk is called, and the default fail-thunk raises exn:fail:filesystem.
(file-or-directory-permissions path [mode]) → (listof (or/c 'read 'write 'execute)) path : path-string? mode : #f = #f (file-or-directory-permissions path mode) → (integer-in 0 65535) path : path-string? mode : 'bits (file-or-directory-permissions path mode) → void path : path-string? mode : (integer-in 0 65535)
If 'bits is supplied as the second argument, the result is a platform-specific integer encoding of the file or directory properties (mostly permissions), and the result is independent of the current user and group. The lowest nine bits of the encoding are somewhat portable, reflecting permissions for the file or directory’s owner, members of the file or directory’s group, or other users:
#o100 : owner has read permission
#o200 : owner has write permission
#o400 : owner has execute permission
#o010 : group has read permission
#o020 : group has write permission
#o040 : group has execute permission
#o001 : others have read permission
#o002 : others have write permission
#o004 : others have execute permission
See also user-read-bit, etc. On Windows, permissions from all three (owner, group, and others) are always the same, and read and execute permission are always available. On Unix and Mac OS X, higher bits have a platform-specific meaning.
If an integer is supplied as the second argument, its is used as an encoding of properties (mostly permissions) to install for the file.
In all modes, the exn:fail:filesystem exception is raised on error (e.g., if no such file exists).
(file-or-directory-identity path [as-link?]) → exact-positive-integer? path : path-string? as-link? : any/c = #f
If as-link? is a true value, then if path refers to a filesystem link, the identity of the link is returned instead of the identity of the referenced file or directory (if any).
(copy-file src dest [exists-ok?]) → void? src : path-string? dest : path-string? exists-ok? : any/c = #f
When the parameter procedure is called to set the current directory, the path argument is cleansed using cleanse-path, simplified using simplify-path, and then converted to a directory path with path->directory-path; cleansing and simplification raise an exception if the path is ill-formed. Thus, the current value of current-directory is always a cleansed, simplified, complete, directory path.
The path is not checked for existence when the parameter is set.
See also the in-directory sequence constructor.
Returns a list of all files and directories in the directory specified by path. On Windows, an element of the list may start with \\?\REL\\.
The racket/runtime-path library provides forms for accessing files and directories at run time using a path that are usually relative to an enclosing source file. Unlike using collection-path, define-runtime-path exposes each run-time path to tools like the executable and distribution creators, so that files and directories needed at run time are carried along in a distribution.
(define-runtime-path id expr)
For run time, id is bound to a path that is based on the result of expr. The path is normally computed by taking a relative path result from expr and adding it to a path for the enclosing file (which is computed as described below). However, tools like the executable creator can also arrange (by colluding with racket/runtime-path) to have a different base path substituted in a generated executable. If expr produces an absolute path, it is normally returned directly, but again may be replaced by an executable creator. In all cases, the executable creator preserves the relative locations of all paths. When expr produces a relative or absolute path, then the path bound to id is always an absolute path.
If expr produces a list of the form (list 'so str),
the value bound to id can be either str or an
absolute path; it is an absolute path when adding the
platform-specific shared-library extension —
If expr produces a list of the form (list 'module module-path var-ref), the value bound to id is a module path index, where module-path is treated as relative (if it is relative) to the module that is the home of the variable reference var-ref, where var-ref can be #f if module-path is absolute. In an executable, the corresponding module is carried along, including all of its dependencies.
For compile-time, the expr result is used by an executable
If a path is needed only on some platforms and not on others, use define-runtime-path-list with an expr that produces an empty list on platforms where the path is not needed.
If the form has a source module according to syntax-source-module, then the source location is determined by preserving the original expression as a syntax object, extracting its source module path at run time (again using syntax-source-module), and then resolving the resulting module path index.
If the expression has no source module, the syntax-source location associated with the form is used, if is a string or path.
In the latter two cases, the path is normally preserved in (platform-specific) byte form. If it is is within the result of find-collects-dir, however, it the path is recorded relative to (find-collects-dir), and it is reconstructed using (find-collects-dir) at run time.
; Access a file "data.txt" at run-time that is originally ; located in the same directory as the module source file: (define-runtime-path data-file "data.txt") (define (read-data) (with-input-from-file data-file (lambda () (read-bytes (file-size data-file))))) ; Load a platform-specific shared object (using ffi-lib) ; that is located in a platform-specific sub-directory of the ; module's source directory: (define-runtime-path libfit-path (build-path "compiled" "native" (system-library-subpath #f) (path-replace-suffix "libfit" (system-type 'so-suffix)))) (define libfit (ffi-lib libfit-path)) ; Load a platform-specific shared object that might be installed ; as part of the operating system, or might be installed ; specifically for Racket: (define-runtime-path libssl-so (case (system-type) [(windows) '(so "ssleay32")] [else '(so "libssl")])) (define libssl (ffi-lib libssl-so))
(define-runtime-paths (id ...) expr)
(define-runtime-path-list id expr)
(define-runtime-module-path-index id module-path-expr)
(define-runtime-module-path id module-path)
The define-runtime-module-path-index form is usually preferred, because it creates a weaker link to the referenced module. Unlike define-runtime-module-path-index, the define-runtime-module-path form creates a for-label dependency from an enclosing module to module-path. Since the dependency is merely for-label, module-path is not instantiated or visited when the enclosing module is instantiated or visited (unless such a dependency is created by other requires), but the code for the referenced module is loaded when the enclosing module is loaded.
(file->string path [#:mode mode-flag]) → string? path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
(file->bytes path [#:mode mode-flag]) → bytes? path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
(file->value path [#:mode mode-flag]) → any path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
(file->list path [proc #:mode mode-flag]) → (listof any/c) path : path-string? proc : (input-port? . -> . any/c) = read mode-flag : (or/c 'binary 'text) = 'binary
(file->lines path [ #:mode mode-flag #:line-mode line-mode]) → (listof string?) path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
line-mode : (or/c 'linefeed 'return 'return-linefeed 'any 'any-one) = 'any
(file->bytes-lines path [ #:mode mode-flag #:line-mode line-mode]) → (listof bytes?) path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
line-mode : (or/c 'linefeed 'return 'return-linefeed 'any 'any-one) = 'any
(display-to-file v path [ #:mode mode-flag #:exists exists-flag]) → void? v : any/c path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
(or/c 'error 'append 'update 'replace 'truncate 'truncate/replace) = 'error
(write-to-file v path [ #:mode mode-flag #:exists exists-flag]) → void? v : any/c path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
(or/c 'error 'append 'update 'replace 'truncate 'truncate/replace) = 'error
(display-lines-to-file lst path [ #:separator separator #:mode mode-flag #:exists exists-flag]) → void? lst : list? path : path-string? separator : any/c = #"\n" mode-flag : (or/c 'binary 'text) = 'binary
(or/c 'error 'append 'update 'replace 'truncate 'truncate/replace) = 'error
(find-files predicate [start-path]) → (listof path?) predicate : (path? . -> . any/c) start-path : (or/c path-string? #f) = #f
The predicate procedure is called with a single argument for each file or directory. If start-path is #f, the argument is a pathname string that is relative to the current directory. Otherwise, it is a path building on start-path. Consequently, supplying (current-directory) for start-path is different from supplying #f, because predicate receives complete paths in the former case and relative paths in the latter. Another difference is that predicate is not called for the current directory when start-path is #f.
If start-path does not refer to an existing file or directory, then predicate will be called exactly once with start-path as the argument.
if a nested path is given, all of its ancestors are also included in the result (but the same ancestor is not added twice);
if a path refers to directory, all of its descendants are also included in the result;
ancestor directories appear before their descendants in the result list.
The proc procedure is called with three arguments for each file, directory, or link:
If start-path is #f, the first argument is a pathname string that is relative to the current directory. Otherwise, the first argument is a pathname that starts with start-path. Consequently, supplying (current-directory) for start-path is different from supplying #f, because proc receives complete paths in the former case and relative paths in the latter. Another difference is that proc is not called for the current directory when start-path is #f.
The second argument is a symbol, either 'file, 'dir, or 'link. The second argument can be 'link when follow-links? is #f, in which case the filesystem traversal does not follow links. If follow-links? is #t, then proc will only get a 'link as a second argument when it encounters a dangling symbolic link (one that does not resolve to an existing file or directory).
The third argument is the accumulated result. For the first call to proc, the third argument is init-val. For the second call to proc (if any), the third argument is the result from the first call, and so on. The result of the last call to proc is the result of fold-files.
The proc argument is used in an analogous way to the procedure argument of foldl, where its result is used as the new accumulated result. There is an exception for the case of a directory (when the second argument is 'dir): in this case the procedure may return two values, the second indicating whether the recursive scan should include the given directory or not. If it returns a single value, the directory is scanned. In the cases of files or links (when the second argument is 'file or 'link), a second value is permitted but ignored.
If the start-path is provided but no such path exists, or if paths disappear during the scan, then an exception is raised.
(make-temporary-file [ template copy-from-filename directory]) → path? template : string? = "rkttmp~a" copy-from-filename : (or/c path-string? #f 'directory) = #f directory : (or/c path-string? #f) = #f
The template argument must be a format string suitable for use with format and one additional string argument (where the string contains only digits). If the resulting string is a relative path, it is combined with the result of (find-system-path 'temp-dir), unless directory is provided and non-#f, in which case the file name generated from template is combined with directory to obtain a full path.
The template argument’s default is only the string "rkttmp~a" when there is no source location information for the callsite of make-temporary-file (or if make-temporary-file is used in a higher-order position). If there is such information, then the template string is based on the source location.
If copy-from-filename is provided as path, the temporary file is created as a copy of the named file (using copy-file). If copy-from-filename is #f, the temporary file is created as empty. If copy-from-filename is 'directory, then the temporary “file” is created as a directory.
When a temporary file is created, it is not opened for reading or writing when the pathname is returned. The client program calling make-temporary-file is expected to open the file with the desired access and flags (probably using the 'truncate flag; see open-output-file) and to delete it when it is no longer needed.
(get-preference name [ failure-thunk flush-mode filename #:use-lock? use-lock? #:timeout-lock-there timeout-lock-there #:lock-there lock-there]) → any name : symbol? failure-thunk : (-> any) = (lambda () #f) flush-mode : any/c = 'timestamp filename : (or/c string-path? #f) = #f use-lock? : any/c = #t timeout-lock-there : (or/c (path? . -> . any) #f) = #f
lock-there : (or/c (path? . -> . any) #f) =
(make-handle-get-preference-locked 0.01 name failure-thunk flush-mode filename #:lock-there timeout-lock-there)
The preference file should contain a list of symbol–value lists written with the default parameter settings. Keys starting with racket:, mzscheme:, mred:, and plt: in any letter case are reserved for use by Racket implementers. If the preference file does not contain a list of symbol–value lists, an error is logged via log-error and failure-thunk is called.
The result of get-preference is the value associated with name if it exists in the association list, or the result of calling failure-thunk otherwise.
Preference settings are cached (weakly) across calls to get-preference, using (path->complete-path filename) as a cache key. If flush-mode is provided as #f, the cache is used instead of re-consulting the preferences file. If flush-mode is provided as 'timestamp (the default), then the cache is used only if the file has a timestamp that is the same as the last time the file was read. Otherwise, the file is re-consulted.
On platforms for which preferences-lock-file-mode returns 'file-lock and when use-lock? is true, preference-file reading is guarded by a lock; multiple readers can share the lock, but writers take the lock exclusively. If the preferences file cannot be read because the lock is unavailable, lock-there is called on the path of the lock file; if lock-there is #f, an exception is raised. The default lock-there handler retries about 5 times (with increasing delays between each attempt) before trying timeout-lock-there, and the default timeout-lock-there triggers an exception.
Old preferences files: When a filename is not provided and the file indicated by (find-system-path 'pref-file) does not exist, the following paths are checked for compatibility with old versions of Racket:
Unix: (expand-user-path "~/.plt-scheme/plt-prefs.ss")
The names argument supplies the preference names, and vals must have the same length as names. Each element of vals must be an instance of a built-in data type whose write output is readable (i.e., the print-unreadable parameter is set to #f while writing preferences).
Current preference values are read from the preference file before updating, and a write lock is held starting before the file read, and lasting until after the preferences file is updated. The lock is implemented by the existence of a file in the same directory as the preference file; see preferences-lock-file-mode for more information. If the directory of the preferences file does not already exist, it is created.
If the write lock is already held, then locked-proc is called with a single argument: the path of the lock file. The default locked-proc (used when the locked-proc argument is #f) reports an error; an alternative thunk might wait a while and try again, or give the user the choice to delete the lock file (in case a previous update attempt encountered disaster and locks are implemented by the presence of the lock file).
If filename is #f or not supplied, and the preference file does not already exist, then values read from the "defaults" collection (if any) are written for preferences that are not mentioned in names.
The 'exists mode is currently used on all platforms except Windows. In 'exists mode, the existence of the lock file indicates that a write lock is held, and readers need no lock (because the preferences file is atomically updated via rename-file-or-directory).
The 'file-lock mode is currently used on Windows. In 'file-lock mode, shared and exclusive locks (in the sense of port-try-file-lock?) on the lock file reflect reader and writer locks on the preference-file content. (The preference file itself is not locked, because a lock would interfere with replacing the file via rename-file-or-directory.)
(make-handle-get-preference-locked delay name [ failure-thunk flush-mode filename #:lock-there lock-there #:max-delay max-delay]) → (path-string? . -> . any) delay : real? name : symbol? failure-thunk : (-> any) = (lambda () #f) flush-mode : any/c = 'timestamp filename : (or/c path-string? #f) = #f lock-there : (or/c (path? . -> . any) #f) = #f max-delay : real? = 0.2
Before calling get-preference, the result procedure uses (sleep delay) to pause. Then, if (* 2 delay) is less than max-delay, the result procedure calls make-handle-get-preference-locked to generate a new retry procedure to pass to get-preference, but with a delay of (* 2 delay). If (* 2 delay) is not less than max-delay, then get-preference is called with the given lock-there, instead.
(call-with-file-lock/timeout filename kind thunk failure-thunk [ #:lock-file lock-file #:delay delay #:max-delay max-delay]) → any filename : (or/c path-string? #f) kind : (or/c 'shared 'exclusive) thunk : (-> any) failure-thunk : (-> any) lock-file : (or/c #f path-string?) = #f delay : (and/c real? (not/c negative?)) = 0.01 max-delay : (and/c real? (not/c negative?)) = 0.2
When thunk returns, call-with-file-lock/timeout releases the lock, returning the result of thunk. The call-with-file-lock/timeout function will retry after delay seconds and continue retrying with exponential backoff until delay reaches max-delay. If call-with-file-lock/timeout fails to obtain the lock, failure-thunk is called in tail position. The kind argument specifies whether the lock is 'shared or 'exclusive in the sense of port-try-file-lock?.
(make-lock-file-name path) → path? path : (or path-string? path-for-some-system?) (make-lock-file-name dir name) → path? dir : (or path-string? path-for-some-system?) name : path-element?
user-read-bit : #o400
user-write-bit : #o200
user-execute-bit : #o100
group-read-bit : #o040
group-write-bit : #o020
group-execute-bit : #o010
other-read-bit : #o004
other-write-bit : #o002
other-execute-bit : #o001