ObjFW
OFFileManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2025 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License version 3.0 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * version 3.0 for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * version 3.0 along with this program. If not, see
17  * <https://www.gnu.org/licenses/>.
18  */
19 
20 #import "OFObject.h"
21 #import "OFDictionary.h"
22 
23 OF_ASSUME_NONNULL_BEGIN
24 
27 #ifdef OF_HAVE_FILES
28 # if (defined(OF_HAVE_CHMOD) && !defined(OF_AMIGAOS) && \
29  !defined(OF_NINTENDO_DS)) || defined(DOXYGEN)
30 # define OF_FILE_MANAGER_SUPPORTS_PERMISSIONS
31 # endif
32 # if (defined(OF_HAVE_CHOWN) && !defined(OF_AMIGAOS)) || defined(DOXYGEN)
33 # define OF_FILE_MANAGER_SUPPORTS_OWNER
34 # endif
35 # if (defined(OF_HAVE_LINK) && !defined(OF_AMIGAOS) && !defined(OF_HAIKU) && \
36  !defined(OF_NINTENDO_DS)) || defined(OF_WINDOWS) || defined(DOXYGEN)
37 # define OF_FILE_MANAGER_SUPPORTS_LINKS
38 # endif
39 # if (defined(OF_HAVE_SYMLINK) && !defined(OF_AMIGAOS) && \
40  !defined(OF_NINTENDO_DS)) || defined(OF_WINDOWS) || defined(DOXYGEN)
41 # define OF_FILE_MANAGER_SUPPORTS_SYMLINKS
42 # endif
43 # if defined(OF_LINUX) || defined(OF_MACOS) || defined(OF_FREEBSD) || \
44  defined(OF_NETBSD) || defined(OF_HAIKU) || defined(OF_SOLARIS) || \
45  defined(DOXYGEN)
46 # define OF_FILE_MANAGER_SUPPORTS_EXTENDED_ATTRIBUTES
47 # endif
48 #endif
49 
50 @class OFArray OF_GENERIC(ObjectType);
51 @class OFConstantString;
52 @class OFDate;
53 @class OFIRI;
54 @class OFString;
55 
78 
96 
102 
107 typedef OFMutableDictionary OF_GENERIC(OFFileAttributeKey, id)
109 
110 #ifdef __cplusplus
111 extern "C" {
112 #endif
113 
119 extern const OFFileAttributeKey OFFileSize;
120 
129 extern const OFFileAttributeKey OFFileType;
130 
138 
146 
154 
162 
170 
178 
186 
194 
202 
210 
219 
224 
229 
234 
239 
244 
249 
254 
263 #ifdef __cplusplus
264 }
265 #endif
266 
273 #ifndef OF_FILE_MANAGER_M
274 OF_SUBCLASSING_RESTRICTED
275 #endif
277 #ifdef OF_HAVE_CLASS_PROPERTIES
278 @property (class, readonly, nonatomic) OFFileManager *defaultManager;
279 #endif
280 
281 #ifdef OF_HAVE_FILES
282 
287 @property (readonly, nonatomic) OFString *currentDirectoryPath;
288 
294 @property (readonly, nonatomic) OFIRI *currentDirectoryIRI;
295 #endif
296 
301 
302 #ifdef OF_HAVE_FILES
303 
312 - (OFFileAttributes)attributesOfItemAtPath: (OFString *)path;
313 #endif
314 
326 - (OFFileAttributes)attributesOfItemAtIRI: (OFIRI *)IRI;
327 
328 #ifdef OF_HAVE_FILES
329 
342 - (void)setAttributes: (OFFileAttributes)attributes
343  ofItemAtPath: (OFString *)path;
344 #endif
345 
361 - (void)setAttributes: (OFFileAttributes)attributes ofItemAtIRI: (OFIRI *)IRI;
362 
363 #ifdef OF_HAVE_FILES
364 
370 - (bool)fileExistsAtPath: (OFString *)path;
371 #endif
372 
381 - (bool)fileExistsAtIRI: (OFIRI *)IRI;
382 
383 #ifdef OF_HAVE_FILES
384 
390 - (bool)directoryExistsAtPath: (OFString *)path;
391 #endif
392 
401 - (bool)directoryExistsAtIRI: (OFIRI *)IRI;
402 
403 #ifdef OF_HAVE_FILES
404 
410 - (void)createDirectoryAtPath: (OFString *)path;
411 
420 - (void)createDirectoryAtPath: (OFString *)path
421  createParents: (bool)createParents;
422 #endif
423 
432 - (void)createDirectoryAtIRI: (OFIRI *)IRI;
433 
444 - (void)createDirectoryAtIRI: (OFIRI *)IRI createParents: (bool)createParents;
445 
446 #ifdef OF_HAVE_FILES
447 
457 - (OFArray OF_GENERIC(OFString *) *)contentsOfDirectoryAtPath: (OFString *)path;
458 #endif
459 
473 - (OFArray OF_GENERIC(OFIRI *) *)contentsOfDirectoryAtIRI: (OFIRI *)IRI;
474 
475 #ifdef OF_HAVE_FILES
476 
485 - (OFArray OF_GENERIC(OFString *) *)subpathsOfDirectoryAtPath: (OFString *)path;
486 
494 - (void)changeCurrentDirectoryPath: (OFString *)path;
495 
503 - (void)changeCurrentDirectoryIRI: (OFIRI *)IRI;
504 
521 - (void)copyItemAtPath: (OFString *)source toPath: (OFString *)destination;
522 #endif
523 
542 - (void)copyItemAtIRI: (OFIRI *)source toIRI: (OFIRI *)destination;
543 
544 #ifdef OF_HAVE_FILES
545 
566 - (void)moveItemAtPath: (OFString *)source toPath: (OFString *)destination;
567 #endif
568 
592 - (void)moveItemAtIRI: (OFIRI *)source toIRI: (OFIRI *)destination;
593 
594 #ifdef OF_HAVE_FILES
595 
603 - (void)removeItemAtPath: (OFString *)path;
604 #endif
605 
616 - (void)removeItemAtIRI: (OFIRI *)IRI;
617 
618 #ifdef OF_FILE_MANAGER_SUPPORTS_LINKS
619 
633 - (void)linkItemAtPath: (OFString *)source toPath: (OFString *)destination;
634 #endif
635 
652 - (void)linkItemAtIRI: (OFIRI *)source toIRI: (OFIRI *)destination;
653 
654 #ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS
655 
672 - (void)createSymbolicLinkAtPath: (OFString *)path
673  withDestinationPath: (OFString *)target;
674 #endif
675 
693 - (void)createSymbolicLinkAtIRI: (OFIRI *)IRI
694  withDestinationPath: (OFString *)target;
695 
696 #ifdef OF_FILE_MANAGER_SUPPORTS_EXTENDED_ATTRIBUTES
697 
712 - (OFData *)extendedAttributeDataForName: (OFString *)name
713  ofItemAtPath: (OFString *)path;
714 
734 - (void)getExtendedAttributeData: (OFData *_Nonnull *_Nonnull)data
735  andType: (id _Nullable *_Nullable)type
736  forName: (OFString *)name
737  ofItemAtPath: (OFString *)path;
738 #endif
739 
757 - (OFData *)extendedAttributeDataForName: (OFString *)name
758  ofItemAtIRI: (OFIRI *)IRI;
759 
781 - (void)getExtendedAttributeData: (OFData *_Nonnull *_Nonnull)data
782  andType: (id _Nullable *_Nullable)type
783  forName: (OFString *)name
784  ofItemAtIRI: (OFIRI *)IRI;
785 
786 #ifdef OF_FILE_MANAGER_SUPPORTS_EXTENDED_ATTRIBUTES
787 
801 - (void)setExtendedAttributeData: (OFData *)data
802  forName: (OFString *)name
803  ofItemAtPath: (OFString *)path;
804 
824 - (void)setExtendedAttributeData: (OFData *)data
825  andType: (nullable id)type
826  forName: (OFString *)name
827  ofItemAtPath: (OFString *)path;
828 #endif
829 
846 - (void)setExtendedAttributeData: (OFData *)data
847  forName: (OFString *)name
848  ofItemAtIRI: (OFIRI *)IRI;
849 
871 - (void)setExtendedAttributeData: (OFData *)data
872  andType: (nullable id)type
873  forName: (OFString *)name
874  ofItemAtIRI: (OFIRI *)IRI;
875 
876 #ifdef OF_FILE_MANAGER_SUPPORTS_EXTENDED_ATTRIBUTES
877 
890 - (void)removeExtendedAttributeForName: (OFString *)name
891  ofItemAtPath: (OFString *)path;
892 #endif
893 
909 - (void)removeExtendedAttributeForName: (OFString *)name
910  ofItemAtIRI: (OFIRI *)IRI;
911 @end
912 
913 @interface OFDictionary (FileAttributes)
919 @property (readonly, nonatomic) unsigned long long fileSize;
920 
926 @property (readonly, nonatomic) OFFileAttributeType fileType;
927 
933 @property (readonly, nonatomic) unsigned long filePOSIXPermissions;
934 
940 @property (readonly, nonatomic) unsigned long fileOwnerAccountID;
941 
947 @property (readonly, nonatomic) unsigned long fileGroupOwnerAccountID;
948 
954 @property (readonly, nonatomic) OFString *fileOwnerAccountName;
955 
961 @property (readonly, nonatomic) OFString *fileGroupOwnerAccountName;
962 
968 @property (readonly, nonatomic) OFDate *fileLastAccessDate;
969 
975 @property (readonly, nonatomic) OFDate *fileModificationDate;
976 
982 @property (readonly, nonatomic) OFDate *fileStatusChangeDate;
983 
989 @property (readonly, nonatomic) OFDate *fileCreationDate;
990 
996 @property (readonly, nonatomic) OFString *fileSymbolicLinkDestination;
997 
1003 @property (readonly, nonatomic)
1004  OFArray OF_GENERIC(OFString *) *fileExtendedAttributesNames;
1005 @end
1006 
1007 OF_ASSUME_NONNULL_END
An abstract class for storing objects in an array.
Definition: OFArray.h:107
const OFFileAttributeKey OFFileModificationDate
The last modification date of the file as an OFDate.
const OFFileAttributeType OFFileTypeSocket
A socket.
OFConstantString * OFFileAttributeKey
A key for a file attribute in the file attributes dictionary.
Definition: OFFileManager.h:54
const OFFileAttributeKey OFFileLastAccessDate
The last access date of the file as an OFDate.
const OFFileAttributeType OFFileTypeSymbolicLink
A symbolic link.
const OFFileAttributeType OFFileTypeFIFO
A FIFO.
A class for storing, accessing and comparing dates.
Definition: OFDate.h:33
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:41
A class which provides management for files, e.g. reading contents of directories, deleting files, renaming files, etc.
Definition: OFFileManager.h:276
The root class for all other classes inside ObjFW.
Definition: OFObject.h:690
OFIRI * currentDirectoryIRI
The IRI of the current working directory.
Definition: OFFileManager.h:294
const OFFileAttributeKey OFFileExtendedAttributesNames
The names of the extended attributes as an OFArray of OFString.
const OFFileAttributeKey OFFileOwnerAccountID
The account ID of the owner of the file as an OFNumber.
const OFFileAttributeKey OFFileSymbolicLinkDestination
The destination of a symbolic link as an OFString.
const OFFileAttributeKey OFFileSize
The size of the file as an OFNumber.
OFDictionary * OFFileAttributes
A dictionary mapping keys of type OFFileAttributeKey to their attribute values.
Definition: OFFileManager.h:101
const OFFileAttributeKey OFFileGroupOwnerAccountID
The account ID of the group owner of the file as an OFNumber.
A class for handling strings.
Definition: OFString.h:142
const OFFileAttributeKey OFFileStatusChangeDate
The last status change date of the file as an OFDate.
const OFFileAttributeKey OFFileType
The type of the file.
An abstract class for storing objects in a dictionary.
Definition: OFDictionary.h:82
const OFFileAttributeKey OFFileOwnerAccountName
The account name of the owner of the file as an OFString.
OFFileManager * defaultManager()
Returns the default file manager.
Definition: OFFileManager.m:136
An abstract class for storing and changing objects in a dictionary.
Definition: OFMutableDictionary.h:46
const OFFileAttributeType OFFileTypeDirectory
A directory.
const OFFileAttributeType OFFileTypeBlockSpecial
A block special file.
A class for representing IRIs, URIs, URLs and URNs, for parsing them as well as accessing parts of th...
Definition: OFIRI.h:39
const OFFileAttributeType OFFileTypeCharacterSpecial
A character special file.
const OFFileAttributeKey OFFilePOSIXPermissions
The POSIX permissions of the file as an OFNumber.
A class for storing arbitrary data in an array.
Definition: OFData.h:45
const OFFileAttributeKey OFFileCreationDate
The creation date of the file as an OFDate.
const OFFileAttributeType OFFileTypeRegular
A regular file.
const OFFileAttributeType OFFileTypeUnknown
An unknown file type.
OFString * currentDirectoryPath
The path of the current working directory.
Definition: OFFileManager.h:287
const OFFileAttributeKey OFFileGroupOwnerAccountName
The account name of the group owner of the file as an OFString.
OFMutableDictionary * OFMutableFileAttributes
A mutable dictionary mapping keys of type OFFileAttributeKey to their attribute values.
Definition: OFFileManager.h:108
OFConstantString * OFFileAttributeType
The type of a file.
Definition: OFFileManager.h:95