1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include
"nsISupports.idl"
7 #include
"nsIDirectoryEnumerator.idl"
13 #include
"mozilla/Path.h"
15 #include
"nsStringFwd.h"
17 using PathString
= nsTString
<filesystem
::Path
::value_type
>;
18 using PathSubstring
= nsTSubstring
<filesystem
::Path
::value_type
>;
19 } // namespace mozilla
22 [ptr] native PRFileDescStar
(PRFileDesc
);
23 [ptr] native PRLibraryStar
(PRLibrary
);
24 [ptr] native FILE
(FILE
);
25 native PathString
(mozilla
::PathString
);
28 * An nsIFile is an abstract representation of a filename. It manages
29 * filename encoding issues, pathname component separators ('/' vs. '\\'
30 * vs. ':') and weird stuff like differing volumes with identical names, as
31 * on pre-Darwin Macintoshes.
33 * This file has long introduced itself to new hackers with this opening
36 * This is the only correct cross-platform way to specify a file.
37 * Strings are not such a way. If you grew up on windows or unix, you
38 * may think they are. Welcome to reality.
40 * While taking the pose struck here to heart would be uncalled for, one
41 * may safely conclude that writing cross-platform code is an embittering
44 * All methods with string parameters have two forms. The preferred
45 * form operates on UCS-2 encoded characters strings. An alternate
46 * form operates on characters strings encoded in the "native" charset.
48 * A string containing characters encoded in the native charset cannot
49 * be safely passed to javascript via xpconnect. Therefore, the "native
50 * methods" are not scriptable.
52 [scriptable
, main_process_scriptable_only
, uuid(2fa6884a
-ae65
-412a
-9d4c
-ce6e34544ba1
), builtinclass
]
53 interface nsIFile
: nsISupports
58 * NORMAL_FILE_TYPE - A normal file.
59 * DIRECTORY_TYPE - A directory/folder.
61 const unsigned long NORMAL_FILE_TYPE
= 0;
62 const unsigned long DIRECTORY_TYPE
= 1;
67 * This function is used for constructing a descendent of the
71 * A string which is intended to be a child node of the nsIFile.
72 * For security reasons, this cannot contain .. and cannot start with
73 * a directory separator. For the |appendNative| method, the node must
74 * be in the native filesystem charset.
76 void append
(in AString node
);
77 [noscript
] void appendNative
(in ACString node
);
80 * Normalize the pathName (e.g. removing .. and . components on Unix).
87 * This function will create a new file or directory in the
88 * file system. Any nodes that have not been created or
89 * resolved, will be. If the file or directory already
90 * exists create() will return NS_ERROR_FILE_ALREADY_EXISTS.
93 * This specifies the type of file system object
94 * to be made. The only two types at this time
95 * are file and directory which are defined above.
96 * If the type is unrecongnized, we will return an
97 * error (NS_ERROR_FILE_UNKNOWN_TYPE).
100 * The unix style octal permissions. This may
101 * be ignored on systems that do not need to do
104 * @param skipAncestors
105 * Optional; if set to true, we'll skip creating
106 * ancestor directories (and return an error instead).
108 [must_use
] void create
(in unsigned long type
, in unsigned long permissions
,
109 [optional,default(false
)] in bool skipAncestors
);
112 * Accessor to the leaf name of the file itself.
113 * For the |nativeLeafName| method, the nativeLeafName must
114 * be in the native filesystem charset.
116 attribute AString leafName
;
117 [noscript
] attribute ACString nativeLeafName
;
120 * The leaf name as displayed in OS-provided file pickers and similar UI.
121 * On Windows and macOS, 'real' leaf names of some directories can be
122 * in English, but the OS will show a different, translated name to users
123 * using a different locale. So folders like "Downloads", "Desktop" and
124 * "Documents" might not normally appear to users with that (English) name,
125 * but with an OS-localized translation. This API will return such a
126 * translation if it exists, or the leafName if it doesn't.
127 * On Linux, this will always be the same as `leafName`.
129 readonly attribute AString displayName
;
134 * This will copy this file to the specified newParentDir.
135 * If a newName is specified, the file will be renamed.
136 * If 'this' is not created we will return an error
137 * (NS_ERROR_FILE_NOT_FOUND).
139 * copyTo may fail if the file already exists in the destination
142 * copyTo will NOT resolve aliases/shortcuts during the copy.
144 * @param newParentDir
145 * This param is the destination directory. If the
146 * newParentDir is null, copyTo() will use the parent
147 * directory of this file. If the newParentDir is not
148 * empty and is not a directory, an error will be
149 * returned (NS_ERROR_FILE_DESTINATION_NOT_DIR). For the
150 * |CopyToNative| method, the newName must be in the
151 * native filesystem charset.
154 * This param allows you to specify a new name for
155 * the file to be copied. This param may be empty, in
156 * which case the current leaf name will be used.
158 void copyTo
(in nsIFile newParentDir
, in AString newName
);
159 [noscript
] void CopyToNative
(in nsIFile newParentDir
, in ACString newName
);
162 * copyToFollowingLinks[Native]
164 * This function is identical to copyTo with the exception that,
165 * as the name implies, it follows symbolic links. The XP_UNIX
166 * implementation always follow symbolic links when copying. For
167 * the |CopyToFollowingLinks| method, the newName must be in the
168 * native filesystem charset.
170 void copyToFollowingLinks
(in nsIFile newParentDir
, in AString newName
);
171 [noscript
] void copyToFollowingLinksNative
(in nsIFile newParentDir
, in ACString newName
);
176 * A method to move this file or directory to newParentDir.
177 * If a newName is specified, the file or directory will be renamed.
178 * If 'this' is not created we will return an error
179 * (NS_ERROR_FILE_NOT_FOUND).
180 * If 'this' is a file, and the destination file already exists, moveTo
181 * will replace the old file.
182 * This object is updated to refer to the new file.
184 * moveTo will NOT resolve aliases/shortcuts during the copy.
185 * moveTo will do the right thing and allow copies across volumes.
186 * moveTo will return an error (NS_ERROR_FILE_DIR_NOT_EMPTY) if 'this' is
187 * a directory and the destination directory is not empty.
188 * moveTo will return an error (NS_ERROR_FILE_ACCESS_DENIED) if 'this' is
189 * a directory and the destination directory is not writable.
191 * @param newParentDir
192 * This param is the destination directory. If the
193 * newParentDir is empty, moveTo() will rename the file
194 * within its current directory. If the newParentDir is
195 * not empty and does not name a directory, an error will
196 * be returned (NS_ERROR_FILE_DESTINATION_NOT_DIR). For
197 * the |moveToNative| method, the newName must be in the
198 * native filesystem charset.
201 * This param allows you to specify a new name for
202 * the file to be moved. This param may be empty, in
203 * which case the current leaf name will be used.
205 void moveTo
(in nsIFile newParentDir
, in AString newName
);
206 [noscript
] void moveToNative
(in nsIFile newParentDir
, in ACString newName
);
209 * moveToFollowingLinks[Native]
211 * This function is identical to moveTo with the exception that,
212 * as the name implies, it follows symbolic links. The XP_UNIX
213 * implementation always follows symbolic links when moving. For
214 * the |MoveToFollowingLinks| method, the newName ust be in the native
215 * filesystem charset.
217 void moveToFollowingLinks
(in nsIFile newParentDir
, in AString newName
);
218 [noscript
] void moveToFollowingLinksNative
(in nsIFile newParentDir
, in ACString newName
);
223 * This method is identical to moveTo except that if this file or directory
224 * is moved to a a different volume, it fails and returns an error
225 * (NS_ERROR_FILE_ACCESS_DENIED).
226 * This object will still point to the old location after renaming.
228 void renameTo
(in nsIFile newParentDir
, in AString newName
);
229 [noscript
] void renameToNative
(in nsIFile newParentDir
, in ACString newName
);
232 * This will try to delete this file. The 'recursive' flag
233 * must be PR_TRUE to delete directories which are not empty.
235 * If passed, 'removeCount' will be incremented by the total number of files
236 * and/or directories removed. Will be 1 unless the 'recursive' flag is
237 * set. The parameter must be initialized beforehand.
239 * This will not resolve any symlinks.
241 void remove
(in boolean recursive
, [optional] inout uint32_t removeCount
);
244 * Attributes of nsIFile.
247 attribute
unsigned long permissions
;
248 attribute
unsigned long permissionsOfLink
;
251 * The last accesss time of the file in milliseconds from midnight, January
252 * 1, 1970 GMT, if available.
254 attribute PRTime lastAccessedTime
;
255 attribute PRTime lastAccessedTimeOfLink
;
258 * File Times are to be in milliseconds from
259 * midnight (00:00:00), January 1, 1970 Greenwich Mean
262 attribute PRTime lastModifiedTime
;
263 attribute PRTime lastModifiedTimeOfLink
;
266 * The creation time of file in milliseconds from midnight, January 1, 1970
269 * This attribute is only implemented on Windows and macOS. Accessing this
270 * on another platform will this will throw NS_ERROR_NOT_IMPLEMENTED.
272 readonly attribute PRTime creationTime
;
273 readonly attribute PRTime creationTimeOfLink
;
276 * WARNING! On the Mac, getting/setting the file size with nsIFile
277 * only deals with the size of the data fork. If you need to
278 * know the size of the combined data and resource forks use the
279 * GetFileSizeWithResFork() method defined on nsILocalFileMac.
281 attribute int64_t fileSize
;
282 readonly attribute int64_t fileSizeOfLink
;
287 * Accessor to the string path. The native version of these
288 * strings are not guaranteed to be a usable path to pass to
289 * NSPR or the C stdlib. There are problems that affect
290 * platforms on which a path does not fully specify a file
291 * because two volumes can have the same name (e.g., mac).
292 * This is solved by holding "private", native data in the
293 * nsIFile implementation. This native data is lost when
294 * you convert to a string.
296 * DO NOT PASS TO USE WITH NSPR OR STDLIB!
299 * Find out what the symlink points at. Will give error
300 * (NS_ERROR_FILE_INVALID_PATH) if not a symlink.
303 * Find out what the nsIFile points at.
305 * Note that the ACString attributes are returned in the
306 * native filesystem charset.
309 readonly attribute AString target
;
310 [noscript
] readonly attribute ACString nativeTarget
;
311 readonly attribute AString path
;
312 [notxpcom
,nostdcall
,must_use
] PathString nativePath
();
315 nsresult GetNativePath
(nsACString
& aPath
);
318 * Returns a human-readable path string.
320 nsCString HumanReadablePath
();
324 boolean isWritable
();
325 boolean isReadable
();
326 boolean isExecutable
();
328 boolean isDirectory
();
332 * Not a regular file, not a directory, not a symlink.
339 * This function will create a new file or directory in the
340 * file system. Any nodes that have not been created or
341 * resolved, will be. If this file already exists, we try
342 * variations on the leaf name "suggestedName" until we find
343 * one that did not already exist.
345 * If the search for nonexistent files takes too long
346 * (thousands of the variants already exist), we give up and
347 * return NS_ERROR_FILE_TOO_BIG.
350 * This specifies the type of file system object
351 * to be made. The only two types at this time
352 * are file and directory which are defined above.
353 * If the type is unrecongnized, we will return an
354 * error (NS_ERROR_FILE_UNKNOWN_TYPE).
357 * The unix style octal permissions. This may
358 * be ignored on systems that do not need to do
362 void createUnique
(in unsigned long type
, in unsigned long permissions
);
367 * This function will allocate and initialize a nsIFile object to the
368 * exact location of the |this| nsIFile.
371 * A nsIFile which this object will be initialize
378 * Will determine if the inFile equals this.
380 boolean equals
(in nsIFile inFile
);
383 * Will determine if inFile is a descendant of this file.
384 * This routine looks in subdirectories too.
386 boolean contains
(in nsIFile inFile
);
389 * Parent will be null when this is at the top of the volume.
391 readonly attribute nsIFile parent
;
394 * Returns an enumeration of the elements in a directory. Each
395 * element in the enumeration is an nsIFile.
397 * @throws NS_ERROR_FILE_NOT_DIRECTORY if the current nsIFile does
398 * not specify a directory.
400 [binaryname
(DirectoryEntriesImpl
)]
401 readonly attribute nsIDirectoryEnumerator directoryEntries
;
404 nsresult GetDirectoryEntries
(nsIDirectoryEnumerator
** aOut
)
406 return GetDirectoryEntriesImpl
(aOut
);
411 * initWith[Native]Path
413 * This function will initialize the nsIFile object. Any
414 * internal state information will be reset.
417 * A string which specifies a full file path to a
418 * location. Relative paths will be treated as an
419 * error (NS_ERROR_FILE_UNRECOGNIZED_PATH). For
420 * initWithNativePath, the filePath must be in the native
421 * filesystem charset.
423 void initWithPath
(in AString filePath
);
424 [noscript
] void initWithNativePath
(in ACString filePath
);
429 * Initialize this object with another file
432 * the file this becomes equivalent to
434 void initWithFile
(in nsIFile aFile
);
437 * Flag for openNSPRFileDesc(), to hint to the OS that the file will be
438 * read sequentially with agressive readahead.
440 const unsigned long OS_READAHEAD
= 0x40000000;
443 * Flag for openNSPRFileDesc(). Deprecated and unreliable!
444 * Instead use NS_OpenAnonymousTemporaryFile() to create a temporary
445 * file which will be deleted upon close!
447 const unsigned long DELETE_ON_CLOSE
= 0x80000000;
450 * Return the result of PR_Open on the file. The caller is
451 * responsible for calling PR_Close on the result. On success, the
452 * returned PRFileDescr must be non-null.
454 * @param flags the PR_Open flags from prio.h, plus optionally
455 * OS_READAHEAD or DELETE_ON_CLOSE. OS_READAHEAD is a hint to the
456 * OS that the file will be read sequentially with agressive
457 * readahead. DELETE_ON_CLOSE is unreliable on Windows and is deprecated.
458 * Instead use NS_OpenAnonymousTemporaryFile() to create a temporary
459 * file which will be deleted upon close.
461 [noscript
, must_use
] PRFileDescStar openNSPRFileDesc
(in long flags
,
465 * Return the result of fopen on the file. The caller is
466 * responsible for calling fclose on the result. On success, the
467 * returned FILE pointer must be non-null.
469 [noscript
, must_use
] FILE openANSIFileDesc
(in string mode
);
472 * Return the result of PR_LoadLibrary on the file. The caller is
473 * responsible for calling PR_UnloadLibrary on the result.
475 [noscript
, must_use
] PRLibraryStar load
();
477 // number of bytes available on disk to non-superuser
478 [must_use
] readonly attribute int64_t diskSpaceAvailable
;
480 // disk capacity in bytes
481 [must_use
] readonly attribute int64_t diskCapacity
;
484 * appendRelative[Native]Path
486 * Append a relative path to the current path of the nsIFile object.
488 * @param relativeFilePath
489 * relativeFilePath is a native relative path. For security reasons,
490 * this cannot contain .. and cannot start with a directory separator.
491 * For the |appendRelativeNativePath| method, the relativeFilePath
492 * must be in the native filesystem charset.
494 void appendRelativePath
(in AString relativeFilePath
);
495 [noscript
] void appendRelativeNativePath
(in ACString relativeFilePath
);
498 * Accessor to a null terminated string which will specify
499 * the file in a persistent manner for disk storage.
501 * The character set of this attribute is undefined. DO NOT TRY TO
502 * INTERPRET IT AS HUMAN READABLE TEXT!
504 [must_use
] attribute ACString persistentDescriptor
;
509 * Ask the operating system to open the folder which contains
510 * this file or folder. This routine only works on platforms which
511 * support the ability to open a folder and is run async on Windows.
512 * This routine must be called on the main.
514 [must_use
] void reveal
();
519 * Ask the operating system to attempt to open the file.
520 * this really just simulates "double clicking" the file on your platform.
521 * This routine only works on platforms which support this functionality
522 * and is run async on Windows. This routine must be called on the
525 [must_use
] void launch
();
528 * getRelativeDescriptor
530 * Returns a relative file path in an opaque, XP format. It is therefore
533 * The character set of the string returned from this function is
534 * undefined. DO NOT TRY TO INTERPRET IT AS HUMAN READABLE TEXT!
537 * the file from which the descriptor is relative.
538 * Throws if fromFile is null.
540 [must_use
] ACString getRelativeDescriptor
(in nsIFile fromFile
);
543 * setRelativeDescriptor
545 * Initializes the file to the location relative to fromFile using
546 * a string returned by getRelativeDescriptor.
549 * the file to which the descriptor is relative
551 * the relative descriptor obtained from getRelativeDescriptor
554 void setRelativeDescriptor
(in nsIFile fromFile
, in ACString relativeDesc
);
559 * Returns a relative file from 'fromFile' to this file as a UTF-8 string.
560 * Going up the directory tree is represented via "../". '/' is used as
561 * the path segment separator. This is not a native path, since it's UTF-8
565 * the file from which the path is relative.
566 * Throws if fromFile is null.
568 [must_use
] AUTF8String getRelativePath
(in nsIFile fromFile
);
573 * Initializes the file to the location relative to fromFile using
574 * a string returned by getRelativePath.
577 * the file from which the path is relative
579 * the relative path obtained from getRelativePath
582 void setRelativePath
(in nsIFile fromFile
, in AUTF8String relativeDesc
);
586 #ifdef MOZILLA_INTERNAL_API
587 #include
"nsDirectoryServiceUtils.h"
588 #include
"nsString.h"
590 inline std
::ostream
& operator
<<(std
::ostream
& aOut
, const nsIFile
& aFile
) {
591 nsIFile
* file
= const_cast
<nsIFile
*>(&aFile
);
594 return aOut
<< "nsIFile { " << path
<< " }";