Task 706: .SWIFT File Format
Task 706: .SWIFT File Format
The .SWIFT file format (often written as .swift, with case-insensitivity in most file systems) is a plain text source code file used for the Swift programming language, developed by Apple. It contains code following the Swift syntax and grammar, typically encoded in UTF-8. There is no binary structure, magic number, or fixed header—it's human-readable text that can be opened in any text editor. The full specifications are defined in the official documentation, "The Swift Programming Language," available at https://docs.swift.org/swift-book/documentation/the-swift-programming-language/. Swift files are used in app development for Apple platforms like iOS, macOS, etc., and are often part of project structures in tools like Xcode.
Properties of this file format intrinsic to its file system (these are standard file metadata attributes common to most file systems, as the .SWIFT format itself has no unique internal structure beyond being plain text; I've listed common cross-platform ones):
- File name (including extension)
- File path
- File size (in bytes)
- Creation time
- Last modification time
- Last access time
- Permissions (read/write/execute for owner/group/others)
- Owner user ID/name
- Group ID/name
- File type (e.g., regular file)
- Inode number (on Unix-like systems)
- Device ID (on Unix-like systems)
- Number of hard links (on Unix-like systems)
Two direct download links for .SWIFT files:
- https://raw.githubusercontent.com/passepartoutvpn/partout/66f3ed5c01c8d357a7b625a1067f19f368698124/Package.swift
- https://raw.githubusercontent.com/SwiftOnTap/Docs/main/SwiftUI.swift
HTML/JavaScript for drag-and-drop to dump properties (embeddable in a Ghost blog post or any HTML page; it uses the browser's File API to access available properties—note that browsers limit access to full file system metadata for security, so only basic properties are dumped):
- Python class (uses
osandstatfor metadata; "decode/read" here means reading file metadata, "write" means setting modifiable properties like modification time/permissions; assumes Unix-like system for full props, but works cross-platform for basics):
import os
import stat
import time
import pwd
import grp
class SwiftFileHandler:
def __init__(self, filepath):
self.filepath = filepath
if not self.filepath.lower().endswith('.swift'):
raise ValueError("File must have .SWIFT extension")
self.stats = os.stat(self.filepath)
def read_properties(self):
props = {
'File name': os.path.basename(self.filepath),
'File path': os.path.abspath(self.filepath),
'File size': self.stats.st_size,
'Creation time': time.ctime(self.stats.st_ctime),
'Last modification time': time.ctime(self.stats.st_mtime),
'Last access time': time.ctime(self.stats.st_atime),
'Permissions': stat.filemode(self.stats.st_mode),
'Owner user ID/name': f"{self.stats.st_uid}/{pwd.getpwuid(self.stats.st_uid).pw_name if hasattr(pwd, 'getpwuid') else 'N/A'}",
'Group ID/name': f"{self.stats.st_gid}/{grp.getgrgid(self.stats.st_gid).gr_name if hasattr(grp, 'getgrgid') else 'N/A'}",
'File type': 'directory' if stat.S_ISDIR(self.stats.st_mode) else 'regular file',
'Inode number': self.stats.st_ino,
'Device ID': self.stats.st_dev,
'Number of hard links': self.stats.st_nlink
}
return props
def print_properties(self):
props = self.read_properties()
for key, value in props.items():
print(f"{key}: {value}")
def write_properties(self, new_mtime=None, new_permissions=None):
# Example: set new modification time (epoch) and permissions (octal)
if new_mtime:
os.utime(self.filepath, (self.stats.st_atime, new_mtime))
if new_permissions:
os.chmod(self.filepath, new_permissions)
self.stats = os.stat(self.filepath) # Refresh stats
# Example usage:
# handler = SwiftFileHandler('example.swift')
# handler.print_properties()
# handler.write_properties(new_mtime=time.time(), new_permissions=0o644)
# handler.print_properties()
- Java class (uses
java.nio.filefor metadata; "decode/read" means reading attributes, "write" means setting modifiable ones like times/permissions):
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.HashMap;
import java.util.Map;
public class SwiftFileHandler {
private Path path;
private BasicFileAttributes attrs;
public SwiftFileHandler(String filepath) throws IOException {
this.path = Paths.get(filepath);
if (!this.path.getFileName().toString().toLowerCase().endsWith(".swift")) {
throw new IllegalArgumentException("File must have .SWIFT extension");
}
this.attrs = Files.readAttributes(path, BasicFileAttributes.class);
}
public Map<String, Object> readProperties() throws IOException {
Map<String, Object> props = new HashMap<>();
props.put("File name", path.getFileName().toString());
props.put("File path", path.toAbsolutePath().toString());
props.put("File size", attrs.size());
props.put("Creation time", attrs.creationTime().toString());
props.put("Last modification time", attrs.lastModifiedTime().toString());
props.put("Last access time", attrs.lastAccessTime().toString());
FileOwnerAttributeView ownerView = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
props.put("Owner user ID/name", ownerView.getOwner().getName());
if (Files.getFileStore(path).supportsFileAttributeView(PosixFileAttributes.class)) {
PosixFileAttributes posixAttrs = Files.readAttributes(path, PosixFileAttributes.class);
props.put("Permissions", PosixFilePermissions.toString(posixAttrs.permissions()));
props.put("Group ID/name", posixAttrs.group().getName());
} else {
props.put("Permissions", "N/A (non-POSIX)");
props.put("Group ID/name", "N/A (non-POSIX)");
}
props.put("File type", attrs.isDirectory() ? "directory" : "regular file");
if (Files.getFileStore(path).supportsFileAttributeView(PosixFileAttributes.class)) {
PosixFileAttributes posixAttrs = Files.readAttributes(path, PosixFileAttributes.class);
props.put("Inode number", posixAttrs.fileKey()); // Approximation
} else {
props.put("Inode number", "N/A");
}
props.put("Device ID", "N/A"); // Not directly available
props.put("Number of hard links", "N/A"); // Not in basic attrs
return props;
}
public void printProperties() throws IOException {
Map<String, Object> props = readProperties();
for (Map.Entry<String, Object> entry : props.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
public void writeProperties(FileTime newMtime, String newPermissions) throws IOException {
if (newMtime != null) {
Files.setLastModifiedTime(path, newMtime);
}
if (newPermissions != null) {
Files.setPosixFilePermissions(path, PosixFilePermissions.fromString(newPermissions));
}
this.attrs = Files.readAttributes(path, BasicFileAttributes.class); // Refresh
}
// Example usage:
// public static void main(String[] args) throws IOException {
// SwiftFileHandler handler = new SwiftFileHandler("example.swift");
// handler.printProperties();
// handler.writeProperties(FileTime.fromMillis(System.currentTimeMillis()), "rw-r--r--");
// handler.printProperties();
// }
}
- JavaScript class (Node.js assumed, uses
fsfor metadata; "decode/read" means reading stats, "write" means setting modifiable like times/permissions):
const fs = require('fs');
const path = require('path');
class SwiftFileHandler {
constructor(filepath) {
this.filepath = filepath;
if (!path.extname(this.filepath).toLowerCase() === '.swift') {
throw new Error('File must have .SWIFT extension');
}
this.stats = fs.statSync(this.filepath);
}
readProperties() {
return {
'File name': path.basename(this.filepath),
'File path': path.resolve(this.filepath),
'File size': this.stats.size,
'Creation time': this.stats.birthtime.toString(),
'Last modification time': this.stats.mtime.toString(),
'Last access time': this.stats.atime.toString(),
'Permissions': (this.stats.mode & parseInt('777', 8)).toString(8),
'Owner user ID/name': this.stats.uid,
'Group ID/name': this.stats.gid,
'File type': this.stats.isDirectory() ? 'directory' : 'regular file',
'Inode number': this.stats.ino,
'Device ID': this.stats.dev,
'Number of hard links': this.stats.nlink
};
}
printProperties() {
const props = this.readProperties();
for (const [key, value] of Object.entries(props)) {
console.log(`${key}: ${value}`);
}
}
writeProperties(newMtime = null, newPermissions = null) {
if (newMtime) {
fs.utimesSync(this.filepath, new Date(), new Date(newMtime));
}
if (newPermissions) {
fs.chmodSync(this.filepath, parseInt(newPermissions, 8));
}
this.stats = fs.statSync(this.filepath); // Refresh
}
}
// Example usage:
// const handler = new SwiftFileHandler('example.swift');
// handler.printProperties();
// handler.writeProperties(Date.now(), '644');
// handler.printProperties();
- C class (uses
struct statfor metadata; "decode/read" means reading stat, "write" means setting modifiable like times/permissions; assumes Unix-like system):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <utime.h>
#include <unistd.h>
typedef struct {
char* filepath;
struct stat stats;
} SwiftFileHandler;
SwiftFileHandler* createSwiftFileHandler(const char* filepath) {
SwiftFileHandler* handler = malloc(sizeof(SwiftFileHandler));
handler->filepath = strdup(filepath);
const char* ext = strrchr(filepath, '.');
if (!ext || strcasecmp(ext, ".swift") != 0) {
fprintf(stderr, "File must have .SWIFT extension\n");
free(handler);
return NULL;
}
if (stat(filepath, &handler->stats) != 0) {
perror("stat");
free(handler->filepath);
free(handler);
return NULL;
}
return handler;
}
void destroySwiftFileHandler(SwiftFileHandler* handler) {
free(handler->filepath);
free(handler);
}
void printProperties(SwiftFileHandler* handler) {
struct tm* tm;
char buf[256];
struct passwd* pw = getpwuid(handler->stats.st_uid);
struct group* gr = getgrgid(handler->stats.st_gid);
printf("File name: %s\n", strrchr(handler->filepath, '/') ? strrchr(handler->filepath, '/') + 1 : handler->filepath);
printf("File path: %s\n", handler->filepath);
printf("File size: %lld bytes\n", (long long)handler->stats.st_size);
tm = localtime(&handler->stats.st_ctime);
strftime(buf, sizeof(buf), "%c", tm);
printf("Creation time: %s\n", buf);
tm = localtime(&handler->stats.st_mtime);
strftime(buf, sizeof(buf), "%c", tm);
printf("Last modification time: %s\n", buf);
tm = localtime(&handler->stats.st_atime);
strftime(buf, sizeof(buf), "%c", tm);
printf("Last access time: %s\n", buf);
printf("Permissions: %o\n", handler->stats.st_mode & 0777);
printf("Owner user ID/name: %d/%s\n", handler->stats.st_uid, pw ? pw->pw_name : "N/A");
printf("Group ID/name: %d/%s\n", handler->stats.st_gid, gr ? gr->gr_name : "N/A");
printf("File type: %s\n", S_ISDIR(handler->stats.st_mode) ? "directory" : "regular file");
printf("Inode number: %ld\n", (long)handler->stats.st_ino);
printf("Device ID: %ld\n", (long)handler->stats.st_dev);
printf("Number of hard links: %ld\n", (long)handler->stats.st_nlink);
}
void writeProperties(SwiftFileHandler* handler, time_t new_mtime, mode_t new_permissions) {
if (new_mtime != -1) {
struct utimbuf ut;
ut.actime = handler->stats.st_atime;
ut.modtime = new_mtime;
utime(handler->filepath, &ut);
}
if (new_permissions != -1) {
chmod(handler->filepath, new_permissions);
}
stat(handler->filepath, &handler->stats); // Refresh
}
// Example usage:
// int main() {
// SwiftFileHandler* handler = createSwiftFileHandler("example.swift");
// if (handler) {
// printProperties(handler);
// writeProperties(handler, time(NULL), 0644);
// printProperties(handler);
// destroySwiftFileHandler(handler);
// }
// return 0;
// }