2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
5 Desc: Lock - Set the write protect status of a device.
9 /**************************************************************************
15 Lock <drive> [ON|OFF] [<passkey>]
18 DRIVE/A,ON/S,OFF/S,PASSKEY
24 Lock will cause the specified device or partition to be made write-
25 protected or write-enabled. This write protection is a soft write
26 protection which is handled by the volume filesystem. Hence the
27 protection will be reset (to writable) on the next system reboot.
29 It is possible to specify an optional passkey which can be used to
30 password protect the locking. The same passkey that is used to lock
31 the volume must be used to unlock the volume. The passkey may be
32 any number of characters in length.
34 The volume given MUST be the device or root volume name, not an
41 This will lock the volume called Work: without a passkey.
44 1.SYS:> MakeDir Work:SomeDir
45 Can't create directory Work:Test
46 MakeDir: Disk is write-protected
48 The volume Work: is locked, so it is impossible to create a
51 1.SYS:> Lock Work: OFF
53 This will unlock the volume work.
55 1.SYS:> Lock Work: MyPassword
57 This will lock Work: with the passkey "MyPassword"
59 **************************************************************************/
61 #include <aros/debug.h>
63 #include <exec/types.h>
64 #include <exec/memory.h>
65 #include <dos/dosextens.h>
66 #include <dos/rdargs.h>
67 #include <proto/exec.h>
68 #include <proto/dos.h>
69 #include <utility/tagitem.h>
71 #define ARG_TEMPLATE "DRIVE/A,ON/S,OFF/S,PASSKEY"
78 const TEXT version
[] = "$VER: Lock 41.3 (7.12.2011)";
79 static const char exthelp
[] =
80 "Lock : Set the write-protect state of a volume\n"
81 "\tDRIVE/A The name of the volume to lock\n"
82 "\tON/S Turn write protect on\n"
83 "\tOFF/S Turn write protect off\n"
84 "\tPASSKEY The password string for the lock (optional)\n";
86 BOOL
lockDevice(struct DevProc
*dvp
, ULONG key
)
88 return DoPkt(dvp
->dvp_Port
, ACTION_WRITE_PROTECT
, DOSTRUE
, key
, 0, 0, 0);
91 BOOL
unlockDevice(struct DevProc
*dvp
, ULONG key
)
93 return DoPkt(dvp
->dvp_Port
, ACTION_WRITE_PROTECT
, DOSFALSE
, key
, 0, 0, 0);
96 /* Perl's hash() algorithm
98 #define HASH_MULTIPLIER 33
100 ULONG
Hash(CONST_STRPTR key
)
104 if (key
== 0 || key
[0] == 0)
107 for (hash
= 0; *key
; key
++)
108 hash
= HASH_MULTIPLIER
* hash
+ *key
;
110 /* Improve distribution of low-order bits */
120 struct RDArgs
*rd
, *rda
= NULL
;
121 IPTR args
[TOTAL_ARGS
] = { 0, FALSE
, FALSE
, 0 };
125 if((rda
= AllocDosObject(DOS_RDARGS
, NULL
)))
127 rda
->RDA_ExtHelp
= (STRPTR
)exthelp
;
128 if((rd
= ReadArgs(ARG_TEMPLATE
, args
, NULL
)))
130 /* Firstly, find out the volume */
131 dp
= GetDeviceProc((STRPTR
)args
[ARG_DRIVE
], NULL
);
133 if( dp
== NULL
|| dp
->dvp_DevNode
== NULL
134 || ( dp
->dvp_DevNode
->dol_Type
!= DLT_DEVICE
135 && dp
->dvp_DevNode
->dol_Type
!= DLT_VOLUME
)
139 error
= ERROR_OBJECT_WRONG_TYPE
;
145 /* First of find out current state. */
146 if (args
[ARG_ON
] && args
[ARG_OFF
])
149 error
= ERROR_TOO_MANY_ARGS
;
151 else if (args
[ARG_OFF
])
153 error
= unlockDevice(dp
, Hash((CONST_STRPTR
)args
[ARG_PASSKEY
])) ? IoErr() : 0;
157 error
= lockDevice(dp
, Hash((CONST_STRPTR
)args
[ARG_PASSKEY
])) ? IoErr() : 0;
165 } /* if( ReadArgs() ) */
169 FreeDosObject(DOS_RDARGS
, rda
);
170 } /* if( AllocDosObject() ) */
172 error
= ERROR_NO_FREE_STORE
;
176 PrintFault(error
, "Lock");