add touch method for changing file timestamps

This commit is contained in:
Martin Rädlinger 2017-06-29 14:54:55 +02:00
parent 2a1e02df76
commit 646ca20f64
4 changed files with 57 additions and 0 deletions

View File

@ -470,6 +470,16 @@ var RNFS = {
};
},
touch(filepath: string, mtime?: Date, ctime?: Date): Promise<void> {
if (ctime && !(ctime instanceof Date)) throw new Error('touch: Invalid value for argument `ctime`');
if (mtime && !(mtime instanceof Date)) throw new Error('touch: Invalid value for argument `mtime`');
return RNFSManager.touch(
normalizeFilePath(filepath),
mtime && mtime.getTime(),
ctime && ctime.getTime()
);
},
MainBundlePath: RNFSManager.RNFSMainBundlePath,
CachesDirectoryPath: RNFSManager.RNFSCachesDirectoryPath,
DocumentDirectoryPath: RNFSManager.RNFSDocumentDirectoryPath,

View File

@ -427,6 +427,10 @@ Check in the Android assets folder if the item exists. `filepath` is the relativ
Reads the file at `path` and returns its checksum as determined by `algorithm`, which can be one of `md5`, `sha1`, `sha224`, `sha256`, `sha384`, `sha512`.
### `touch(filepath: string, mtime?: Date, ctime?: Date): Promise<string>`
Sets the modification timestamp `mtime` and creation timestamp `ctime` of the file at `filepath`. Setting `ctime` is only supported on iOS, android always sets both timestamps to `mtime`.
### `mkdir(filepath: string, options?: MkdirOptions): Promise<void>`
```

View File

@ -637,6 +637,38 @@ RCT_EXPORT_METHOD(copyAssetsFileIOS: (NSString *) imageUri
}
RCT_EXPORT_METHOD(touch:(NSString*)filepath
mtime:(NSDate *)mtime
ctime:(NSDate *)ctime
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSFileManager *manager = [NSFileManager defaultManager];
BOOL exists = [manager fileExistsAtPath:filepath isDirectory:false];
if (!exists) {
return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file, open '%@'", filepath], nil);
}
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
if (mtime) {
[attr setValue:mtime forKey:NSFileModificationDate];
}
if (ctime) {
[attr setValue:ctime forKey:NSFileCreationDate];
}
NSError *error = nil;
BOOL success = [manager setAttributes:attr ofItemAtPath:filepath error:&error];
if (!success) {
return [self reject:reject withError:error];
}
resolve(nil);
}
- (NSNumber *)dateToTimeIntervalNumber:(NSDate *)date
{
return @([date timeIntervalSince1970]);

View File

@ -615,6 +615,17 @@ public class RNFSManager extends ReactContextBaseJavaModule {
promise.resolve(info);
}
@ReactMethod
public void touch(String filepath, double mtime, double ctime, Promise promise) {
try {
File file = new File(filepath);
promise.resolve(file.setLastModified((long) mtime));
} catch (Exception ex) {
ex.printStackTrace();
reject(promise, filepath, ex);
}
}
private void reject(Promise promise, String filepath, Exception ex) {
if (ex instanceof FileNotFoundException) {
rejectFileNotFound(promise, filepath);