Files and Storage

RSS for tag

Ask questions about file systems and block storage.

Posts under Files and Storage tag

200 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

JSONEncoder has changed
Prior to Sonoma 14.0 and Xcode 15.0.1 my custom class was encoded in a consistent pattern that I exploited to append the file many thousands of times. Now, the encodings are of a seemingly random pattern that cannot be exploited to append. This may be a result of efforts to make the encoding more efficient or virtually any other reason. My questions are: how do I revert to the previous JSONEncoder while using current OS & Xcode versions?, i.e, what files and in what locations do I restore? is it likely to be sustainable if I did so? It has been previously noted that atomic files are not "append able" which is why I used JSON in the first place. Any info or suggestion is appreciated. Thank you.
2
1
461
Nov ’23
NSFileCoordinator woes
Hi all, I’ve never used NSFileCoordinator class, simply because I haven’t had any need for it until now. And now that I’m about to need it rather soon, I’m trying to get a grasp on it, but it does’t go very well so far. Even though the documentation reads pretty clear, I don’t seem to get it correctly. Also, I haven’t found any example online, which would be of any value to me, so I’m taking my chances here. In order to begin to understand it, I’ll start with the simplest real case scenario, for which I don’t even understand yet whether NSFileCoordinator is the way to go. Let’s say, I have a bunch of directories in a parent directory (so, all are at the same level in the file system hierarchy), which I want to compress and pack in a ZIP archive (for example) programatically (e.g. using libarchive). After selecting which directories to zip, I’d like to ensure that once the zipping operation has started, any changes to all files and directories in the hierarchies of selected directories to be packed will stay unchanged, until the zipping operation is complete. That means, no file anywhere in the hierarchies should be modified in any way and no directory or file anywhere in the hierarchies should be added, deleted or moved. I thought I would achieve this by using one NSFileCoordinator and a bunch NSFileAccessIntent objects, each for every directory at the top level of the hierarchies. The intents will be for reading without changing, because that’s what I want to do; just to read those directories and files (and zip them) without making any changes to them. The code would be something like this (I want to do the operation asynchronously): NSMutableArray *intents = [NSMutableArray arrayWithCapacity:[sources count]]; for (NSURL *source in sources) [intents addObject:[NSFileAccessIntent readingIntentWithURL:source options:NSFileCoordinatorReadingWithoutChanges]]; NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; [coordinator coordinateAccessWithIntents:intents queue:[NSOperationQueue new] byAccessor:^(NSError *error) { // Do the zipping here. }]; It looks simple and straightforward, but it doesn’t really work as I thought it would. Once the operation is started, I can modify files and directories the way I described above, from within the same application/process, as well as others (using NSFileCoordinator approach), like Finder. Hence, I’d really appreciate if someone can point me in the right direction. For example, what needs to be done to achieve this simple example I described above. Once I get some pointers, I may start understanding those classes and documentation in the right way and work myself through more complex examples and use cases. Thanks for any insights. -- Dragan
0
0
348
Nov ’23
#Gui Linux virtual machine on Mac
Good day , thank you very much for the #virtualization article I have now managed to create a linux(#debian 12/bookworm) virtual machine I now have the following questions. How can i increase the size of the GUI Linus VM.bundle from 68.72G to 150G? Unable to obtain an IP V4 address to a bridge (have intall brige-utils package) - can get IP v6 address without any problem. my #bridge setup is auto br0 iface br0 inet dhcp bridge_ports enp0s1 bridge-stp on bridge-fd 0 Unable to ping the host from the virtual machine , but able to ping vms from the host - no firewalls are running, how can i enable icmp from vm to host and host to other containers. my target is to run lxd/incus containers to test few of my applications in apple silicon I am now able to run lxd/incus without any problem. however i am not able to access the application that are running in the containers, provided I am logged into the linux vm which beats the purpose of publishing the my apps, few of the services are IBM informix REST API / Nginx . My #requirement is 1, increase the size of the vm image.(#GUI Linux vm.bundle) 2. Obtain #ip address V4 for br0 3. Run #lxd / #incus containers 4. #icmp comms between host / vm / containers Please let me know if there are possibilities to achieve my requirement. Thank you
0
0
420
Nov ’23
makeFileSystemObjectSource does not monitor changes made with nano
I have folder monitoring code using makeFileSystemObjectSource. The events are triggered for everything else, but not for when I edit a file with nano terminal command. Am I doing something wrong? Is it a bug? Is this intended and unfixable? Code sample: monitoredFolderFileDescriptor = open(currentlyMonitoredPath, O_EVTONLY) folderMonitorSource = DispatchSource.makeFileSystemObjectSource(fileDescriptor: monitoredFolderFileDescriptor, eventMask: .all, queue: .main) folderMonitorSource?.setEventHandler { // ... } folderMonitorSource?.setCancelHandler { // ... } folderMonitorSource?.resume()
2
0
621
Nov ’23
recordAccelerometer
I am writing a code to capture the accelerometer data for one hour, it suppose to save in the iOS device first, and then I would like to upload to cloudkit. Here is my code. import UIKit import CoreMotion import CloudKit class ViewController: UIViewController { @IBOutlet weak var accelerometerSwitch: UISwitch! private let recorder = CMSensorRecorder() private let cloudKitContainer = CKContainer(identifier: "iCloud.com.example.accelerometer-recorder") override func viewDidLoad() { super.viewDidLoad() accelerometerSwitch.addTarget(self, action: #selector(accelerometerSwitchValueChanged), for: .valueChanged) } private func saveDataToAppFolder(data: [CMSensorRecordingData]) { let fileName = "accelerometer_data.dat" let fileManager = FileManager.default let documentsDirectoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! let fileURL = documentsDirectoryURL.appendingPathComponent(fileName) let jsonData = try! JSONEncoder().encode(data) try! jsonData.write(to: fileURL) } @objc private func accelerometerSwitchValueChanged() { if accelerometerSwitch.isOn { recorder.recordAccelerometer(forDuration: 3600.0) { sensorData in if let sensorData = sensorData { // Handle the recorded data saveDataToAppFolder(data: sensorData) uploadDataToCloudKit(data: sensorData) } else { print("No accelerometer data recorded") } } } else { // No need to stop recording since it will stop automatically after the duration } } private func uploadDataToCloudKit(filePath: String) { let fileURL = URL(fileURLWithPath: filePath) let data = try! Data(contentsOf: fileURL) let record = CKRecord(recordType: "RecordedData") record["data"] = data let database = cloudKitContainer.publicCloudDatabase database.save(record, completionHandler: { record, error in if let error = error { print("Error uploading data to CloudKit: \(error)") } else { print("Data uploaded to CloudKit successfully") } }) } } Anyone can help to fix the error. Thanks a lot.
1
0
474
Nov ’23
`fetchContents(for:version:request:completionHandler:)` get the requesting executable name
I was successfully able to get the callback for file reads for the files in my mounted folder. Now we need to implement some read protection on top of that, we need to only allow some specific applications to access the data through the File provider. for that, we need to get the name or any information regarding the Application that requested the read. one thing I've seen is the request.requestingExecutable , which is coming nil for all the applications trying to read the files in the mounted folder. But i do get applications name for the things done by finder (drag&drop and Copy paste) . What are the ways I can get the reading Application names ? and what could be done for implementing data leak features working in pair with the File Provider Extension ?
2
0
516
Mar ’24
ios17 container problem occur when I replace container in xcode
it can read the files before I replace container in xcode . Has anyone encountered the same problem? and any solution? Error: Error Domain=NSCocoaErrorDomain Code=257 "The file “Documents” couldn’t be opened because you don’t have permission to view it." UserInfo={NSUserStringVariant=( Folder ), NSFilePath=/var/mobile/Containers/Data/Application/97D816A0-8B58-4E33-8455-3F19E0A9FBAA/Documents, NSUnderlyingError=0x281e650b0 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}
4
0
1k
Dec ’23
Generic Protocol Specification for USB Flash Drive to communicate with IOS App
Hello Everyone, I am developing an IOS application for communicating with USB Flash Drive. For that I am going to use EAAccessory Framework. To be use EAAccessory Framework I need to mention protocol specification of USB Flash Drive (hardware) in info.plist file. Looking forward to know generic protocol specification which will enable IOS application to communicate with most or all USB flash drives.
2
0
550
Nov ’23
Selecting Word to open document from Inside my application with UIDocumentInteractionController Read/Only
I am trying to open a document from inside my application with word using the UIDocumentInteractionController. The document opens but the document opens in Read/Only. It provides me with an option to "Save Copy" . This is only happening in iOS 17. iPadOS and iOS. This works 100% on iOS 16 so the issue was introduced with the new version of ios 17. `documentController = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(filePath)); string fileExtension = Path.GetExtension(sharedPath.FileName).Substring(1); string uti = MobileCoreServices.UTType.CreatePreferredIdentifier(MobileCoreServices.UTType.TagClassFilenameExtension.ToString(), fileExtension, null); documentController.Uti = uti; UIView presentingView = UIApplication.SharedApplication.KeyWindow.RootViewController.View; documentController.PresentOpenInMenu(CGRect.Empty, presentingView, true);` info.Plist UIFileSharingEnabled LSSupportsOpeningDocumentsInPlace UISupportsDocumentBrowser Is there perhaps something that changed that is not documented. It seems that some sort of security issue that has changed with how my app and word is allowed to access the file inside my app folder. I have logged multiple questions on other platforms but no luck.
2
0
342
Nov ’23
Learning Swift on macOS, struggling with acl_free
I'm converting some code that reads a file's ACLs and I'm having a hard time with acl_free crashing. There is clearly something that I am not understanding. // --- Do we have access control lists? let fileSecurity = properties.fileSecurity var accessList: acl_t? if CFFileSecurityCopyAccessControlList( fileSecurity, &accessList ) { print( "We have access control lists" ) acl_free( &accessList ) } The line acl_free( &accessList ) crashes with the following. malloc: *** error for object 0x16d28ac40: pointer being freed was not allocated In the debugger I can see that accessList points to an object. "accessList = acl_t? 0x000000015b00ea00" I notice that the crash report ptr ref is different to what Xcode is showing in the debugger. Also worth noting, is CFFileSecurityCopyAccessControlList takes a "UnsafeMutablePointer<acl_t?>!" While acl_free takes a "void *obj_p" Can anyone please give me a ptr as to what I am doing wrong? 16" MacBook Pro M2 Pro with Ventura 13.5 running Xcode 15.0
1
0
460
Nov ’23
SMB credentials check on Sonoma
I can't anymore check SMB credentials on macOS. Sonoma stops checking them after first successful validation. No matter which way to go, it could be smbutil, mount_smbfs, or even the NetFSMountURLSync function from the NetFS framework. Below is a script that works fine on Ventura, but would fail on Sonoma. There are three smbutil calls. The first call includes the correct username and password, the second omits the password, the third goes with an incorrect password. On Ventura and earlier the second and third calls fail, but Sonoma feels okay. The same could be seen with mount_smb. Is it a normal behavior? Is there a way to check credentials on Sonoma? #!/bin/bash # SMB server or its ip address SERVER="127.0.0.1" RESOURCE="Test" # username and password to authenticate on the server USER_NAME="username" USER_PASSWORD="password" # path to mount server resource MOUNT_POINT="/tmp/1" function Raise { echo "!!! ERROR: $1" exit 1 } /usr/bin/smbutil view -ANf "//${USER_NAME}:${USER_PASSWORD}@${SERVER}" || Raise "Unable to browse shares with credentials" /usr/bin/smbutil view -ANf "//${USER_NAME}:@${SERVER}" && Raise "Successfully browsed shares without password" /usr/bin/smbutil view -ANf "//${USER_NAME}:foobar@${SERVER}" && Raise "Successfully browsed shares with incorrect password" mkdir -p "/tmp/1" mount_smbfs -s "//${USER_NAME}:${USER_PASSWORD}@${SERVER}/${RESOURCE}" "/tmp/1" || Raise "Unable to mount share with credentials" diskutil unmount "/tmp/1" mkdir -p "/tmp/1" mount_smbfs -s "//${USER_NAME}:foobar@${SERVER}/${RESOURCE}" "/tmp/1" && Raise "Successfully mounted with incorrect password" diskutil unmount "/tmp/1"
4
3
1.4k
Feb ’24
Create a dmg file from a sandboxed application
Hello, I am trying to create a dmg file by launching hdiutil through my swift program. This swift program is sandboxed. Here is what i've done: let hdd_file:String = NSHomeDirectory() + "hdd.dmg.sparseimage" let process = Process() process.launchPath = "/usr/bin/hdiutil" process.arguments = ["create", "-size", "30g", "-fs", "'APFS'", "-volname", "myvolume", "-type", "SPARSE", hdd_file] let pipe = Pipe() process.standardOutput = pipe process.launch() let data = try pipe.fileHandleForReading.readToEnd() print(data) I get this error: hdiutil: create failed - Device not configured I don't understand why i get this error because the dmg file is created in application's sandbox home directory. Or maybe hdiutil is forbidden but i am just creating a dmg file. I am not trying to mount a device. Do you have any idea of how i can create a dmg file from my sandboxed application ? Thanks
1
0
505
Nov ’23
UserDefaults not saving only on iOS 17
Hello. I'm experiencing some issues with my iOS application. We distribute our app through an enterprise plan, and our app is almost 8 years old. We've noticed that on iOS 17 devices, our UserDefaults files are not being saved in the Preferences folder. I've conducted research on this issue, and everywhere I looked, it indicated that the system should handle this saving process. Does anyone know what changes have been made in iOS 17 that prevent us from saving data in UserDefaults? I've tested it with a new app, and it worked perfectly fine. Below are screenshots of the 'Preferences' folder on iOS 17 and 16.
6
2
1.2k
Nov ’23
Why does it not exist via @AppStorage
2 versions 1 works 1 doesn't. UIViewRepresentable to show a PDF @AppStorage(DefaultsKey.userActiveBook.rawValue) var activeBook : URL = Bundle.main.url(forResource: "BookPlaceHolder", withExtension: "pdf")! func makeUIView(context: Context) -> PDFView { do { let pdfData = try Data(contentsOf: activeBook) pdfView.document = PDFDocument(data: pdfData) //<---- is nil ... } catch let Error { print(Error) } } fails with Error Domain=NSCocoaErrorDomain Code=260 "The file “BookPlaceHolder.pdf” couldn’t be opened because there is no such file." func makeUIView(context: Context) -> PDFView { do { let pdfData = try Data(contentsOf: Bundle.main.url(forResource: "BookPlaceHolder", withExtension: "pdf")!) pdfView.document = PDFDocument(data: pdfData) ... } catch let Error { print(Error) } } Works perfectly. What is it with using @AppStorage to access the exact same URL causing the discrepancies ?
2
0
550
Dec ’23
help needed understanding the apps file system
How often/ under what circumstances are users sandbox directory URL's changed? So I tried to save a URL to a document in @AppStorage @AppStorage(DefaultsKey.userActiveBook.rawValue) var activeBook : URL = Bundle.main.url(forResource: "BookPlaceHolder", withExtension: "pdf")! When user selects a new document activeBook is updated with the new URL. Issue is when the app relaunches from xCode the path to whatever is stored in activeBook changes. My work around so far is to just access the users Documents Directory via FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!. Then appending the last path component of activeBook. sidebar: obviously this doesn't work if the saved URL in activeBook is still the default from Bundle.main.url. I haven't written that workaround as yet. Am I only seeing issues in development due to a fresh relaunch via xCode? If the user only ever terminates the app on their device are the .documentDirectory URL's changing on each launch or do the remain constant? I don't want to write a workaround to ensure the correct URL for whats stored in @AppStorage, if the changing URL's only occurs due to launching from xCode which the end user wont ever encounter that edge case.
1
0
416
Nov ’23
NSFileCoordinator & NSFilePresenter
Context I'm using the NSFileCoordinator & NSFilePresenter in a sandboxed application to access SQLite database files and their secondary files (e.g. WAL or journal files) as per https://developer.apple.com/documentation/foundation/nsfilepresenter/1415415-primarypresenteditemurl E.g. something similar to this: var presenters: [NSFilePresenter] = ["wal", "journal", "shm"].map { ext in let presenter = SQLiteTempFilePresenter(databaseId: databaseContext.id, sqliteMainFile: url, newExt: ext) // addFilePresenter needs to be balanced with a `removeFilePresenter`. See SQLiteTempFilePresenter#deinit NSFileCoordinator.addFilePresenter(presenter) return presenter } That way there will be a NSFilePresenter for each possible secondary SQLite file (e.g. with the main file being foo/bar/database.sqlite there will be presenter for each of the secondary files foo/bar/database.sqlite-shm and foo/bar/database.sqlite-wal) Using NSFilePresenter to work with SQLite files within the Sandbox environment works as expected. Desired change I'd like to expand the usage of NSFileCoordinator to react to changes to the SQLite files that happen outside of the application. To achieve that I added an additional NSFilePresenter for the main file (e.g. foo/bar/database.sqlite) that has a func presentedItemDidChange() method. That method does get called when I change the corresponding SQLite file (e.g. by using the sqlite3 command line tool). So far so good. But in WAL mode (https://www.sqlite.org/wal.html), changes to the SQLite file don't immediately change the file itself but get written to the write-ahead-log first (e.g. foo/bar/database.sqlite-wal in this example). Only when the outside connection is closed, will the changes be committed to the main SQLite file itself. At which point the NSFilePresenter#presentedItemDidChange() method will be called. So I also like to be notified when the secondary files change. Adding a presentedItemDidChange() callback method to the SQLiteTempFilePresenter instances for the secondary files does not seem to work, the method never gets called even though the corresponding secondary files change. Questions If I add another instance of the NSFilePresenter for each of the secondary files, the callback presentedItemDidChange() gets called for the secondary files as well. Having two different instance of the NSFilePresenter for a single URL (one for sandboxing purposes, the other for being notified of file changes) seems a bit fishy though. Is that the intended (or at least an acceptable) way of using NSFilePresenter? The documentation for NSFilePresenter states that "If another process uses a file coordinator for the same file or directory, your presenter objects are similarly notified whenever the other process makes its changes." I do get notified though when using the sqlite3 command line tool which does not use a NSFileCoordinator. Is there any documentation that explains that behaviour? I mean it's great that it seems to work but I'd like to understand why.
0
0
394
Nov ’23
NSFileCoordinator with SQLite library
Context I'm using https://github.com/stephencelis/SQLite.swift as the access layer for interacting with SQLite databases. For each database I maintain a long running connection. As reading the schema is done lazily on each connection and can be expensive I prefer to open the connection once, do a number of operations and then close the connection when it is not needed any more (and given that the app is a GUI for working with SQLite databases, long running means long running). E.g. func open(url: URL, options: ...) func commitCurrentTransaction() throws func rollbackCurrentTransaction() throws func executeStatements(rawSQL: RawSQL, executeMode: ExecuteMode) async throws -> SQLOperationResult ... func close() To be a good citizen on the Mac I'm looking to use the NSFileCoordinator to indicate read and write operations on the SQLite database files the application is using. I'm thinking of wrapping each operation that might result in a file access with the corresponding coordinate(readingItemAt: ... and coordinate(writingItemAt: ... calls. But the API documentation states that the last argument, the block byAccessor should use the given URL: A Block object containing the file operations you want to perform in a coordinated manner. This block receives an NSURL object containing the URL of the item and returns no value. Always use the URL passed into the block instead of the value in the url parameter. In my case, the URL is used once when opening the connection but not afterwards so I'd be ignoring the URL passed to the block. The examples I have found for using the NSFileCoordinator seem to follow a different pattern, where the entire operation on the file happens within the block of the coordinate method. Questions What is the correct way of using NSFileCoordinator in a case like this where there essentially is a long live file handle in use? The application does work without using the NSFileCoordinator. What is the ramification of not using the NSFileCoordinator in this case?
1
0
291
Nov ’23
Permission errors within an app after updating from MacOS 14.0 to 14.1
After updating from MacOS 14.0 to 14.1 we got permission errors within an app we developed. We got an "operation not permitted" error if the app tries to access files on a samba network share which is mounted by another tool. Additionally, we got a "Permission denied" if the app tries to run programs outside the app, for example /usr/local/bin/ip. The app works fine on Catalina, Big Sur, Monterey, Ventura and Sonoma 14.0. The app and its components are codesigned and the app is notarized, no entitlements. In the Info.plist the NSNetworkVolumesUsageDescription key is set. Any ideas? Could it be that the app is sandboxed by default on MacOS 14.1? And we have to disable sandboxing via entitlement? Thanks for any input!
6
0
1.2k
Nov ’23