balena-image-fs
Advanced tools
Comparing version 7.3.0 to 7.4.0-build-get-filesystem-label-66623b9a0b714b989bb19cef1b554c6a7db7a6eb-1
/// <reference types="node" /> | ||
import * as Fs from 'fs'; | ||
import { Disk } from 'file-disk'; | ||
import * as partitioninfo from 'partitioninfo'; | ||
/** | ||
@@ -16,1 +17,17 @@ * @summary Run a function with a node fs like interface for a partition | ||
export declare function interact<T>(disk: Disk | string, partition: number | undefined, fn: (fs: typeof Fs) => Promise<T>): Promise<T>; | ||
/** | ||
* @summary Return the label encoded in the filesystem for a given partition, | ||
* or an empty string if one can't be found. | ||
* | ||
* @example | ||
* | ||
* await filedisk.withOpenFile('/foo/bar.img', 'r', async (handle) => { | ||
* const disk = new filedisk.FileDisk(handle); | ||
* const info = partitioninfo.getPartitions(disk); | ||
* for (const partition of info.partitions) { | ||
* const label = await getLabel(disk, partition); | ||
* console.log(`${partition.index}: ${label}`); | ||
* } | ||
* } | ||
*/ | ||
export declare function getLabel(disk: Disk, partition: partitioninfo.GPTPartition | partitioninfo.MBRPartition): Promise<string>; |
@@ -18,3 +18,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.interact = void 0; | ||
exports.getLabel = exports.interact = void 0; | ||
/** | ||
@@ -163,2 +163,76 @@ * @module imagefs | ||
exports.interact = interact; | ||
/** | ||
* @summary Return the label encoded in the filesystem for a given partition, | ||
* or an empty string if one can't be found. | ||
* | ||
* @example | ||
* | ||
* await filedisk.withOpenFile('/foo/bar.img', 'r', async (handle) => { | ||
* const disk = new filedisk.FileDisk(handle); | ||
* const info = partitioninfo.getPartitions(disk); | ||
* for (const partition of info.partitions) { | ||
* const label = await getLabel(disk, partition); | ||
* console.log(`${partition.index}: ${label}`); | ||
* } | ||
* } | ||
*/ | ||
async function getLabel(disk, partition) { | ||
// If GPT, can we just read the protective MBR? | ||
// Is there a more Typescript native way to determine partition table type? | ||
// const isGpt = 'guid' in partition; | ||
// Linux native FS, expecting ext2+, so skip to superblock for metadata. | ||
let metadataOffset = 0; | ||
if (partition.type === 0x83) { | ||
metadataOffset += 0x400; | ||
} | ||
// Read filesystem metadata | ||
let buf = Buffer.alloc(0x100); | ||
await disk.read(buf, 0, buf.length, partition.offset + metadataOffset); | ||
let labelOffset = 0; | ||
let maxLength = 0; | ||
// First verify magic signature to determine metadata layout for label offset. | ||
const fatTypes = [0xB, 0xC, 0xE]; | ||
if (fatTypes.some(ptype => partition.type === ptype)) { | ||
maxLength = 11; | ||
// FAT16 | ||
if (buf.readUInt8(0x26) === 0x29) { | ||
labelOffset = 0x2B; | ||
// FAT32 | ||
} | ||
else if (buf.readUInt8(0x42) === 0x29) { | ||
labelOffset = 0x47; | ||
} | ||
else { | ||
return ''; | ||
} | ||
} | ||
else if (partition.type === 0x83) { | ||
maxLength = 16; | ||
if (buf.readUInt16LE(0x38) === 0xEF53) { | ||
labelOffset = 0x78; | ||
} | ||
else { | ||
return ''; | ||
} | ||
// Unexpected partition type | ||
} | ||
else { | ||
return ''; | ||
} | ||
// Identify and exclude trailing /0 bytes to stringify. | ||
let i = 0; | ||
for (; i <= maxLength; i++) { | ||
// If label fills available space, no need to test. We just need i | ||
// to have the expected value for Buffer.toString() call below. | ||
if (i == maxLength) { | ||
break; | ||
} | ||
if (buf.readUInt8(labelOffset + i) == 0) { | ||
break; | ||
} | ||
} | ||
const label = buf.toString('utf8', labelOffset, labelOffset + i).trim(); | ||
return label; | ||
} | ||
exports.getLabel = getLabel; | ||
//# sourceMappingURL=index.js.map |
@@ -7,4 +7,8 @@ # Change Log | ||
## 7.3.0 - 2025-01-02 | ||
## 7.4.0 - 2025-01-15 | ||
* Add getLabel() to retrieve filesystem label [Ken Bannister] | ||
## 7.3.0 - 2025-01-06 | ||
* Drop Bluebird from devDependencies [Thodoris Greasidis] | ||
@@ -11,0 +15,0 @@ * flowzone: Remove empty with clause [Thodoris Greasidis] |
{ | ||
"name": "balena-image-fs", | ||
"version": "7.3.0", | ||
"version": "7.4.0-build-get-filesystem-label-66623b9a0b714b989bb19cef1b554c6a7db7a6eb-1", | ||
"description": "Image filesystem manipulation utilities", | ||
@@ -26,3 +26,3 @@ "main": "build/index.js", | ||
"build": "npm run clean && tsc", | ||
"test": "npm run lint && mocha -r ts-node/register tests/e2e.ts", | ||
"test": "npm run lint && mocha -r ts-node/register tests/*.ts", | ||
"readme": "jsdoc2md --template doc/README.hbs build/index.js > README.md", | ||
@@ -63,4 +63,4 @@ "prepublish": "npm run test && npm run build && npm run readme" | ||
"versionist": { | ||
"publishedAt": "2025-01-02T16:41:02.856Z" | ||
"publishedAt": "2025-01-15T04:33:01.361Z" | ||
} | ||
} |
@@ -30,2 +30,7 @@ balena-image-fs | ||
* [imagefs](#module_imagefs) | ||
* [~interact()](#module_imagefs..interact) | ||
* [~getLabel()](#module_imagefs..getLabel) | ||
<a name="module_imagefs..interact"></a> | ||
@@ -43,3 +48,20 @@ | ||
``` | ||
<a name="module_imagefs..getLabel"></a> | ||
### imagefs~getLabel() | ||
**Kind**: inner method of [<code>imagefs</code>](#module_imagefs) | ||
**Summary**: Return the label encoded in the filesystem for a given partition, | ||
or an empty string if one can't be found. | ||
**Example** | ||
```js | ||
await filedisk.withOpenFile('/foo/bar.img', 'r', async (handle) => { | ||
const disk = new filedisk.FileDisk(handle); | ||
const info = partitioninfo.getPartitions(disk); | ||
for (const partition of info.partitions) { | ||
const label = await getLabel(disk, partition); | ||
console.log(`${partition.index}: ${label}`); | ||
} | ||
} | ||
``` | ||
Support | ||
@@ -46,0 +68,0 @@ ------- |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
34249
264
89
1